fix(aiguard): evaluate LiteLLM proxy /v1/responses and /v1/messages answers #22038
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
avara1986
Description
Jira: APPSEC-71907
A LiteLLM proxy can run the Datadog AI Guard guardrail in
pre_callandpost_callmode. #21845 made the guardrail own the AI Guard evaluation of chat and text completion requests in the proxy, and left/v1/responsesand/v1/messagesrequests to the provider listeners. But litellm sends these requests with its own HTTP client (its Responses API and Anthropic Messages handlers). For/v1/messagesto an OpenAI model it can also go throughchat.completions.with_raw_response, whose answer the OpenAI integration cannot read. As a result:mcp_callandmcp_approval_requestitems, were never evaluated, and thepost_callmode could not block them./v1/messages, the guardrail's prompt conversion dropped the system prompt and thetool_useandtool_resultblocks, 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.pypost_callmode, the guardrail claims a request only when its post-call hooks evaluate the answer: chat and text completions as before, and now non-streamedresponses,aresponsesandanthropic_messagesrequests on a create route (/responsesor/v1/messages). The provider listeners skip a claimed request. Streamed Responses and Messages requests and/cursor/chat/completionsare not claimed, so the provider listeners keep their checks there.instructions,inputand thepromptvariables of a saved prompt are evaluated. litellm's own conversion drops the variables. If the converter fails, litellm's conversion still runs.-
/v1/responses: the Responses converter, with the hosted MCP servers of the requesttools(mcp_serversandmcp.server_id, gated byDD_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_itemsandcompactanswers are not.instructions,inputandprompt; Anthropicsystemandmessages), never from the request metadata, which the client controls./v1/messagesprompts. The Anthropic converter now converts them, so the system prompt,tool_useandtool_resultare 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.toolsdeclaration that cannot be read only drops the MCP metadata..cursor/rules/ai-guard.mdcdescribes the new claim conditions, the route rule, the fallback and the known limit below.| Request (
pre_callandpost_callmodes) | 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/messagesthat the Anthropic converter cannot read | same as the row above | guardrail: text of the prompt and answer; provider listeners: as before ||
/v1/responsesand/v1/messages, streamed | guardrail: prompt; OpenAI integration: prompt (/v1/messagesto OpenAI models on the chat path) | unchanged: not claimed, and the stream passes through live |Customer impact
With the guardrail in
post_callmode, a LiteLLM proxy now evaluates the non-streamed answers of/v1/responsesand/v1/messagesrequests and can block them./v1/messagesprompts are evaluated with their system prompt, tool calls and tool results. The release note describes the fix.Testing
scripts/run-tests:ai_guard_litellm_guardrailpasses on all 10 environments (litellm 1.78.5 and 1.82.6, Python 3.10 to 3.14), 249 tests in each.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_serversandmcp.server_id, and without them whenDD_AI_GUARD_MCP_ENABLED=false;- the prompt from the request body, also with only the
post_callmode;- string items of content lists;
- the converter fallback, which does not claim;
- conversion errors, which skip the evaluation and do not fail the request.
test_litellm_proxy.pysend requests through the LiteLLM proxy app in process, with offline stubs:-
/v1/responses, and/v1/messageson 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_resultis 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.
/v1/messagesassertion that codified the gap ([("litellm", "prompt"), ("openai", "prompt")]) now expects one guardrail prompt and one guardrail answer.litellm.pyofmain, 64 of the 249 tests fail.scripts/lint checks, the type comment check andreno lintpass.Risks
post_callmode, a non-streamed/v1/responsesor/v1/messagesrequest now waits for one more AI Guard evaluation (the answer), as chat completions already do./v1/messagesbody 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..cursor/rules/ai-guard.mdc: litellm's chat completions adapter for/v1/messagescan send animageblock inside atool_resultto 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./v1/responsesand/v1/messagesanswers are still not evaluated.Additional Notes
ai_guard_litellm_guardrailsuite ran.scripts/run-tests --listalso selects other suites through theddtrace/aiguard/*pattern; CI runs them.🤖 Generated with Claude Code
[APPSEC-71907]: https://datadoghq.atlassian.net/browse/APPSEC-71907?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ