Repository navigation
fix(py): keep streamed OpenAI tool calls apart when the endpoint omits index - #6954
pcbeingused33 wants to merge 2 commits into
Conversation
…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.
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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.
| 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
|
Friendly ping: this is ready for review whenever someone has a moment. Happy to adjust anything if needed. |
|
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 |
|
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. |
|
Closing in favour of genkit-ai/genkit-python#145, since the Python SDK moved there. |
With an OpenAI-compatible endpoint that does not send
indexon streamed tool call deltas (Gemini's OpenAI-compatible API does this), thegenkit-openaiplugin merged all parallel tool calls into a single request with their arguments concatenated._generate_streamaccumulated fragments in a dict keyed bytool_call.index, so every fragment withindex=Nonelanded in the same slot: two calls such asget_weather({"city": "Paris"})andget_time({"tz": "UTC"})came out as one call namedget_weatherwith unparseable arguments.Fix: a small helper,
_tool_call_key, picks the slot for each fragment. A fragment with anindexuses it, as before. Without one, a fragment with a newid(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 sendindexbehave exactly as before.Tests (
py/packages/genkit-openai/tests/openai_model_test.py): two parallel calls withoutindexyield two tool requests with the right name, ref and arguments (fails before the change); a repeated id withoutindexstays one call.pytest tests/openai_model_test.py: 121 passed. Three tests in that file (test__generate_reports_extra_token_countsand the twotest_generate_classifies_a_stream_cut_mid_readcases) fail onmainas well, with and without this change; they are unrelated to tool calls.ruff checkandruff format --checkare clean.The JS
compat-oaiplugin 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).