Skip to content

fix(openai): keep streamed tool calls without an index apart - #2028

Open
pcbeingused333 wants to merge 2 commits into
langfuse:mainfrom
pcbeingused333:fix/openai-stream-unindexed-tool-calls
Open

pcbeingused333 wants to merge 2 commits into
langfuse:mainfrom
pcbeingused333:fix/openai-stream-unindexed-tool-calls

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Oct 20, 2026 •

Copy link
Copy Markdown

With the OpenAI integration, a streamed chat completion whose tool-call deltas have no index is traced as a single tool call. Gemini's OpenAI-compatible API (https://generativelanguage.googleapis.com/v1beta/openai/) streams parallel calls this way: each call arrives whole (id, name and arguments) in its own chunk, without tool_calls[].index.

_extract_streamed_openai_response sends a delta without an index to the latest call, so two parallel get_weather calls end up as one generation output with the second call's id, the first call's name and the arguments concatenated ({"city": "Paris"}{"city": "Tokyo"}), which is not valid JSON.

A delta without an index now starts a new call when it brings a new id, or a name without an id; anything else continues the latest call, so argument fragments of one call still merge, including a fragment that repeats the call's id. Deltas with an index are grouped exactly as before. This is the same rule as in langfuse-js #1040 and langsmith-python #65.

Tests in tests/unit/test_openai.py: two whole calls without index (fails on main, passes here) and one call without index split into fragments (passes before and after). tests/unit/test_openai.py and test_openai_prompt_extraction.py pass (62), and ruff check / ruff format --check are clean. No live API call was made.

This change was made with Claude Code.

RetriggerView in GreptileConfidence Score: 4/5

The PR appears safe to merge, with a non-blocking gap in test coverage.

Summary

Updates streamed OpenAI tool-call grouping for providers that omit index.

  • Unindexed streamed tool calls stay separate when they have different IDs.

Reviews (1) · Last reviewed commit: "fix(openai): keep streamed tool calls wi..." · Reviewed by Greptile

_extract_streamed_openai_response put every tool-call delta without an index into the latest call. Gemini's OpenAI-compatible API streams each parallel call whole in its own chunk and leaves index unset, so two calls were merged into one: the second id overwrote the first, the first name was kept and the arguments were concatenated into invalid JSON.

A delta without an index now starts a new call when it brings a new id, or a name without an id; otherwise it continues the latest one, so argument fragments still merge. Deltas with an index are grouped as before.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

Comment thread tests/unit/test_openai.py
Comment on lines +430 to +445
def test_streaming_chat_completion_keeps_parallel_tool_calls_without_index_apart():
_model, completion, _usage, _metadata, _service_tier = (
lf_openai_module._extract_streamed_openai_response(
SimpleNamespace(type="chat"),
[
_tool_call_chunk(
id="call_paris",
name="get_weather",
arguments='{"city": "Paris"}',
),
_tool_call_chunk(
id="call_tokyo",
name="get_weather",
arguments='{"city": "Tokyo"}',
finish_reason="stop",
),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Name-only calls lack test coverage

Neither new test exercises the rule that a name without an ID starts another call. Both parallel calls have IDs, and the fragment without an ID also has no name. Removing this new branch would therefore leave the tests passing.

Add a test with two calls that omit both index and id, share a function name, and have different arguments. Assert that their arguments stay separate.

Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/unit/test_openai.py
Line: 430-445

Comment:
**Name-only calls lack test coverage**

Neither new test exercises the rule that a name without an ID starts another call. Both parallel calls have IDs, and the fragment without an ID also has no name. Removing this new branch would therefore leave the tests passing.

Add a test with two calls that omit both `index` and `id`, share a function name, and have different arguments. Assert that their arguments stay separate.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

… call

The rule that a delta without an index, id or continuation starts a new call when it carries a function name had no test: removing that branch left the suite green. Two calls with the same function name and different arguments, both without index and id, must stay separate; the test fails without the name branch.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant