mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
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.
This commit is contained in:
parent
b19363b73a
commit
be69d8c753
3 changed files with 68 additions and 5 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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 == ""
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue