Skip to content

fix(py): keep streamed OpenAI tool calls apart when the endpoint omits index - #6954

Closed
pcbeingused33 wants to merge 2 commits into
genkit-ai:mainfrom
pcbeingused33:fix/openai-stream-tool-calls-no-index
Closed

pcbeingused33 wants to merge 2 commits into
genkit-ai:mainfrom
pcbeingused33:fix/openai-stream-tool-calls-no-index

Conversation

@pcbeingused33

Copy link
Copy Markdown

With an OpenAI-compatible endpoint that does not send index on streamed tool call deltas (Gemini's OpenAI-compatible API does this), the genkit-openai plugin merged all parallel tool calls into a single request with their arguments concatenated. _generate_stream accumulated fragments in a dict keyed by tool_call.index, so every fragment with index=None landed in the same slot: two calls such as get_weather({"city": "Paris"}) and get_time({"tz": "UTC"}) came out as one call named get_weather with unparseable arguments.

Fix: a small helper, _tool_call_key, picks the slot for each fragment. A fragment with an index uses it, as before. Without one, a fragment with a new id (or, lacking an id, a function name) starts a new call, and any other fragment continues the latest call; a fragment repeating the current call's id continues it. Streams that send index behave exactly as before.

Tests (py/packages/genkit-openai/tests/openai_model_test.py): two parallel calls without index yield two tool requests with the right name, ref and arguments (fails before the change); a repeated id without index stays one call. pytest tests/openai_model_test.py: 121 passed. Three tests in that file (test__generate_reports_extra_token_counts and the two test_generate_classifies_a_stream_cut_mid_read cases) fail on main as well, with and without this change; they are unrelated to tool calls. ruff check and ruff format --check are clean.

The JS compat-oai plugin has the same shape (index ?? 0); it is left out to keep this PR to one language.

This change was prepared with the help of an AI agent (Claude).

…s index

The streaming accumulator keyed tool call fragments by tool_call.index, so an
OpenAI-compatible endpoint that sends no index (Gemini) collapsed every parallel call
into one slot with its arguments glued together. Resolve a slot for index-less
fragments: a new id (or function name) starts a call, other fragments continue the
latest one.

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request introduces a helper function _tool_call_key to support streaming tool calls from OpenAI-compatible endpoints (such as Gemini) that omit the index field, along with corresponding unit tests. A review comment correctly identifies a bug in _tool_call_key where interleaved parallel tool calls with repeated IDs could be misrouted, and provides a code suggestion to search across all accumulated tool calls to resolve the issue.

Comment on lines +195 to +199
if tool_call.id:
starts_new_call = tool_call.id != accumulated[latest].id
else:
starts_new_call = bool(tool_call.function and tool_call.function.name)
return latest + 1 if starts_new_call else latest

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.

high

If parallel tool calls are interleaved or sent in the same chunk, and the provider repeats the id on subsequent chunks, comparing only against accumulated[latest].id will fail. For example, if we have two active tool calls (index 0 for call_a and index 1 for call_b), a subsequent chunk for call_a will have latest = 1 (which is call_b). Comparing call_a to call_b will result in starts_new_call = True, incorrectly starting a third tool call (index 2) instead of continuing index 0.

By looking up the id across all accumulated tool calls, we can correctly route the fragment to the existing slot regardless of the order or interleaving of chunks.

Suggested change
if tool_call.id:
starts_new_call = tool_call.id != accumulated[latest].id
else:
starts_new_call = bool(tool_call.function and tool_call.function.name)
return latest + 1 if starts_new_call else latest
if tool_call.id:
for k, v in accumulated.items():
if getattr(v, 'id', None) == tool_call.id:
return k
starts_new_call = True
else:
starts_new_call = bool(tool_call.function and tool_call.function.name)
return latest + 1 if starts_new_call else latest

# Conflicts:
#	py/packages/genkit-openai/tests/openai_model_test.py
@pcbeingused33

Copy link
Copy Markdown
Author

Friendly ping: this is ready for review whenever someone has a moment. Happy to adjust anything if needed.

@MichaelDoyle

Copy link
Copy Markdown
Contributor

Hi @pcbeingused333 — heads up that Genkit Python SDK development has moved to https://github.com/genkit-ai/genkit-python! Could you re-open this PR over there (file paths map 1:1 after stripping the leading py/ prefix), and link/close this PR once moved? cc @huangjeff5

@pcbeingused33

Copy link
Copy Markdown
Author

Moved to genkit-ai/genkit-python#145 as requested. The change is the same, adapted to the current main.

This comment was drafted with Claude Code.

@pcbeingused33

Copy link
Copy Markdown
Author

Closing in favour of genkit-ai/genkit-python#145, since the Python SDK moved there.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants