IntentLayerDocsDashboard ↗
Docs/Integrations
Integrations

FastAPI middleware

Pure ASGI middleware that gates POST chat payloads and returns clarifying questions in suggest mode.

Repository example implements pure ASGI middleware for POST /chat. It reads JSON message, resolves session ID, awaits gate, and can short-circuit with questions.

#Run

bash
python3 -m venv .venv-fastapi
source .venv-fastapi/bin/activate
pip install -e sdk -r examples/fastapi-middleware/requirements.txt
uvicorn app:app --app-dir examples/fastapi-middleware --port 8011

#Gate path

python
result = await intentlayer.gate_async(
    user_message,
    session_id,
    context={"integration": "fastapi-middleware"},
)

if self.mode == "suggest" and result.questions:
    return JSONResponse({
        "clarifying_questions": result.questions,
        "session_id": session_id,
    })

Session lookup order: X-Session-ID, then session_id cookie, then generated anonymous UUID. Middleware replays consumed request body to downstream app and stores session at request.state.intentlayer_session_id.

#Try

bash
curl http://localhost:8011/chat \
  -H "content-type: application/json" \
  -H "x-session-id: customer-42" \
  -d '{"message":"help me"}'

Example assumes only POST /chat contains chat payload and body field is message. Adapt path/extraction for application. Invalid payload returns 422.

Source truthexamples/fastapi-middleware/README.mdexamples/fastapi-middleware/app.py