← Back to Pull requests

fix(aiguard): evaluate LiteLLM proxy /v1/responses and /v1/messages answers #22038

Closed
unknown closed this pull request without merging on Nov 25, 2026 15:32 UTC
View on GitHub
unknown unknown commented on Nov 23, 2026 18:23 UTC
Author

Description

Jira: APPSEC-71907

A LiteLLM proxy can run the Datadog AI Guard guardrail in pre_call and post_call mode. #21845 made the guardrail own the AI Guard evaluation of chat and text completion requests in the proxy, and left /v1/responses and /v1/messages requests to the provider listeners. But litellm sends these requests with its own HTTP client (its Responses API and Anthropic Messages handlers). For /v1/messages to an OpenAI model it can also go through chat.completions.with_raw_response, whose answer the OpenAI integration cannot read. As a result:

  • The answers of these routes, including hosted MCP mcp_call and mcp_approval_request items, were never evaluated, and the post_call mode could not block them.

  • On /v1/messages, the guardrail's prompt conversion dropped the system prompt and the tool_use and tool_result blocks, so a prompt injection inside a tool result never reached AI Guard. The OpenAI integration also evaluated the prompt a second time.
  • Changes in ddtrace/aiguard/integrations/litellm.py

  • Claim. In the proxy with the post_call mode, the guardrail claims a request only when its post-call hooks evaluate the answer: chat and text completions as before, and now non-streamed responses, aresponses and anthropic_messages requests on a create route (/responses or /v1/messages). The provider listeners skip a claimed request. Streamed Responses and Messages requests and /cursor/chat/completions are not claimed, so the provider listeners keep their checks there.

  • Responses prompts. The pre-call check uses the Responses input converter, so instructions, input and the prompt variables of a saved prompt are evaluated. litellm's own conversion drops the variables. If the converter fails, litellm's conversion still runs.

  • Answers. The post-call hook finds the route from the proxy URL and evaluates the non-streamed answer one time:

  • - /v1/responses: the Responses converter, with the hosted MCP servers of the request tools (mcp_servers and mcp.server_id, gated by DD_AI_GUARD_MCP_ENABLED, as in the OpenAI integration);
    - /v1/messages: the Anthropic converter.
    - Only the create route of the Responses API is evaluated. Retrieve, cancel, input_items and compact answers are not.
  • Prompt of the answer evaluation. It is rebuilt from the request body (Responses instructions, input and prompt; Anthropic system and messages), never from the request metadata, which the client controls.

  • /v1/messages prompts. The Anthropic converter now converts them, so the system prompt, tool_use and tool_result are evaluated. Before the conversion, the guardrail reads each string item of the content lists as a text block, in a copy, because litellm forwards these items to the model as text.

  • Fallback. When the Anthropic converter cannot read a body, the guardrail evaluates the chat conversion of the messages and does not claim the request. Thus a provider listener that sees the model call still evaluates it.

  • Errors. A body or an answer that the converters cannot convert never fails the proxy request. The guardrail logs at debug level and skips the evaluation that it cannot build. A tools declaration that cannot be read only drops the MCP metadata.
  • .cursor/rules/ai-guard.mdc describes the new claim conditions, the route rule, the fallback and the known limit below.

    | Request (pre_call and post_call modes) | Before | After |
    | --- | --- | --- |
    | /v1/responses, not streamed | guardrail: prompt | guardrail: prompt and answer |
    | /v1/messages, not streamed | guardrail: prompt without the system prompt and tool blocks; OpenAI integration: prompt (OpenAI models on the chat path) | guardrail: prompt and answer |
    | /v1/messages that the Anthropic converter cannot read | same as the row above | guardrail: text of the prompt and answer; provider listeners: as before |
    | /v1/responses and /v1/messages, streamed | guardrail: prompt; OpenAI integration: prompt (/v1/messages to OpenAI models on the chat path) | unchanged: not claimed, and the stream passes through live |

    Customer impact

    With the guardrail in post_call mode, a LiteLLM proxy now evaluates the non-streamed answers of /v1/responses and /v1/messages requests and can block them. /v1/messages prompts are evaluated with their system prompt, tool calls and tool results. The release note describes the fix.

    Testing

  • scripts/run-tests: ai_guard_litellm_guardrail passes on all 10 environments (litellm 1.78.5 and 1.82.6, Python 3.10 to 3.14), 249 tests in each.

  • New unit tests in test_litellm_guardrail.py:

  • - the claimed call types and the route rule (create route only);
    - the conversion of Responses and Messages answers, with hosted MCP mcp_servers and mcp.server_id, and without them when DD_AI_GUARD_MCP_ENABLED=false;
    - the prompt from the request body, also with only the post_call mode;
    - string items of content lists;
    - the converter fallback, which does not claim;
    - conversion errors, which skip the evaluation and do not fail the request.
  • New proxy tests in test_litellm_proxy.py send requests through the LiteLLM proxy app in process, with offline stubs:

  • - /v1/responses, and /v1/messages on the chat path, on the litellm 1.82.6 Responses adapter and on the native Anthropic path, get one prompt and one answer evaluation from the guardrail and none from the provider listeners;
    - a DENY answer returns HTTP 403;
    - a prompt injection inside a tool_result is blocked;
    - a body that the Anthropic converter cannot read is not claimed, and it can still be blocked through the OpenAI integration;
    - streamed answers pass through.
  • The /v1/messages assertion that codified the gap ([("litellm", "prompt"), ("openai", "prompt")]) now expects one guardrail prompt and one guardrail answer.

  • With the litellm.py of main, 64 of the 249 tests fail.

  • scripts/lint checks, the type comment check and reno lint pass.
  • Risks

  • In post_call mode, a non-streamed /v1/responses or /v1/messages request now waits for one more AI Guard evaluation (the answer), as chat completions already do.

  • A /v1/messages body that the Anthropic converter cannot read gets two prompt evaluations when litellm sends it through the OpenAI SDK: one from the guardrail and one from the OpenAI integration.

  • Known limit, described in .cursor/rules/ai-guard.mdc: litellm's chat completions adapter for /v1/messages can send an image block inside a tool_result to the model as tool text (for example litellm 1.78.5 for OpenAI and Azure OpenAI models). The guardrail evaluates that block as an image, not as text.

  • Streamed /v1/responses and /v1/messages answers are still not evaluated.
  • Additional Notes

  • This change builds on #21845 and #21892, which are not released yet, so it has no backport labels.

  • Locally, only the ai_guard_litellm_guardrail suite ran. scripts/run-tests --list also selects other suites through the ddtrace/aiguard/* pattern; CI runs them.
  • 🤖 Generated with Claude Code

    [APPSEC-71907]: https://datadoghq.atlassian.net/browse/APPSEC-71907?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: avara1986 Review duration: 2709m
    All Vouch review confidence checks passed
    Residual risk 0.23 = Change Risk (0.57) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.57 · 650 lines diff across 8 files
    0.57
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #22038"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 57% (MEDIUM) · Higher = larger blast radius
    57%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%