Skip to main content
The FastAPI integration adds InferenceWall scanning to your entire LLM API with a single @app.middleware("http") decorator. Every POST request with a JSON body is scanned before it reaches your endpoint, and every JSON response is scanned before it leaves your server — without changing any of your endpoint code.

Install

Steps

1

Register the middleware

Add the inferwall_middleware function to your FastAPI app. The decorator intercepts all HTTP traffic.
2

Scan the request body

The middleware reads the request body, extracts the text field (adapting to your field name), and calls inferwall.scan_input(). Blocked requests receive an immediate 403 response; flagged requests are logged and allowed through.
The middleware checks for prompt, message, and text fields in the request body. Update the data.get() calls to match the field names in your own API.
3

Forward the request to your endpoint

After the input scan, the middleware calls the next handler in the stack — your actual endpoint function.
4

Scan the response body

The middleware intercepts the response before it is sent. If the response body contains a response or text field, InferenceWall scans it. Blocked responses return 451 Unavailable For Legal Reasons.

What gets scanned automatically

Non-JSON requests and responses pass through without scanning.

Handling blocked requests

When InferenceWall blocks a request, your endpoint code never executes. The middleware returns the error response directly. The JSON body includes the decision, score, and matched_signatures so clients can inspect the reason.
The output scan only works when the response object has a body attribute populated before the middleware reads it. This is the default behavior for JSONResponse and standard FastAPI responses. Streaming responses are not scanned automatically — scan streaming chunks explicitly using inferwall.scan_output() in your endpoint.

Complete example