litellm/tests/test_litellm/llms/anthropic/chat
Mateo Wang 4f7b20ec10
fix(guardrails): skip streaming guardrail rounds that re-scan cleared output (#39386)
* fix(guardrails): skip streaming guardrail rounds that re-scan cleared output

Streaming guardrails scanned the finished answer twice at end of stream
whenever the chunk count landed on a multiple of the sampling rate, ran
sampled rounds whose payload was identical to the previous one, and on
/v1/messages could scan an empty text before the first content chunk.
Every redundant round is a paid guardrail provider call.

Each endpoint handler now exposes a scan key describing what a round
would hand to apply_guardrail (the text so far, plus tool calls once the
stream has ended), and the unified streaming hook skips a sampled or
end-of-stream round whose key equals the last scanned one or carries
nothing to scan yet. Rounds that carry tool calls are never skipped.

* test(guardrails): expect one end-of-stream scan when the terminal chunk is sampled

Update sampled cadence expectations and use tuple-backed scan state

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: yassin <yassin@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-09-02 18:25:28 -07:00
..
guardrail_translation fix(guardrails): skip streaming guardrail rounds that re-scan cleared output (#39386) 2026-09-02 18:25:28 -07:00
conftest.py refactor: remove unnecessary comments from #27074 2026-05-04 19:34:56 +00:00
test_anthropic_chat_handler.py fix: filter deployment default API key limits 2026-09-01 21:50:45 +00:00
test_anthropic_chat_transformation.py Merge remote-tracking branch 'origin/litellm_internal_staging' into litellm_bedrock_converse_legacy_thinking_adaptive 2026-09-02 11:03:02 -07:00
test_code_interpreter_results_extraction.py fix: streaming container_id and consistent Pydantic types in output 2026-03-18 12:37:13 +01:00