fix(streaming): treat null choices as empty in the raw SSE abort check

This commit is contained in:
mateo-berri 2026-10-28 05:38:10 -07:00
parent 0ece673ce7
commit 9b1bbe9a7a
2 changed files with 16 additions and 6 deletions

View file

@ -959,7 +959,7 @@ class OpenAIChatCompletionStreamingHandler(BaseModelResponseIterator):
status_code=error_status_code,
message=error_message,
)
choices = chunk.get("choices", [])
choices = chunk.get("choices") or []
if any(choice.get("finish_reason") == "abort" for choice in _CHOICE_MAPPINGS.validate_python(choices)):
raise OpenAIError(
status_code=500,

View file

@ -1447,7 +1447,10 @@ async def test_aborted_stream_raises_instead_of_finishing_successfully(
@pytest.mark.parametrize("async_stream", [False, True])
async def test_sdk_stream_with_null_choices_chunk_finishes_normally(async_stream: bool, logging_obj: Logging) -> None:
@pytest.mark.parametrize("transport", ["sdk", "http_handler"])
async def test_stream_with_null_choices_chunk_finishes_normally(
async_stream: bool, transport: str, logging_obj: Logging
) -> None:
body: Final = (
"".join(
"data: "
@ -1478,15 +1481,22 @@ async def test_sdk_stream_with_null_choices_chunk_finishes_normally(async_stream
)
client: Final = AsyncOpenAI(api_key="unused") if async_stream else OpenAI(api_key="unused")
stream: Final = (
AsyncStream(cast_to=ChatCompletionChunk, response=raw_response, client=client)
if isinstance(client, AsyncOpenAI)
else Stream(cast_to=ChatCompletionChunk, response=raw_response, client=client)
(
AsyncStream(cast_to=ChatCompletionChunk, response=raw_response, client=client)
if isinstance(client, AsyncOpenAI)
else Stream(cast_to=ChatCompletionChunk, response=raw_response, client=client)
)
if transport == "sdk"
else OpenAIChatCompletionStreamingHandler(
streaming_response=raw_response.aiter_lines() if async_stream else raw_response.iter_lines(),
sync_stream=not async_stream,
)
)
response: Final = CustomStreamWrapper(
completion_stream=stream,
model="test-model",
logging_obj=logging_obj,
custom_llm_provider="openai",
custom_llm_provider="openai" if transport == "sdk" else "hosted_vllm",
)
try: