diff --git a/litellm/llms/openai/chat/gpt_transformation.py b/litellm/llms/openai/chat/gpt_transformation.py index 9a3e564f153..2dd00d20f30 100644 --- a/litellm/llms/openai/chat/gpt_transformation.py +++ b/litellm/llms/openai/chat/gpt_transformation.py @@ -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, diff --git a/tests/unit/litellm_core_utils/test_streaming_handler.py b/tests/unit/litellm_core_utils/test_streaming_handler.py index 858f1d2418f..a1b58521d7e 100644 --- a/tests/unit/litellm_core_utils/test_streaming_handler.py +++ b/tests/unit/litellm_core_utils/test_streaming_handler.py @@ -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: