fix: three tool calling bugs causing multi-turn agentic loops to fail#454
Merged
threepointone merged 2 commits intocloudflare:mainfrom Mar 22, 2026
Merged
Conversation
…ption false in tool prep
🦋 Changeset detectedLatest commit: cff5c89 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
Improve handling of tool-result outputs by unwrapping different output types (text, error-text, json, error-json, execution-denied, and content parts) and returning appropriate plain strings instead of serializing wrapper objects. Add unit tests covering error-text/error-json/execution-denied/content cases and adjust existing expectations. Extend E2E fixtures and tests to exercise multi-step agentic tool loops and toolChoice="required" behavior (including new routes in the binding worker, summary table updates, and per-model checks). Also update a test description in text-generation.test and remove a couple of models from E2E model lists.
threepointone
approved these changes
Mar 22, 2026
Collaborator
threepointone
left a comment
There was a problem hiding this comment.
added some more checks and updated e2e tests, looking good to me now
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes three bugs in
workers-ai-providerthat collectively caused multi-turn tool calling to fail for new models (@cf/moonshotai/kimi-k2.5,@cf/meta/llama-4-scout-17b-16e-instruct,@cf/zai-org/glm-4.7-flash).Fixes
1. Tool result output not unwrapped (primary cause of broken agentic loops)
convert-to-workersai-chat-messages.tswas callingJSON.stringify(toolResponse.output)on the fullLanguageModelV3ToolResultOutputwrapper ({ type: 'text', value: '...' }), sending the wrapper object as the tool message content instead of just the value. Models received garbled tool results and stopped after the first tool call instead of continuing the loop.Fix: exhaustive switch over all 7
LanguageModelV3ToolResultOutputvariants:2.
toolChoice: "required"mapped to"any"instead of"required"All models returned
8001: Invalid inputfortool_choice: "any". Anyone usingtoolChoice: "required"ortoolChoice: { type: "tool", toolName: "..." }got a hard error.3.
description: falseandparameters: falsein tool definitions&&short-circuit evaluated tofalse(notundefined) for non-function tool types, which would cause8001: Invalid inputif the ai-sdk ever sends a non-function tool type.Testing
Unit tests
222 unit tests passing, including new tests covering:
LanguageModelV3ToolResultOutputvariants (text,json,error-text,error-json,execution-denied,content, unknown)toolChoice: "required"mappingE2E tests
E2E tests against real models via both the Workers AI binding and REST API, including two new test scenarios:
toolChoice: "required"(T-Rq): Validates thetool_choicemapping fix — verifies no API error and at least one tool call is made.Binding E2E results:
T-MS = multi-step agentic loop, T-Rq = toolChoice required, ~ = partial (model behavior)
QwQ 32B's T-Rq failure is a server-side model limitation, not a provider bug.