fix: improve message history management for tool use and thinking blocks#39
Merged
bigboateng merged 1 commit intomainfrom Dec 15, 2025
Merged
Conversation
This commit adds two critical fixes for message history management: 1. **Tool_use/tool_result pairing fix**: The API requires that each tool_result must have its corresponding tool_use in the IMMEDIATELY PREVIOUS message, not just anywhere in history. Updated cleanMessageHistory() to check pairing on a per-message basis. 2. **Thinking block cleanup enhancement**: When filtering out assistant messages without thinking blocks, also remove the following user message to maintain proper conversation flow (user -> assistant -> user -> assistant pattern). 3. **Enhanced test coverage**: Updated test-extended-thinking.ts to include multiple tool uses, better exercising message history management. These fixes prevent 400 errors: - "unexpected tool_use_id found in tool_result blocks" - "Expected thinking or redacted_thinking, but found text" Tested with extended thinking example - no errors with multiple tool uses.
There was a problem hiding this comment.
Important
Looks good to me! 👍
Reviewed everything up to 8d9146b in 1 minute and 16 seconds. Click for details.
- Reviewed
202lines of code in2files - Skipped
0files when reviewing. - Skipped posting
5draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. examples/test-extended-thinking.ts:44
- Draft comment:
Updated log message now clarifies that the test uses multiple tool uses. Also, check that the template literal’s indentation doesn’t introduce unintended whitespace in the prompt. - Reason this comment was not posted:
Confidence changes required:33%<= threshold50%None
2. examples/test-extended-thinking.ts:77
- Draft comment:
Additional error handling for tool_use/tool_result pairing is a useful diagnostic addition. - Reason this comment was not posted:
Confidence changes required:0%<= threshold50%None
3. utils/message-processing.ts:185
- Draft comment:
The revised cleanMessageHistory function now strictly associates each tool_result with a tool_use in the immediately preceding assistant message, aligning with the API requirements. - Reason this comment was not posted:
Confidence changes required:0%<= threshold50%None
4. utils/message-processing.ts:221
- Draft comment:
Assistant message reordering now omits tool_result blocks—which matches the API spec. Confirm that excluding tool_result blocks in assistant messages is intended. - Reason this comment was not posted:
Confidence changes required:33%<= threshold50%None
5. utils/message-processing.ts:263
- Draft comment:
The updated ensureThinkingBlocksForExtendedThinking function now removes the following user message when an assistant message lacks a thinking block, preserving conversation flow. Consider deduplicating indices in the removal loop to avoid potential double removals. - Reason this comment was not posted:
Confidence changes required:33%<= threshold50%None
Workflow ID: wflow_FWvHKoNt4dz9r8ig
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
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.
Important
Improves message history management for tool use and thinking blocks to prevent errors and ensure proper conversation flow.
examples/test-extended-thinking.tsto include multiple tool uses in the task description and improved error handling for tool use and thinking block issues.cleanMessageHistory()inutils/message-processing.tsnow ensures tool_result blocks have corresponding tool_use in the immediately previous message and orders assistant message blocks correctly.ensureThinkingBlocksForExtendedThinking()inutils/message-processing.tsremoves assistant messages without initial thinking blocks and the following user message to maintain flow.examples/test-extended-thinking.tsfor better error diagnosis.This description was created by
for 8d9146b. You can customize this summary. It will automatically update as commits are pushed.