Skip to content

Handle multiple tool calls in ChatSession#162

Merged
davidkoski merged 1 commit intoml-explore:mainfrom
alankessler:fix-multiple-tool-calls
Mar 27, 2026
Merged

Handle multiple tool calls in ChatSession#162
davidkoski merged 1 commit intoml-explore:mainfrom
alankessler:fix-multiple-tool-calls

Conversation

@alankessler
Copy link
Copy Markdown
Contributor

Fixes #134.

The generation loop was breaking on the first tool call, canceling the stream, and losing the rest. Now it drains the stream, collects all tool calls, dispatches them, and restarts with all tool results. Works for both mid-stream (JSON/XML tagged) and EOS-emitted (Mistral) tool calls.

Skips dispatch if the consumer has canceled. No change to the streamDetails path — callers without toolDispatch still get tool call items directly.

The generation loop was breaking on the first tool call, canceling the
stream, and losing the rest. Now it drains the stream, collects all
tool calls, dispatches them, and restarts with all tool results.

Skips dispatch if the consumer has canceled (Task.isCancelled).
No change to the streamDetails path.

Fixes ml-explore#134.
Copy link
Copy Markdown
Collaborator

@davidkoski davidkoski left a comment

Choose a reason for hiding this comment

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

Nice and simple, thank you!

@davidkoski davidkoski merged commit 2a296f1 into ml-explore:main Mar 27, 2026
2 checks passed
TheTom pushed a commit to TheTom/mlx-swift-lm that referenced this pull request Apr 5, 2026
The generation loop was breaking on the first tool call, canceling the
stream, and losing the rest. Now it drains the stream, collects all
tool calls, dispatches them, and restarts with all tool results.

Skips dispatch if the consumer has canceled (Task.isCancelled).
No change to the streamDetails path.

Fixes ml-explore#134.
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.

[BUG] ChatSession should handle multiple tool calls

2 participants