From 6b37cc3261470a799a192cee949ee7474d37db1b Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Mon, 2 Nov 2026 12:43:12 -0800 Subject: [PATCH] fix(streaming): keep vLLM fields when a stream's only choice index is not 0 --- .../streaming_chunk_builder_utils.py | 7 +++---- .../test_streaming_chunk_builder_utils.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py index 71b1440c869..c87357122ce 100644 --- a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py +++ b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py @@ -508,7 +508,7 @@ class ChunkProcessor: "index": 0, "message": {"role": role, "content": ""}, "finish_reason": finish_reason, - **_streamed_vllm_choice_fields(_first_choices_of_index_zero(chunks)), + **_streamed_vllm_choice_fields(_first_choices(chunks)), } ] ), @@ -1510,9 +1510,8 @@ def _field_of(value: object, name: str) -> object: return getattr(value, name, None) -def _first_choices_of_index_zero(chunks: Iterable[object]) -> tuple[object, ...]: - first_choices: Final = (next(iter(object_items(_field_of(chunk, "choices"))), None) for chunk in chunks) - return tuple(choice for choice in first_choices if _field_of(choice, "index") in (0, None)) +def _first_choices(chunks: Iterable[object]) -> tuple[object, ...]: + return tuple(next(iter(object_items(_field_of(chunk, "choices"))), None) for chunk in chunks) def _choice_extras(choice: object) -> Mapping[str, object]: diff --git a/tests/unit/litellm_core_utils/test_streaming_chunk_builder_utils.py b/tests/unit/litellm_core_utils/test_streaming_chunk_builder_utils.py index 4ea8578d6b1..527c83d5760 100644 --- a/tests/unit/litellm_core_utils/test_streaming_chunk_builder_utils.py +++ b/tests/unit/litellm_core_utils/test_streaming_chunk_builder_utils.py @@ -2789,6 +2789,21 @@ def test_stream_chunk_builder_keeps_the_vllm_stop_reason_and_every_token_id_of_e ] +def test_stream_chunk_builder_keeps_the_vllm_fields_of_a_sole_choice_streamed_on_a_nonzero_index() -> None: + built: Final = stream_chunk_builder( + chunks=[ + _vllm_chat_chunk(1, "7,", stop_reason=None, token_ids=[22, 11]), + _vllm_chat_chunk(1, "8", "stop", stop_reason=",9", token_ids=[23]), + ] + ) + + assert built is not None + assert {field: built.model_dump()["choices"][0].get(field) for field in ("stop_reason", "token_ids")} == { + "stop_reason": ",9", + "token_ids": [22, 11, 23], + } + + def test_stream_chunk_builder_keeps_null_vllm_fields_and_adds_none_for_other_providers() -> None: vllm_built: Final = stream_chunk_builder( chunks=[