mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
test: keep the content-part list tests in a region main does not touch so the branch merges cleanly
This commit is contained in:
parent
67e9e3585b
commit
59536a2b4a
1 changed files with 65 additions and 64 deletions
|
|
@ -10,10 +10,8 @@ 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,
|
||||
delta_content_as_str,
|
||||
stream_chunk_builder_text_completion,
|
||||
)
|
||||
from litellm.llms.anthropic.chat.handler import ModelResponseIterator
|
||||
|
|
@ -2304,6 +2302,71 @@ def test_stream_chunk_builder_keeps_null_text_completion_logprobs_fields_null()
|
|||
}
|
||||
|
||||
|
||||
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."""
|
||||
from litellm.litellm_core_utils.streaming_chunk_builder_utils import delta_content_as_str
|
||||
|
||||
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."""
|
||||
from litellm.litellm_core_utils.streaming_chunk_builder_utils import delta_content_as_str
|
||||
|
||||
class _TextPart:
|
||||
text = "from attribute"
|
||||
|
||||
assert delta_content_as_str([_TextPart()]) == "from attribute"
|
||||
|
||||
|
||||
def test_stream_chunk_builder_keeps_a_text_completion_stream_that_ends_on_a_chat_chunk() -> None:
|
||||
response: Final = litellm.stream_chunk_builder(
|
||||
chunks=[
|
||||
|
|
@ -2476,68 +2539,6 @@ def test_stream_chunk_builder_keeps_reasoning_items_on_text_only_replies():
|
|||
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"
|
||||
|
||||
|
||||
def test_stream_chunk_builder_carries_the_bedrock_guardrail_trace_and_records_it_on_the_logging_object(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue