InferenceWallCallback handler that you call explicitly before and after the LLM invocation. Both approaches scan user input before it reaches the LLM and scan the LLM output before it reaches your user.
Install
Approach 1: Wrapper function
guarded_chat is a drop-in wrapper that scans the input, calls your LangChain model (or runs in demo mode without one), and scans the output — all in a single function call.
1
Scan input and block if needed
guarded_chat calls inferwall.scan_input() before touching the model. Blocked requests return a string message immediately; flagged requests log a warning and proceed.2
Call your LangChain model
Pass your chat model as the second argument. If you omit it, the function runs in demo mode with a simulated response.
3
Scan output and block if needed
Approach 2: Callback handler
InferenceWallCallback gives you explicit control. Call guard.on_input() before invoking the model and guard.on_output() on the result. Both methods raise ValueError when the content is blocked, so your existing exception-handling code catches them naturally.
1
Instantiate the handler
block_on_flag=True if you want flagged content treated as blocked:2
Scan input before invoking the model
on_input returns the original text unchanged so you can chain the call. It raises ValueError if InferenceWall blocks the prompt.guard.last_input_scan holds the full ScanResponse for logging or auditing.3
Invoke the model and scan the output
guard.last_output_scan holds the output scan result.Complete example
Agent compatibility
The callback handler approach works with LangChain agents: call
guard.on_input() before invoking the agent and wrap tool outputs with guard.on_output() before passing them back into the agent loop. The wrapper function is better suited to simple chain or single-turn invocations.