fix: ensure tool_result messages have matching tool_use after token trimming (#2450) by jpr5 · Pull Request #3820 · CopilotKit/CopilotKit · GitHub
Skip to content

fix: ensure tool_result messages have matching tool_use after token trimming (#2450) - #3820

Merged
AlemTuzlak merged 1 commit into
mainfrom
fix/token-trimming-tool-pairing-2450
Apr 16, 2026
Merged

AlemTuzlak merged 1 commit into
mainfrom
fix/token-trimming-tool-pairing-2450

Conversation

@jpr5

@jpr5 jpr5 commented Apr 12, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #2450

Token trimming in the Anthropic adapter could remove assistant messages containing tool_use blocks while keeping the corresponding user messages with tool_result blocks. Anthropic's API rejects orphaned tool_result messages. This adds a post-processing step that removes orphaned tool_result blocks after trimming.

Test plan

  • Verify token trimming preserves tool_use/tool_result pairing
  • Verify orphaned tool_result blocks are removed
  • Verify messages with mixed content (tool_result + text) retain non-orphaned content

@changeset-bot

changeset-bot Bot commented Apr 12, 2026

Copy link
Copy Markdown

@vercel

vercel Bot commented Apr 12, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
chat-with-your-data Ready Ready Preview, Comment Apr 14, 2026 11:27pm
docs Ready Ready Preview, Comment Apr 14, 2026 11:27pm
form-filling Ready Ready Preview, Comment Apr 14, 2026 11:27pm
research-canvas Ready Ready Preview, Comment Apr 14, 2026 11:27pm
travel Ready Ready Preview, Comment Apr 14, 2026 11:27pm

Request Review

@github-actions

Copy link
Copy Markdown
Contributor

📣 Social Copy Generator

Generate social media copies (Twitter/X, LinkedIn, Blog Post) for this PR using Claude.

  • Generate social media copies

@pkg-pr-new

pkg-pr-new Bot commented Apr 12, 2026 •

Copy link
Copy Markdown

@AlemTuzlak
AlemTuzlak merged commit 8b1bc3a into main Apr 16, 2026
35 checks passed
@AlemTuzlak
AlemTuzlak deleted the fix/token-trimming-tool-pairing-2450 branch April 16, 2026 09:55
zeekay pushed a commit to Dream-AI-4444/CopilotKit that referenced this pull request Sep 11, 2026
…rimming (CopilotKit#2450) (CopilotKit#3820)

## Summary

Fixes CopilotKit#2450

Token trimming in the Anthropic adapter could remove assistant messages
containing `tool_use` blocks while keeping the corresponding user
messages with `tool_result` blocks. Anthropic's API rejects orphaned
`tool_result` messages. This adds a post-processing step that removes
orphaned `tool_result` blocks after trimming.

## Test plan

- [ ] Verify token trimming preserves tool_use/tool_result pairing
- [ ] Verify orphaned tool_result blocks are removed
- [ ] Verify messages with mixed content (tool_result + text) retain
non-orphaned content

This branch was successfully deployed

5 active deployments
Preview – travel — 78c65a8a Deployed Apr 14, 2026 by vercel[bot]
Preview – research-canvas — 78c65a8a Deployed Apr 14, 2026 by vercel[bot]
Preview – form-filling — 78c65a8a Deployed Apr 14, 2026 by vercel[bot]
Preview – docs — 78c65a8a Deployed Apr 14, 2026 by vercel[bot]
Preview – chat-with-your-data — 78c65a8a Deployed Apr 14, 2026 by vercel[bot]
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: Conversation gets broken if token limit message trim is applied between tool_use and tool_result

2 participants