Skip to main content
The LangChain integration gives you two ways to add InferenceWall scanning to any LangChain chat model: a simple wrapper function that handles the scan-call-scan loop inline, and an 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

Pass 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.
After the call, 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

You can access guard.last_input_scan and guard.last_output_scan after each call to retrieve the full ScanResponse for logging, metrics, or tracing.

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.