mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
Merge cf4ee17844 into c69e82f538
This commit is contained in:
commit
91c5c753bc
3 changed files with 155 additions and 3 deletions
|
|
@ -706,7 +706,7 @@ class ChunkProcessor:
|
|||
content = delta.get(delta_key, "")
|
||||
if content is None:
|
||||
continue # openai v1.0.0 sets content = None for chunks
|
||||
content_list.append(content)
|
||||
content_list.append(delta_content_as_str(content))
|
||||
|
||||
# Combine the "content" strings into a single string || combine the 'function' strings into a single string
|
||||
combined_content: Final = "".join(content_list)
|
||||
|
|
@ -1476,12 +1476,33 @@ def _delta_carries_more_than_text(delta: Mapping[str, object]) -> bool:
|
|||
return any(delta.get(field) is not None for field in _NON_TEXT_DELTA_FIELDS)
|
||||
|
||||
|
||||
def delta_content_as_str(content: object) -> str:
|
||||
"""Coerce a streamed delta content value to plain text.
|
||||
|
||||
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 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 ""
|
||||
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:
|
||||
deltas: Final = tuple(_stream_choice_delta(choice) for choice in choices)
|
||||
if any(_delta_carries_more_than_text(delta) for delta in deltas):
|
||||
return None
|
||||
content: Final = deltas[0].get("content")
|
||||
return content if isinstance(content, str) else ""
|
||||
return delta_content_as_str(content)
|
||||
|
||||
|
||||
def stream_chunk_builder(
|
||||
|
|
|
|||
|
|
@ -12,7 +12,11 @@ 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, stream_chunk_builder_text_completion
|
||||
from litellm.litellm_core_utils.streaming_chunk_builder_utils import (
|
||||
ChunkProcessor,
|
||||
delta_content_as_str,
|
||||
stream_chunk_builder_text_completion,
|
||||
)
|
||||
from litellm.llms.anthropic.chat.handler import ModelResponseIterator
|
||||
from litellm.types.utils import (
|
||||
ChatCompletionDeltaToolCall,
|
||||
|
|
@ -2517,3 +2521,65 @@ def test_stream_chunk_builder_keeps_reasoning_items_on_text_only_replies():
|
|||
assert response is not None
|
||||
assert response.choices[0].message.content == "Hello world"
|
||||
assert response.choices[0].message.reasoning_items == reasoning_items
|
||||
|
||||
|
||||
def test_stream_chunk_builder_joins_content_part_list_delta():
|
||||
"""A provider streaming delta.content as a content-part list must aggregate as text."""
|
||||
chunks = [
|
||||
ModelResponseStream(
|
||||
id="chatcmpl-123",
|
||||
created=1,
|
||||
model="gpt-4.1-mini",
|
||||
object="chat.completion.chunk",
|
||||
choices=[
|
||||
StreamingChoices(
|
||||
finish_reason=None,
|
||||
index=0,
|
||||
delta=Delta(content=[{"type": "text", "text": "Hello "}], role="assistant"),
|
||||
)
|
||||
],
|
||||
),
|
||||
ModelResponseStream(
|
||||
id="chatcmpl-123",
|
||||
created=2,
|
||||
model="gpt-4.1-mini",
|
||||
object="chat.completion.chunk",
|
||||
choices=[
|
||||
StreamingChoices(
|
||||
finish_reason="stop",
|
||||
index=0,
|
||||
delta=Delta(content=[{"type": "text", "text": "world"}], role=None),
|
||||
)
|
||||
],
|
||||
),
|
||||
]
|
||||
|
||||
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"
|
||||
|
|
|
|||
|
|
@ -7844,3 +7844,68 @@ async def test_leaving_an_async_with_block_closes_the_provider_stream():
|
|||
async with _wrapper_over(stream):
|
||||
pass
|
||||
assert stream.close_calls == 1
|
||||
|
||||
|
||||
def _chunk_with_list_content(text: str, chunk_id: str, created: int) -> ModelResponseStream:
|
||||
return ModelResponseStream(
|
||||
id=chunk_id,
|
||||
object="chat.completion.chunk",
|
||||
created=created,
|
||||
model="gpt-5.6",
|
||||
choices=[
|
||||
StreamingChoices(
|
||||
index=0,
|
||||
delta=Delta(role="assistant", content=[{"type": "text", "text": text}]),
|
||||
finish_reason=None,
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
def test_streaming_delta_content_list_accumulates_as_str():
|
||||
"""A provider streaming delta.content as a content-part list must still accumulate as text."""
|
||||
chunks: Final = [
|
||||
_chunk_with_list_content("hello ", "chunk1", 1741037890),
|
||||
_chunk_with_list_content("world", "chunk2", 1741037891),
|
||||
]
|
||||
|
||||
response: Final = CustomStreamWrapper(
|
||||
completion_stream=iter(chunks),
|
||||
model="gpt-5.6",
|
||||
logging_obj=MagicMock(),
|
||||
custom_llm_provider="openai",
|
||||
)
|
||||
|
||||
for _ in response:
|
||||
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