Skip to content

Commit 2dd9a1f

Browse files
committed
fix(openai): handle None arguments in legacy function_call streaming merge
1 parent 465316d commit 2dd9a1f

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

‎langfuse/openai.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,9 @@ def _extract_streamed_openai_response(resource: Any, chunks: Any) -> Any:
811811
curr["name"] = curr["name"] or getattr(
812812
tool_call_chunk, "name", None
813813
)
814-
curr["arguments"] += getattr(tool_call_chunk, "arguments", "")
814+
curr["arguments"] = (curr.get("arguments") or "") + (
815+
getattr(tool_call_chunk, "arguments", None) or ""
816+
)
815817

816818
if (
817819
delta.get("tool_calls", None) is not None

‎tests/unit/test_openai.py‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,75 @@ def test_streaming_chat_completion_preserves_tool_calls_after_content():
401401
assert metadata == {"finish_reason": "tool_calls"}
402402

403403

404+
def _make_chat_stream_chunks_with_legacy_function_call_none_arguments():
405+
"""Mirrors a real OpenAI-compatible-proxy payload shape (see PR #1339,
406+
which fixed the identical crash for the newer `tool_calls` array but
407+
left this deprecated `function_call` merge branch unpatched): the first
408+
streamed delta carries a `function_call` with `arguments=None`, and a
409+
later delta appends string arguments to it.
410+
"""
411+
return [
412+
SimpleNamespace(
413+
model="gpt-4o-mini",
414+
choices=[
415+
SimpleNamespace(
416+
delta=SimpleNamespace(
417+
role="assistant",
418+
content=None,
419+
function_call=SimpleNamespace(
420+
name="get_weather", arguments=None
421+
),
422+
tool_calls=None,
423+
),
424+
finish_reason=None,
425+
)
426+
],
427+
usage=None,
428+
),
429+
SimpleNamespace(
430+
model="gpt-4o-mini",
431+
choices=[
432+
SimpleNamespace(
433+
delta=SimpleNamespace(
434+
role=None,
435+
content=None,
436+
function_call=SimpleNamespace(
437+
name=None, arguments='{"city": "Berlin"}'
438+
),
439+
tool_calls=None,
440+
),
441+
finish_reason="function_call",
442+
)
443+
],
444+
usage=None,
445+
),
446+
]
447+
448+
449+
def test_streaming_chat_completion_handles_legacy_function_call_none_arguments():
450+
"""Regression test: the deprecated `function_call` streaming-merge branch
451+
must not crash when the first delta's `arguments` is None (a real
452+
OpenAI-compatible-proxy behaviour, analogous to the `tool_calls` bug
453+
fixed in PR #1339 but never patched for this legacy branch).
454+
"""
455+
model, completion, usage, metadata = (
456+
lf_openai_module._extract_streamed_openai_response(
457+
SimpleNamespace(type="chat"),
458+
_make_chat_stream_chunks_with_legacy_function_call_none_arguments(),
459+
)
460+
)
461+
462+
assert model == "gpt-4o-mini"
463+
assert completion == {
464+
"role": "assistant",
465+
"function_call": {
466+
"name": "get_weather",
467+
"arguments": '{"city": "Berlin"}',
468+
},
469+
}
470+
assert metadata == {"finish_reason": "function_call"}
471+
472+
404473
def test_response_api_output_serializes_openai_parsed_response_objects():
405474
class ParsedOutput(BaseModel):
406475
name: str

0 commit comments

Comments
 (0)