Skip to content

Commit aeb8e6b

Browse files
committed
fix(openai): record single nonzero choice index instead of dropping it
An n>1 stream that only ever yields output for one nonzero choice index, for example after partial consumption, was read back from index 0 and recorded as None. Fall back to the single collected choice regardless of its index.
1 parent d54d9ba commit aeb8e6b

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

‎langfuse/openai.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -918,15 +918,17 @@ def get_response_for_chat(completion: Any) -> Any:
918918
get_response_for_chat(chat_completions[index])
919919
for index in sorted(chat_completions)
920920
]
921+
elif chat_completions:
922+
response = get_response_for_chat(next(iter(chat_completions.values())))
921923
else:
922-
response = get_response_for_chat(
923-
chat_completions.get(0, defaultdict(lambda: None))
924-
)
924+
response = get_response_for_chat(defaultdict(lambda: None))
925925
else:
926926
if len(completion_texts) > 1:
927927
response = [completion_texts[index] for index in sorted(completion_texts)]
928+
elif completion_texts:
929+
response = next(iter(completion_texts.values()))
928930
else:
929-
response = completion_texts.get(0, "")
931+
response = ""
930932

931933
return (
932934
model,

‎tests/unit/test_openai.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,41 @@ def test_streaming_chat_completion_single_choice_output_unchanged():
501501
assert metadata == {"finish_reason": "stop"}
502502

503503

504+
def test_streaming_chat_completion_single_nonzero_choice_index_not_lost():
505+
# An n>1 stream that only ever yields output for choice 1 (for example
506+
# after partial consumption) must still record that choice's response
507+
# instead of reading index 0 and recording None.
508+
chunks = _make_chat_stream_chunks_two_choices()
509+
only_choice_one = [
510+
SimpleNamespace(
511+
model="gpt-4o-mini",
512+
choices=[choice for choice in chunk.choices if choice.index == 1],
513+
usage=chunk.usage,
514+
)
515+
for chunk in chunks
516+
]
517+
518+
model, completion, _usage, _metadata, _service_tier = (
519+
lf_openai_module._extract_streamed_openai_response(
520+
SimpleNamespace(type="chat"),
521+
only_choice_one,
522+
)
523+
)
524+
525+
assert model == "gpt-4o-mini"
526+
assert completion == {
527+
"role": "assistant",
528+
"content": "B1",
529+
"tool_calls": [
530+
{
531+
"id": "call_1",
532+
"type": "function",
533+
"function": {"name": "tool_1", "arguments": '{"y":1}'},
534+
}
535+
],
536+
}
537+
538+
504539
def test_response_api_output_serializes_openai_parsed_response_objects():
505540
class ParsedOutput(BaseModel):
506541
name: str

0 commit comments

Comments
 (0)