From f601727b0e0a39ee452c04c8e345ba6e3ba4c2ed Mon Sep 17 00:00:00 2001 From: Hassieb Pakzad <68423100+hassiebp@users.noreply.github.com> Date: Wed, 8 Jul 2026 16:58:55 +0200 Subject: [PATCH] fix(openai): handle null choices in chat completion responses Some OpenAI-compatible providers and gateways return "choices": null in the response body. Since dict.get() only falls back to its default for missing keys (not explicit None values), the wrapper crashed with "TypeError: object of type 'NoneType' has no len()" and failed the user's call even though the underlying API request succeeded. Guard the choices extraction in both the non-streaming and streaming paths (and the analogous embedding data extraction) with `or []`. Fixes LFE-10734 Co-Authored-By: Claude Fable 5 --- langfuse/openai.py | 8 ++--- tests/unit/test_openai.py | 63 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/langfuse/openai.py b/langfuse/openai.py index 97bb0f89e..d78ed2850 100644 --- a/langfuse/openai.py +++ b/langfuse/openai.py @@ -755,7 +755,7 @@ def _extract_streamed_openai_response(resource: Any, chunks: Any) -> Any: if chunk_usage is not None: usage = chunk_usage - choices = chunk.get("choices", []) + choices = chunk.get("choices") or [] for choice in choices: if _is_openai_v1(): @@ -898,7 +898,7 @@ def _get_langfuse_data_from_default_response( completion = None if resource.type == "completion": - choices = response.get("choices", []) + choices = response.get("choices") or [] if len(choices) > 0: choice = choices[-1] @@ -908,7 +908,7 @@ def _get_langfuse_data_from_default_response( completion = _extract_response_api_completion(response.get("output", {})) elif resource.type == "chat": - choices = response.get("choices", []) + choices = response.get("choices") or [] if len(choices) > 0: # If multiple choices were generated, we'll show all of them in the UI as a list. if len(choices) > 1: @@ -927,7 +927,7 @@ def _get_langfuse_data_from_default_response( ) elif resource.type == "embedding": - data = response.get("data", []) + data = response.get("data") or [] if len(data) > 0: first_embedding = data[0] embedding_vector = ( diff --git a/tests/unit/test_openai.py b/tests/unit/test_openai.py index c06ce9df2..78195e92e 100644 --- a/tests/unit/test_openai.py +++ b/tests/unit/test_openai.py @@ -311,6 +311,69 @@ def test_chat_completion_exports_generation_span( } +def test_chat_completion_with_none_choices_does_not_crash( + langfuse_memory_client, get_span, json_attr +): + openai_client = lf_openai.OpenAI(api_key="test") + response = SimpleNamespace( + model="gpt-4o-mini", + choices=None, + usage=SimpleNamespace(prompt_tokens=3, completion_tokens=1, total_tokens=4), + ) + + with patch.object(openai_client.chat.completions, "_post", return_value=response): + result = openai_client.chat.completions.create( + name="unit-openai-chat-none-choices", + model="gpt-4o-mini", + messages=[{"role": "user", "content": "1 + 1 = ?"}], + ) + + assert result is response + + langfuse_memory_client.flush() + span = get_span("unit-openai-chat-none-choices") + + assert LangfuseOtelSpanAttributes.OBSERVATION_LEVEL not in span.attributes + assert ( + span.attributes[LangfuseOtelSpanAttributes.OBSERVATION_MODEL] == "gpt-4o-mini" + ) + assert json_attr(span, LangfuseOtelSpanAttributes.OBSERVATION_USAGE_DETAILS) == { + "prompt_tokens": 3, + "completion_tokens": 1, + "total_tokens": 4, + } + + +def test_openai_stream_with_none_choices_chunk_does_not_crash( + langfuse_memory_client, get_span +): + openai_client = lf_openai.OpenAI(api_key="test") + chunks_with_none_choices = [ + SimpleNamespace(model="gpt-4o-mini", choices=None, usage=None), + *_make_chat_stream_chunks(), + ] + raw_stream = DummyOpenAIStream(chunks_with_none_choices, DummySyncResponse()) + + with patch.object(openai_client.chat.completions, "_post", return_value=raw_stream): + stream = openai_client.chat.completions.create( + name="unit-openai-stream-none-choices", + model="gpt-4o-mini", + messages=[{"role": "user", "content": "1 + 1 = ?"}], + stream=True, + ) + + chunks = list(stream) + stream.close() + + assert len(chunks) == 3 + + langfuse_memory_client.flush() + span = get_span("unit-openai-stream-none-choices") + + assert span.attributes[LangfuseOtelSpanAttributes.OBSERVATION_OUTPUT] == "2" + assert span.attributes["langfuse.observation.metadata.finish_reason"] == "stop" + + def test_streaming_chat_completion_preserves_tool_calls_after_content(): model, completion, usage, metadata = ( lf_openai_module._extract_streamed_openai_response(