litellm/tests/integration/streaming
Lai Wei bda4e60f9b
fix(streaming): keep finish_reason and provider usage when rebuilding streamed text completions (#44349)
* fix(streaming): keep finish_reason and provider usage when rebuilding streamed text completions

stream_chunk_builder_text_completion read finish_reason from chunks[-1] and recounted usage from messages. With include_usage the last chunk is a usage-only trailer, so finish_reason came back None (or raised IndexError when the trailer had no choices), and prompt_tokens was 0 because a text-completion prompt is not in messages. Scan for the last finish_reason and prefer the provider-reported usage, as the chat path already does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(streaming): type the text completion chunk builder so its scans add no basedpyright debt

Read chunk fields as typed attributes instead of untyped subscripts, which
clears the reportUnknownArgumentType breach the lint gate flagged

* fix(streaming): keep the provider's full usage, including token details, when rebuilding text completions

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(streaming): rebuild dict-form text completion chunks again

stream_chunk_builder routes dict chunks holding TextChoices to the text
completion builder, which now reads attributes. Convert them to
TextCompletionResponse at the routing point.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(streaming): accept dict chunks in the text rebuild and derive its usage like the chat path

Moves the dict to TextCompletionResponse conversion into stream_chunk_builder_text_completion so direct callers keep working, and builds usage through ChunkProcessor.calculate_usage over the chunks that report counts, so a trailer without total_tokens keeps its prompt and completion counts and the result is a fresh Usage

* fix(streaming): read text completion usage through the shared usage extractor so usage dicts on typed chunks rebuild

* fix(streaming): merge text completion logprobs across chunks and rebuild dict text chunks as text completions

* fix(streaming): rebuild one text completion choice per index and keep null logprobs fields null

* fix(streaming): keep dumped chat chunks with a choice-level text on the chat rebuild path

Route a dict chunk to the text completion builder only when its choice has `text` and no `delta`.
The Responses bridge snapshots chat chunks with model_dump(), and Together's chat choices carry `text`
next to `delta`, so streamed /v1/responses calls to Together ended without response.completed.

Group text completion choices by index in one sorted pass, so rebuilding an n>1 stream costs about
the same as rebuilding an n=1 stream of the same size.

* fix(streaming): keep text completion rebuilds working on mixed chunks and unreadable logprobs

A chat chunk inside a text completion stream now converts to a text chunk that keeps its finish_reason and adds no text. A dict chunk whose logprobs fail validation is read again with logprobs dropped, and logprobs that are not a Logprobs object keep the last value sent. Each of these inputs raised or lost data at the previous commit, and each now rebuilds without raising

Also adds a Purview test showing a multi-choice text stream is blocked like the non-streamed response, and Final annotations on the new test locals

* fix(streaming): rebuild text completion choices without an index at index 0

Text completion choices whose index is None now rebuild under index 0, as on the merge base, where the sort by index raised at the previous commit. A chat chunk inside a text completion stream now keeps the usage it reports, and a choice whose logprobs mix a Logprobs object with another shape keeps the last value sent, as on the merge base

* fix(streaming): rebuild text completion choices with a string index under that int

A text completion choice whose index is a string now rebuilds under the int it names, and one whose index cannot be read as an int rebuilds at index 0, as on the merge base. A string index mixed with an int index raised TypeError at the previous commit, and a string-only index came back as a string

* test(streaming): drop a docstring that repeats its test

* fix(streaming): fall back to choice 0 for an infinite text completion index

A float infinity index made int() raise OverflowError, which the index
coercion did not catch, so the rebuild failed where the merge base
returned choice 0.

---------

Co-authored-by: Lai Wei <8022184+roywei@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-10-20 05:31:39 +00:00
..
faked_responses_case.py fix(responses): open faked stream items empty so tool arguments and text arrive once (#46533) 2026-10-15 09:35:12 -07:00
test_assistants_evals_stream.py test(integration): pin assistants and evals endpoint contracts (#44734) 2026-10-14 07:08:35 +00:00
test_azure_streaming_function_calls.py test: migrate local_testing SDK and provider tests out of build_and_test (#45760) 2026-10-10 23:14:25 +00:00
test_chat_stream_guardrail_error_events.py test(integration): give each owned proxy a private database and Redis by default (#47213) 2026-10-18 03:37:41 +00:00
test_decision_model_buffering.py fix(guardrails): honor decision model streaming buffering (#46086) 2026-10-12 16:28:22 -07:00
test_decision_model_pipeline_buffering.py fix(guardrails): honor decision model streaming buffering (#46086) 2026-10-12 16:28:22 -07:00
test_fake_stream_reasoning_wire.py fix(bedrock)!: keep reasoning, thinking blocks and the json_mode finish_reason on fake streams (#46863) 2026-10-16 17:27:57 +00:00
test_faked_responses_stream_bridges.py fix(responses): open faked stream items empty so tool arguments and text arrive once (#46533) 2026-10-15 09:35:12 -07:00
test_faked_responses_stream_cache_replay.py fix(responses): open faked stream items empty so tool arguments and text arrive once (#46533) 2026-10-15 09:35:12 -07:00
test_faked_responses_stream_chaos.py test(integration): give each owned proxy a private database and Redis by default (#47213) 2026-10-18 03:37:41 +00:00
test_faked_responses_stream_guardrail_blocks.py fix(responses): open faked stream items empty so tool arguments and text arrive once (#46533) 2026-10-15 09:35:12 -07:00
test_faked_responses_stream_items.py fix(responses): open faked stream items empty so tool arguments and text arrive once (#46533) 2026-10-15 09:35:12 -07:00
test_file_content_streaming.py test(integration): regression tests for July provider translation, routing and streaming bugs (#42693) 2026-09-23 09:51:06 -07:00
test_google_genai_stream_usage_settings_wire.py test(integration): give each owned proxy a private database and Redis by default (#47213) 2026-10-18 03:37:41 +00:00
test_interception_converted_stream_options.py test(integration): give each owned proxy a private database and Redis by default (#47213) 2026-10-18 03:37:41 +00:00
test_mid_stream_fallback_continuation.py feat(router): mid-stream fallback continuation for chat completions (#41127) 2026-10-16 13:51:32 +00:00
test_noma_v2_streaming_settings.py test(integration): give each owned proxy a private database and Redis by default (#47213) 2026-10-18 03:37:41 +00:00
test_openai_like_stream_usage_wire.py fix(vertex_ai): route Model Garden models through the generateContent adapter (#41836) 2026-10-15 08:06:49 -07:00
test_openai_sdk_plain_chunk_fields.py perf(streaming): cut per-chunk pydantic round trips on openai-compatible streams (#47030) 2026-10-20 02:42:32 +00:00
test_openai_stream_passthrough_fields.py test: migrate SDK and proxy-booting legacy CircleCI tests to their tiers (#45758) 2026-10-11 00:17:21 -07:00
test_responses_bridge_stream_chaos.py test(integration): give each owned proxy a private database and Redis by default (#47213) 2026-10-18 03:37:41 +00:00
test_responses_bridge_stream_errors.py test(integration): give each owned proxy a private database and Redis by default (#47213) 2026-10-18 03:37:41 +00:00
test_responses_top_level_error_events.py fix(responses): keep the provider code and message on top-level stream error events (#46438) 2026-10-14 16:16:46 -07:00
test_stream_contracts.py test(integration): give each owned proxy a private database and Redis by default (#47213) 2026-10-18 03:37:41 +00:00
test_stream_open_error_provider_headers_proxy.py test(integration): hand every harness server a port from one per-worker block (#46761) 2026-10-15 16:56:40 -07:00
test_stream_parallel_slot_release.py test(integration): regression tests for August provider translation and streaming bugs (#42621) 2026-09-22 20:52:00 -07:00
test_text_completion_stream_guardrail_rebuild.py fix(streaming): keep finish_reason and provider usage when rebuilding streamed text completions (#44349) 2026-10-20 05:31:39 +00:00
test_ttft_keepalive.py test(integration): regression tests for August provider translation and streaming bugs (#42621) 2026-09-22 20:52:00 -07:00