litellm/tests/unit/proxy/guardrails/guardrail_hooks
alberto f768fb32ef
fix(streaming)!: send the include_usage chunk with an empty choices array (#47109)
* fix: Send the include_usage chunk with an empty choices array

Signed-off-by: Alberto Perdomo <aperdomo@redhat.com>

* test: Skip chunks with no choices when reading streams

Signed-off-by: Alberto Perdomo <aperdomo@redhat.com>

* fix: Strip usage from plain-iterator async stream chunks

Signed-off-by: Alberto Perdomo <aperdomo@redhat.com>

* test: Expect empty choices on the usage chunk in the gemma tracing test

Signed-off-by: Alberto Perdomo <aperdomo@redhat.com>

* fix(streaming): send the empty-choices usage chunk only to callers that asked for include_usage

The usage chunk now has choices=[] only when the caller set
stream_options.include_usage itself. When the OpenAI handler, the proxy's
always_include_stream_usage setting, or the harness added include_usage on the
caller's behalf, the chunk keeps its legacy single empty-delta choice, so
clients that read choices[0] on every chunk keep working.

The proxy and harness mark their injection with the private
_litellm_stream_usage_injected flag, and the proxy scrubs a client-sent copy.
Plain-iterator streams keep usage on their chunk when usage was not requested,
and the LLM Shield trailing flush ignores chunks with no choices.

* fix(proxy): mark the default stream usage injection so hooks keep the legacy usage chunk shape

The proxy's default strip-mode injection adds include_usage for clients that did not ask and drops the usage chunk before it reaches them, but streaming iterator hooks still saw it with choices=[]. Marking that injection with _litellm_stream_usage_injected keeps the old single empty choice for them.

* fix(router): treat deployment and router-default stream_options as injected, and drop the client flag on the queue route

* refactor(router): move the stream usage injection check into router_utils

* fix(router): read stream_options from pydantic deployment params when marking injected usage

Update the mantle, openinference, and text-completion integration tests to expect the include_usage chunk with empty choices.

* fix(proxy): keep the stream usage injection marker out of stored request bodies

* test(streaming): cover who asked for stream usage on chat, queue, deployment and spend log paths

* test: type the queue flag test, pin the logging start time, and build the deployment usage config without mutation

---------

Signed-off-by: Alberto Perdomo <aperdomo@redhat.com>
Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-21 02:26:05 +00:00
..
akto feat(guardrails): support logging_only mode for the Akto guardrail (#45461) 2026-10-08 17:28:08 -07:00
aporia_ai refactor(types): replace Any with proven types in 157 files (#44798) 2026-10-06 15:31:54 +00:00
azure fix(guardrails): restore Azure guardrail get_user_prompt dispatch and allow logging (#44067) 2026-10-01 23:30:32 -07:00
content_filter refactor(types): replace Any with proven types in 157 files (#44798) 2026-10-06 15:31:54 +00:00
dynamoai chore(typing): clear 670 basedpyright Any errors by validating provider payloads at the boundary (#45388) 2026-10-13 15:35:23 -07:00
guardrails_ai test: move routing, caching and callback tests out of local_testing (#45761) 2026-10-10 09:33:09 -07:00
ibm_guardrails chore(typing): clear 670 basedpyright Any errors by validating provider payloads at the boundary (#45388) 2026-10-13 15:35:23 -07:00
javelin test: move whole-unit legacy test files into tests/unit and delete dead skips (#44807) 2026-10-07 05:02:28 +00:00
litellm_content_filter fix(guardrails): record every content filter match in logging_only and offer the mode in the UI (#46673) 2026-10-16 00:37:03 -07:00
noma fix(guardrails): honor streaming settings on the Noma v2 guardrail (#46606) 2026-10-15 18:29:05 -07:00
openai feat(openai)!: honor per-deployment workload identity on non-chat routes (#39966) 2026-10-16 22:25:15 +00:00
semantic_guard test: migrate SDK and proxy-booting legacy CircleCI tests to their tiers (#45758) 2026-10-11 00:17:21 -07:00
unified_guardrails fix(guardrails): scan or refuse attachments in the bedrock guardrail (#43383) 2026-10-10 16:48:37 -07:00
zscaler_ai_guard feat(zscaler_ai_guard): send the request user to AI Guard (#46094) 2026-10-12 16:13:22 -07:00
__init__.py
_cisco_ai_defense_test_utils.py
code_execution_compliance_dataset.json
test_agent_365.py feat(mcp): hand listed-tool description and input schema to pre-call hooks per caller (#41162) 2026-10-04 00:53:58 -07:00
test_aim.py test: move routing, caching and callback tests out of local_testing (#45761) 2026-10-10 09:33:09 -07:00
test_airia.py feat(guardrails): add Airia guardrail integration (#40784) 2026-10-14 15:45:47 -07:00
test_alice.py
test_bedrock_guardrails.py test(bedrock): detect Vault reads on the event-loop thread (#47597) 2026-10-20 10:45:04 -07:00
test_bedrock_invoke_guardrail_checks.py
test_block_code_execution.py
test_block_code_execution_compliance.py
test_cato_networks.py
test_cisco_ai_defense_chat.py
test_cisco_ai_defense_mcp.py
test_compresr.py
test_conduct.py
test_crowdstrike_aidr.py
test_custom_code_bounded_execution.py test(unit): wait on the work itself instead of fixed sleeps (#46261) 2026-10-13 12:41:06 -07:00
test_decision_model.py fix(guardrails): resolve decision models in caller team scope (#46085) 2026-10-15 11:43:36 -07:00
test_deepkeep.py test: migrate SDK and proxy-booting legacy CircleCI tests to their tiers (#45758) 2026-10-11 00:17:21 -07:00
test_dynamoai.py
test_enkryptai.py
test_generic_guardrail_api.py feat(guardrails): add num_retries to generic_guardrail_api for checks lost after connect (#46965) 2026-10-17 12:22:41 -07:00
test_grayswan.py
test_headroom.py test(headroom): pin that SDK-echoed null tool_calls never reach /v1/compress (#39667) 2026-10-10 17:29:46 -07:00
test_hiddenlayer.py fix(guardrails): avoid blocking the event loop during HiddenLayer JWT refresh (#45437) 2026-10-09 22:29:24 -07:00
test_javelin.py
test_lakera_ai_v2.py test: move whole-unit legacy test files into tests/unit and delete dead skips (#44807) 2026-10-07 05:02:28 +00:00
test_lasso.py test: migrate SDK and proxy-booting legacy CircleCI tests to their tiers (#45758) 2026-10-11 00:17:21 -07:00
test_llm_shield_proxy.py fix(streaming)!: send the include_usage chunk with an empty choices array (#47109) 2026-10-21 02:26:05 +00:00
test_mcp_end_user_permission.py
test_mcp_security.py fix(mcp): keep worker MCP configurations consistent via a catalog revision (#42568) 2026-10-06 18:21:29 -07:00
test_microsoft_purview.py fix(streaming): keep finish_reason and provider usage when rebuilding streamed text completions (#44349) 2026-10-20 05:31:39 +00:00
test_model_armor.py
test_noma.py
test_noma_v2.py fix(bedrock)!: stream Converse passthrough frames through post_call guardrails instead of buffering (#46307) 2026-10-17 00:53:53 -07:00
test_onyx.py
test_ovalix.py
test_pangea.py
test_panw_prisma_airs.py fix(security): remove the publicly known master key from the repo (#44718) 2026-10-06 10:55:24 -07:00
test_presidio.py fix(presidio): stop overlapping entity spans from corrupting numbered PII tokens (#47024) 2026-10-17 11:21:03 -07:00
test_presidio_union_fix.py
test_promptguard.py
test_qualifire.py
test_repelloai.py
test_response_rejection_guardrail_code.py
test_singulr.py
test_straiker.py fix(config): treat blank env values as unset so the proxy neither crash-loops nor silently drops a guardrail (#46341) 2026-10-14 12:49:31 -07:00
test_structured_messages_writeback.py
test_tool_permission.py fix(anthropic): carry safeguard_results and stop_details in synthesized message_delta events (#46096) 2026-10-17 16:48:37 +00:00
test_tool_policy_guardrail.py fix(responses bridge): round-trip tool_search_call and tool_search_output items between turns (#46444) 2026-10-14 21:40:14 -07:00
test_typesafe.py
test_vigil_guard.py
test_wingback.py feat(guardrails): add Wingback guardrail integration (#46136) 2026-10-12 22:18:10 -07:00
test_xecguard.py