Skip to content

fix: use filtered messages in async _check_and_compact()#1214

Closed
thakoreh wants to merge 1 commit intoanthropics:mainfrom
thakoreh:fix-check-and-compact-tool-use-filtering
Closed

fix: use filtered messages in async _check_and_compact()#1214
thakoreh wants to merge 1 commit intoanthropics:mainfrom
thakoreh:fix-check-and-compact-tool-use-filtering

Conversation

@thakoreh
Copy link

@thakoreh thakoreh commented Mar 1, 2026

Summary

Fixes #1205

The async _check_and_compact() method was using self._params['messages'] instead of the filtered local messages variable when building the summarization request. This caused tool_use blocks to be sent to the API without corresponding tool_result blocks, resulting in BadRequestError.

Root Cause

In src/anthropic/lib/tools/_beta_runner.py, line 478:

# Before (BUGGY)
messages = [
    *self._params["messages"],  # <-- Uses unfiltered messages
    BetaMessageParam(...)
]

The sync version of this method (line 218) correctly uses *messages (the filtered variable), but the async version was using the unfiltered self._params["messages"].

Fix

Changed line 478 to use the filtered messages variable:

# After (FIXED)
messages = [
    *messages,  # <-- Uses filtered messages (tool_use blocks removed)
    BetaMessageParam(...)
]

Testing

  • The fix aligns the async implementation with the sync implementation
  • The filtering logic (lines 459-476) now has its effect preserved when building the summarization request

The async _check_and_compact() method was using self._params['messages']
instead of the filtered local 'messages' variable when building the
summarization request. This caused tool_use blocks to be sent to the API
without corresponding tool_result blocks, resulting in BadRequestError.

Fixes anthropics#1205
@thakoreh thakoreh requested a review from a team as a code owner March 1, 2026 01:23
@karpetrosyan
Copy link
Collaborator

#1205 was already fixed

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: async _check_and_compact() ignores tool_use filtering, causes BadRequestError

2 participants