fix(proxy): send null for chat chunk values JSON cannot encode instead of failing the stream

This commit is contained in:
mateo-berri 2026-10-31 02:17:42 -07:00
parent 65965ec352
commit 48f0bdff45
2 changed files with 27 additions and 0 deletions

View file

@ -10070,7 +10070,13 @@ def _restamp_nested_response_model_in_sse_line(line: bytes, target_model: str) -
return _SSE_DATA_FIELD + b" " + serialized_event.encode() + line_ending
def _null_for_unencodable(_value: object) -> None:
return None
def _serialize_streaming_chunk(chunk: BaseModel) -> bytes:
if isinstance(chunk, ModelResponseStream):
return chunk.model_dump_json(exclude_none=True, exclude_unset=True, fallback=_null_for_unencodable).encode()
return chunk.model_dump_json(exclude_none=True, exclude_unset=True).encode()

View file

@ -768,6 +768,27 @@ async def test_async_data_generator_sse_frame_keeps_provider_fields_on_a_text_ch
assert read_field(json.loads(_sse_text(out[0]).removeprefix("data: ").removesuffix("\n\n"))) == sent
@pytest.mark.asyncio
async def test_async_data_generator_streams_a_text_chunk_a_hook_tagged_with_a_value_json_cannot_encode(monkeypatch):
_patch_logging_flags(monkeypatch)
chunk: Final = _azure_chunk({}, {"obfuscation": "Ab3"})
setattr(chunk, "trace_handle", object())
out: Final = [
line
async for line in async_data_generator(
response=_async_iter([chunk]),
user_api_key_dict=_user_auth(),
request_data={"model": "gpt-4"},
)
]
payload: Final = json.loads(_sse_text(out[0]).removeprefix("data: ").removesuffix("\n\n"))
assert payload["choices"][0]["delta"]["content"] == "Red"
assert payload["obfuscation"] == "Ab3"
assert payload["trace_handle"] is None
@pytest.mark.asyncio
async def test_async_data_generator_uses_response_fallback_metadata(monkeypatch):
_patch_logging_flags(monkeypatch)