Skip to content

fix(openai): avoid corrupting parallel streamed tool calls with repeated index - #758

Open
pcbeingused33 wants to merge 1 commit into
activeagents:mainfrom
pcbeingused33:fix/openai-chat-streamed-tool-calls-index
Open

pcbeingused33 wants to merge 1 commit into
activeagents:mainfrom
pcbeingused33:fix/openai-chat-streamed-tool-calls-index

Conversation

@pcbeingused33

Copy link
Copy Markdown
Contributor

Root Cause

ActiveAgent::Providers::OpenAI::ChatProvider#hash_merge_delta (inherited by OllamaProvider, OpenRouterProvider and GeminiProvider) matches a streamed tool_calls delta to an existing call purely by index. Ollama's OpenAI-compatible endpoint (/v1/chat/completions) streams parallel tool calls as separate chunks that all carry index: 0 with distinct ids, so the second call is merged into the first: the ids become "call_1call_2", the function names "get_weatherget_time" and the arguments {"city":"NYC"}{"tz":"EST"}, and the second call is lost. The same path also concatenates id and function.name when a server repeats them on indexed continuation chunks.

Fix

  • tool_call_target (renamed from unindexed_tool_call_target) now resolves every streamed tool_calls delta, indexed or not:
    • a delta with an id continues the call with that id, or starts a new call when the id is unseen (even if its index collides);
    • otherwise a delta with an index continues the most recent call at that index;
    • otherwise a delta without a function name continues the last call.
  • tool_call_fragment (renamed from unindexed_tool_call_fragment) is applied to every continuation, so repeated id, type and function.name are dropped instead of concatenated.

Standard OpenAI streams (id on the first chunk, then index-only argument fragments) follow the same path as before.

Tests

New cases in test/providers/open_ai/chat/streaming_tool_calls_test.rb (they fail on main):

  • parallel calls repeating index: 0 with distinct ids stay separate;
  • argument fragments after a repeated index: 0 continue the latest call at that index;
  • indexed chunks repeating id, type and function.name are not concatenated.

Drafted with AI assistance and verified against the local test suite.

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