mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
Merge 7d33d5b043 into fbd126e518
This commit is contained in:
commit
ea94694f45
2 changed files with 36 additions and 0 deletions
|
|
@ -10176,6 +10176,16 @@ def _fast_serialize_simple_model_response_stream(
|
|||
):
|
||||
return None
|
||||
|
||||
# Fields set on the chunk outside its schema (e.g. by a streaming iterator
|
||||
# hook) live in ``model_extra``. The payload below only writes
|
||||
# ``service_tier``, so any other extra would be silently dropped here
|
||||
# while the canonical serializer keeps it. None-valued extras (e.g. the
|
||||
# ``citations=None`` the stream wrapper sets on every chunk) are dropped by
|
||||
# ``exclude_none`` there too, so they must not force the slow path.
|
||||
model_extra: Final = getattr(chunk, "model_extra", None)
|
||||
if model_extra and any(key != "service_tier" and value is not None for key, value in model_extra.items()):
|
||||
return None
|
||||
|
||||
choices: Final = getattr(chunk, "choices", None)
|
||||
if not isinstance(choices, list) or len(choices) != 1:
|
||||
return None
|
||||
|
|
|
|||
|
|
@ -553,6 +553,32 @@ def test_fast_serialize_simple_model_response_stream_with_usage_returns_none_inv
|
|||
assert _fast_serialize_simple_model_response_stream(chunk) is None
|
||||
|
||||
|
||||
def test_fast_serialize_simple_model_response_stream_with_extra_field_returns_none():
|
||||
"""A field a callback attaches to the chunk is not in the fast payload, so
|
||||
the fast path must bail rather than drop it."""
|
||||
chunk = _simple_chunk()
|
||||
chunk.gateway_metadata = {"served_by": "fallback-model"}
|
||||
assert _fast_serialize_simple_model_response_stream(chunk) is None
|
||||
|
||||
|
||||
def test_fast_serialize_simple_model_response_stream_with_none_extra_uses_fast_path():
|
||||
"""The stream wrapper sets ``citations=None`` on every OpenAI-style chunk.
|
||||
``exclude_none`` drops it on the slow path too, so it must not disable
|
||||
the fast path."""
|
||||
chunk = _simple_chunk()
|
||||
chunk.citations = None
|
||||
fast = _fast_serialize_simple_model_response_stream(chunk)
|
||||
assert fast is not None
|
||||
assert "citations" not in json.loads(fast)
|
||||
|
||||
|
||||
def test_serialize_streaming_chunk_keeps_extra_field():
|
||||
chunk = _simple_chunk()
|
||||
chunk.gateway_metadata = {"served_by": "fallback-model"}
|
||||
payload = json.loads(_serialize_streaming_chunk(chunk))
|
||||
assert payload["gateway_metadata"] == {"served_by": "fallback-model"}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# _serialize_streaming_chunk
|
||||
# ---------------------------------------------------------------------------
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue