From be69d8c75321083cd89398d9f26469f06c2f53a2 Mon Sep 17 00:00:00 2001 From: inchang-ing <291394088@qq.com> Date: Tue, 27 Oct 2026 03:16:45 +0800 Subject: [PATCH] fix(streaming_handler): skip content parts without usable text delta_content_as_str joined parts with part.get("text", ""), which returns None when the key is present with a null value, raising TypeError in "".join. Non-mapping elements raised AttributeError. Both abort the mid-stream accumulation the helper exists to protect, so skip any part that yields no str and keep typing objects via getattr. --- .../streaming_chunk_builder_utils.py | 13 +++++--- .../test_streaming_chunk_builder_utils.py | 31 ++++++++++++++++++- .../test_streaming_handler.py | 29 +++++++++++++++++ 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py index b6ef7eab932..19450f0e688 100644 --- a/litellm/litellm_core_utils/streaming_chunk_builder_utils.py +++ b/litellm/litellm_core_utils/streaming_chunk_builder_utils.py @@ -1386,15 +1386,20 @@ def delta_content_as_str(content: object) -> str: OpenAI-compatible providers are supposed to stream delta.content as a string, but some stream a list of content parts instead. Join the text - parts into one string and map everything else to an empty string, so the - running answer text neither loses the content nor raises on a list. + parts into one string and skip every part that carries no usable text, so + the running answer text neither loses the content nor raises when a part + holds a null value or is not a mapping at all. """ if isinstance(content, str): return content if not isinstance(content, list): return "" - parts: Final = cast("Sequence[Mapping[str, str]]", content) # cast-ok: json content parts are dicts - return "".join(part.get("text", "") for part in parts) + texts: list[str] = [] + for part in content: + text = part.get("text") if isinstance(part, Mapping) else getattr(part, "text", None) + if isinstance(text, str): + texts.append(text) + return "".join(texts) def _simple_text_part(choices: Sequence[object]) -> str | None: 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 1eafd47f706..814b2e3e881 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 @@ -12,7 +12,10 @@ import respx import litellm from litellm import ChatCompletionUsageBlock, stream_chunk_builder from litellm.types.utils import GenericStreamingChunk -from litellm.litellm_core_utils.streaming_chunk_builder_utils import ChunkProcessor +from litellm.litellm_core_utils.streaming_chunk_builder_utils import ( + ChunkProcessor, + delta_content_as_str, +) from litellm.llms.anthropic.chat.handler import ModelResponseIterator from litellm.types.utils import ( ChatCompletionDeltaToolCall, @@ -2216,3 +2219,29 @@ def test_stream_chunk_builder_joins_content_part_list_delta(): response = stream_chunk_builder(chunks=chunks) assert response is not None assert response.choices[0].message.content == "Hello world" + + +def test_delta_content_as_str_skips_parts_without_usable_text(): + """Parts holding a null value or not being mappings must not abort the join.""" + assert delta_content_as_str([{"type": "text", "text": "a"}, {"type": "text", "text": "b"}]) == "ab" + assert delta_content_as_str([{"type": "text", "text": None}]) == "" + assert delta_content_as_str(["bare string", 42, None]) == "" + assert ( + delta_content_as_str( + [ + {"type": "text", "text": "a"}, + {"type": "text", "text": None}, + {"type": "text", "text": "b"}, + ] + ) + == "ab" + ) + + +def test_delta_content_as_str_reads_text_from_non_mapping_parts(): + """A part exposing only a ``text`` attribute still contributes its text.""" + + class _TextPart: + text = "from attribute" + + assert delta_content_as_str([_TextPart()]) == "from attribute" diff --git a/tests/unit/litellm_core_utils/test_streaming_handler.py b/tests/unit/litellm_core_utils/test_streaming_handler.py index b1eb5dd8b5a..aa023c4988b 100644 --- a/tests/unit/litellm_core_utils/test_streaming_handler.py +++ b/tests/unit/litellm_core_utils/test_streaming_handler.py @@ -6725,3 +6725,32 @@ def test_streaming_delta_content_list_accumulates_as_str(): pass assert response.response_uptil_now == "hello world" + + +def test_streaming_accumulates_content_part_with_null_text(): + """A content part carrying a null value must not abort the accumulated stream.""" + chunk: Final = ModelResponseStream( + id="chunk-null", + object="chat.completion.chunk", + created=1741037892, + model="gpt-5.6", + choices=[ + StreamingChoices( + index=0, + delta=Delta(role="assistant", content=[{"type": "text", "text": None}]), + finish_reason=None, + ) + ], + ) + + response: Final = CustomStreamWrapper( + completion_stream=iter([chunk]), + model="gpt-5.6", + logging_obj=MagicMock(), + custom_llm_provider="openai", + ) + + for _ in response: + pass + + assert response.response_uptil_now == ""