litellm/tests/unit/responses
owen-hj fd0ee2c94e
fix(anthropic): skip a stream data line that is not a JSON object (#53393)
* fix(anthropic): skip a stream data line that is not a JSON object

An Anthropic-format upstream that sends a data line holding a JSON number, string, array or null
(data: 5) failed /v1/chat/completions and /v1/responses with "'int' object has no attribute 'get'",
and the agentic /v1/messages iterator sent an error event after message_stop. The shared stream
parser now skips such a line the way SSE clients ignore an event they do not understand, so the
/v1/messages logging path no longer needs its own copy of that check

* test(anthropic): cover the /v1/messages log assembler skipping a data line that is not a JSON object

The log assembler no longer has its own non-object check, so this pins that a tool use stream with such a line still assembles its text and tool call

* fix(anthropic): skip a non-object stream line before decoding it and reset the agentic event name per data line

* fix(anthropic): skip a non-object data line before decoding it when looking for a /v1/messages error event

* fix(anthropic): skip a stream data line too deep or too long to decode on every Anthropic stream reader

Chat, the agentic and native /v1/messages iterators, the /v1/messages log scans, guardrail SSE readers,
and BaseModelResponseIterator (Snowflake Claude streams, Vertex streamRawPredict logging) now share one
loads_json_object helper that returns None for a payload that is not a JSON object or that json.loads
cannot finish (deep nesting, a number past the int digit limit)

* test(snowflake): inject the HTTP client instead of patching HTTPHandler.post

* fix(guardrails): refuse a /v1/messages stream event the guardrail cannot decode

A JSON object line nested too deep, or holding a number over Python's digit limit, was skipped by the guardrail readers while a JavaScript client can still parse it and show its text. The unified guardrail now refuses such a stream before it runs, and the /v1/messages assembler returns nothing for it, so Presidio, Bedrock, tool permission and Model Armor refuse it too. Spend logging still skips the line

* fix(guardrails): fail closed on unreadable Anthropic stream events in every scan mode

The streaming scan key is now None when a data line holds an object the guardrail cannot decode, or when one SSE event carries more than one data line, so the scheduler can no longer skip that round as redundant and the refusal always runs

The guardrail stream readers in the harness usage parser, the LLM Shield restorer, the blocked-usage helper and Presidio unmasking now skip a line that is nested too deep or holds a number too large to decode instead of raising, and PANW Prisma AIRS refuses such a text event instead of forwarding it unscanned

* fix(guardrails): skip a deeply nested array line in PANW and read bytearray stream chunks

* fix(guardrails): read guarded /v1/messages streams in whole frames and skip undecodable lines in more readers

The unified guardrail now rejoins transport reads into whole SSE frames for every /v1/messages stream, not only when tool calls are held, so a text event split across two reads, or inside a UTF-8 character, is still checked

The chat stream's JSON accumulator, the Rust router's /v1/messages usage reader and the Databricks invocation metrics filter now skip a line that is nested too deep or holds a number too long to decode instead of failing the stream

The check for an undecodable object line now skips json.loads when a payload has too few brackets to hit the recursion limit and no digit run long enough to hit the int digit limit, which halves its cost per scan round

* fix(guardrails): count sampled scan rounds by whole frames so split reads do not skip them

* fix(anthropic): step over a number too long to convert in the chat stream joiner so the event after it is kept

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-22 22:54:02 +00:00
..
litellm_completion_transformation fix(anthropic): skip a stream data line that is not a JSON object (#53393) 2026-11-22 22:54:02 +00:00
mcp refactor(auth_cache): type auth cache keys and keep the cache always native (#51130) 2026-11-21 17:20:30 +09:00
__init__.py
test_additional_tools.py feat(proxy): mixed Codex model picker and LiteLLM routing on native codex routes (#49408) 2026-11-06 11:14:47 -08:00
test_custom_tool_call.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_input_items_query.py fix(proxy): forward limit, order, after, before and include on GET /v1/responses/{id}/input_items (#51259) 2026-11-20 07:54:26 -08:00
test_instructions_length.py fix(responses): send bridged system prompts too long for instructions as input (#52964) 2026-11-20 11:56:47 -08:00
test_metadata_codex_callback.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_no_duplicate_spend_logs.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_null_test_fix.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_responses_api_bridge_flag.py fix(tool_choice): accept the Responses API's flat allowed_tools layout on chat completions (#50467) 2026-11-10 13:04:06 -08:00
test_responses_api_lifecycle.py fix(responses): log the resolved provider on Responses API calls (#52622) 2026-11-18 22:34:16 +00:00
test_responses_api_request_body.py fix(responses): log the resolved provider on Responses API calls (#52622) 2026-11-18 22:34:16 +00:00
test_responses_prompt_management.py fix(sdk): decide MCP gateway routing without importing the proxy handler (#45961) 2026-10-20 01:44:54 +00:00
test_responses_router_cooldown.py refactor(router): delete the python router core, the rust core is the only core (#50178) 2026-11-05 10:40:46 -08:00
test_responses_streaming_iterator.py fix(responses): resume background response streams with stream and starting_after (#48819) 2026-10-29 02:31:22 -07:00
test_responses_supported_endpoints_passthrough.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_responses_utils.py feat(gemini): support Gemini Live Avatar on Vertex AI realtime (#53270) 2026-11-21 15:06:59 -08:00
test_responses_websocket_all_providers.py fix(responses): replay tool calls a client answers in managed WebSocket history (#53410) 2026-11-22 22:23:30 +00:00
test_rust_bridge_websocket.py refactor(rust-bridge): require the native extension (#48852) 2026-10-28 13:16:56 -07:00
test_sse_output_recovery.py test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199) 2026-09-25 17:10:13 -07:00
test_streaming_iterator.py fix(responses): preserve incomplete status in synthetic streams (#52489) 2026-11-19 22:24:50 -08:00
test_streaming_iterator_error_events.py fix(responses): surface mid-stream upstream errors that arrive without an event type (#50466) 2026-11-07 14:53:07 -08:00
test_text_format_conversion.py refactor: expose core private helpers under public names (#44871) 2026-10-07 00:05:27 +00:00