Skip to content

Conversation

@bigboateng
Copy link
Contributor

@bigboateng bigboateng commented Dec 15, 2025

Important

Improves message history management for tool use and thinking blocks to prevent errors and ensure proper conversation flow.

  • Behavior:
    • Updated examples/test-extended-thinking.ts to include multiple tool uses in the task description and improved error handling for tool use and thinking block issues.
  • Functions:
    • cleanMessageHistory() in utils/message-processing.ts now ensures tool_result blocks have corresponding tool_use in the immediately previous message and orders assistant message blocks correctly.
    • ensureThinkingBlocksForExtendedThinking() in utils/message-processing.ts removes assistant messages without initial thinking blocks and the following user message to maintain flow.
  • Misc:
    • Improved logging in examples/test-extended-thinking.ts for better error diagnosis.

This description was created by Ellipsis for 8d9146b. You can customize this summary. It will automatically update as commits are pushed.

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.
@ellipsis-dev ellipsis-dev bot changed the title ... fix: improve message history management for tool use and thinking blocks Dec 15, 2025
@bigboateng bigboateng merged commit a54f4c8 into main Dec 15, 2025
1 check passed
@bigboateng bigboateng deleted the fix/extended-thinking-400-error branch December 15, 2025 18:11
Copy link

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed everything up to 8d9146b in 1 minute and 16 seconds. Click for details.
  • Reviewed 202 lines of code in 2 files
  • Skipped 0 files when reviewing.
  • Skipped posting 5 draft 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% <= threshold 50% 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% <= threshold 50% 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% <= threshold 50% 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% <= threshold 50% 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% <= threshold 50% None

Workflow ID: wflow_FWvHKoNt4dz9r8ig

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

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.

2 participants