fix: send reasoning content as reasoning field in assistant message history#451
Merged
threepointone merged 3 commits intocloudflare:mainfrom Mar 21, 2026
Merged
Conversation
🦋 Changeset detectedLatest commit: b12704b 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: |
Apply formatting and minor code style changes across the repo: reindent demos.json, rewrap README examples, normalize imports/whitespace in TS source and tests, and tidy up changelog formatting. These are non-functional readability changes (no behavioural logic changes), including small console.log and array formatting adjustments, type assertion wrapping, and consistent ternary formatting.
d84e5c1 to
f0ebbfc
Compare
Refresh demos' package-lock.json files and update demos.json package_json_hash entries. Bump multiple dependencies and metadata across demo locks (notable bumps: @ai-sdk/gateway 3.0.66→3.0.77, @ai-sdk/provider-utils 4.0.19→4.0.21, @cloudflare/unenv-preset 2.15.0→2.16.0, @cloudflare/vite-plugin 1.29.1→1.30.0, rollup platform packages 4.59.0→4.59.1, ai 6.0.116→6.0.134, @emnapi/runtime 1.9.0→1.9.1) and small lockfile metadata tweaks. These updates ensure demo dependencies and recorded hashes are in sync with the updated locks.
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
convert-to-workersai-chat-messages.tswherereasoningparts in assistant messages were concatenated into thecontentstring instead of being sent as a separate fieldcontent, models like@cf/moonshotai/kimi-k2.5and@cf/zai-org/glm-4.7-flashreceive their own internal reasoning as if it were spoken text, corrupting the conversation history and causing empty text responses or leaked special tokens on subsequent turnsreasoningfield on the assistant message object — the field name Chat Completions spec expects on input for reasoning modelsRoot cause
In multi-turn agentic conversations (e.g. using
generateTextwith tools), the ai-sdk stores reasoning content asreasoningparts on assistant messages. When building the message history for the next turn, the provider was doing:This sends hundreds of tokens of internal model reasoning back to the model as if it were the assistant's spoken reply, which confuses the chat template and causes the model to send back content in reasoning.
Fix
The
reasoningfield name is what Chat Completions specCustomChatCompletionMessageParamdefines for input —reasoning_contentexists onConversationMessagebut is deprecated in favour ofreasoning.Testing
Updated existing unit tests and added 4 new cases in
convert-to-workersai-chat-messages.test.tscovering:reasoningfield, not concatenated into contentreasoningfield omitted when absentFull suite: 213 tests passing.