[1.x] Keep every tool call of a streamed Ollama reply

Merged
EZ
7d ago
+88-130

When a model served by Ollama calls several tools in one reply, a streamed run only executes the last call, with the arguments of the others merged into it. Blocking runs are not affected.

The problem

For some models, Ollama streams each tool call in a chunk of its own and identifies it via function.index. The call is then always the only entry of that chunk's tool_calls list. This is a reply captured from Ollama 0.40.14 with qwen3.5 (trimmed to the relevant fields):

{"message":{"role":"assistant","content":"","tool_calls":[{"id":"call_cqxmab7u","function":{"index":0,"name":"get_weather","arguments":{"city":"Sanaa"}}}]},"done":false}
{"message":{"role":"assistant","content":"","tool_calls":[{"id":"call_f67xe4r7","function":{"index":1,"name":"get_weather","arguments":{"city":"Aden"}}}]},"done":false}
{"message":{"role":"assistant","content":"","tool_calls":[{"id":"call_jrrdtoch","function":{"index":2,"name":"get_weather","arguments":{"city":"Taiz"}}}]},"done":false}
{"message":{"role":"assistant","content":""},"done":true,"done_reason":"stop"}

The Ollama gateway keys the pending tool calls by their position in the chunk's list. That position is 0 for every call above, so each call overwrites the previous one: the id and name are replaced and the arguments are merged via array_replace.

With this stream the agent invokes get_weather once (Taiz), and a single tool result is sent back to the model. No exception is thrown. When the calls target different tools, the last tool is invoked with the merged arguments of all of them.

The fix

A streamed tool call is now keyed by its function.index when the chunk provides one, falling back to its position in the list otherwise. This matches what the other gateways already do with the index their provider sends ($tcDelta['index'], output_index).

The shapes that already worked are unchanged:

  • Several calls sent together in one chunk, numbered or not. Other models on the same Ollama version send their calls this way.
  • A single call whose fields arrive in pieces without an index, which is covered by the existing streaming accumulates tool call arguments across chunks test.

For reference, Ollama's guide (Tool calling with streaming) treats the tool_calls of every chunk as complete calls and appends them to a list.

Tests

  • streaming keeps every tool call when each arrives in its own chunk fails on 1.x and passes with this change.
  • streaming keeps tool calls sent together in one chunk guards the shape that already worked.

The Ollama feature tests pass (81 tests), and Pint and PHPStan are clean for the touched files.