Skip to content

Conversation

@stephaniepang97
Copy link

Problem

When compaction is triggered while the last assistant message contains server_tool_use blocks (for server-side tools like web_fetch or web_search), the compaction API call fails with:

messages.X: `web_fetch` tool use with id `srvtoolu_XXX` was found without a corresponding `web_fetch_tool_result` block

Root Cause

The compaction code filters out tool_use blocks from the last assistant message before making the compaction request (since they require corresponding tool_result blocks). However, it wasn't filtering out server_tool_use blocks, which also require corresponding result blocks (web_fetch_tool_result, web_search_tool_result, etc.).

Solution

Changed the filter from:

block.get("type") != "tool_use"

to:

block.get("type") not in ("tool_use", "server_tool_use")

Changes

  • Updated BaseSyncToolRunner._check_and_compact() to filter out both tool_use and server_tool_use
  • Updated BaseAsyncToolRunner._check_and_compact() with the same fix

…tion

When compaction is triggered, the code filters out tool_use blocks from
the last assistant message to avoid 400 errors (since tool_use requires
a corresponding tool_result). However, it wasn't filtering out
server_tool_use blocks, which also require corresponding result blocks
(web_fetch_tool_result, web_search_tool_result, etc.).

This caused compaction to fail with:
'messages.X: `web_fetch` tool use with id `srvtoolu_XXX` was found
without a corresponding `web_fetch_tool_result` block'

Fixed by changing the filter from:
  block.get("type") != "tool_use"
to:
  block.get("type") not in ("tool_use", "server_tool_use")
@stephaniepang97 stephaniepang97 requested a review from a team as a code owner January 24, 2026 16:26
When compaction triggers, the current assistant message (which contains
server tool results like web_fetch_tool_result) hasn't been appended to
_params['messages'] yet. This causes compaction to fail because:

1. Message N has server_tool_use for some ID
2. Message N+1 (current, not yet appended) has the corresponding result
3. Compaction only sees messages up to N, missing the result
4. API rejects with 'tool use without corresponding result block'

Fix: Pass the current message to _check_and_compact() so it can include
the message (with server tool results) when building messages for the
compaction API call.
When including the current message in compaction, the content blocks
are pydantic objects. The filter logic checks isinstance(block, dict),
which fails for pydantic objects, causing ALL blocks to be filtered out
(including the server tool results we want to keep).

Fix: Convert content blocks to dicts using model_dump() when appending
the current message, so the existing dict-based filter logic works.
@benkomalo benkomalo requested a review from kevinc13 January 25, 2026 16:47
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