Skip to content

fix(anthropic): reorder interleaved text before tool_use - #640

Open
Wibias wants to merge 2 commits into
lidge-jun:devfrom
Wibias:fix/620-anthropic-tool-result
Open

fix(anthropic): reorder interleaved text before tool_use#640
Wibias wants to merge 2 commits into
lidge-jun:devfrom
Wibias:fix/620-anthropic-tool-result

Conversation

@Wibias

@Wibias Wibias commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Reorder assistant content so text/thinking always precede tool_use blocks (Anthropic rejects interleaved text-after-tool_use as unpaired tool calls).
  • Regression test for the multi-step history shape.

Test plan

  • bun test tests/adapter-usage.test.ts
  • CI green

Closes #620

Summary by CodeRabbit

  • Bug Fixes

    • Improved Anthropic tool-call handling when assistant messages contain text and multiple tool calls.
    • Preserved tool-call pairing and ordering in requests, preventing malformed tool histories.
  • Tests

    • Added coverage for interleaved text, tool calls, and tool results.
    • Verified that serialized requests maintain the correct tool-call and result sequence.

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@Wibias, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 51 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 32a07531-70af-4848-b6c9-290416eef517

📥 Commits

Reviewing files that changed from the base of the PR and between 09d0e78 and a17ae34.

📒 Files selected for processing (1)
  • tests/adapter-usage.test.ts
📝 Walkthrough

Walkthrough

The Anthropic adapter now emits assistant text and thinking blocks before tool-use blocks, regardless of their original order. A regression test verifies interleaved text and multiple tool calls preserve matching tool_use_id values in subsequent tool results.

Changes

Anthropic tool-use ordering

Layer / File(s) Summary
Reorder assistant blocks and validate pairing
src/adapters/anthropic.ts, tests/adapter-usage.test.ts
Assistant conversion separates preface and tool_use blocks, emits preface content first, and tests ordering plus corresponding tool-result IDs.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested labels: bug

Suggested reviewers: lidge-jun, ingwannu

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately describes the main change: reordering interleaved Anthropic text before tool_use blocks.
Linked Issues check ✅ Passed src/adapters/anthropic.ts reorders assistant blocks so text/thinking precede tool_use, and the new test covers the #620 pairing failure mode.
Out of Scope Changes check ✅ Passed The code change and regression test are directly tied to Anthropic tool_use ordering; no unrelated edits are introduced.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the bug Something isn't working label Jul 28, 2026
@Wibias

Wibias commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review
@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

@Wibias: I’ll review the changes in #640, focusing on whether the reordering preserves Anthropic message semantics across multi-step tool-call histories.

✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/adapter-usage.test.ts`:
- Line 518: In the callback containing the JSON request parsing, remove the
duplicate const body declarations and retain a single declaration of body.
Ensure the remaining declaration is used by the regression assertions without
changing the callback’s behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: f1a484fe-1006-4433-97b8-b364038dc7de

📥 Commits

Reviewing files that changed from the base of the PR and between ca7b104 and 09d0e78.

📒 Files selected for processing (2)
  • src/adapters/anthropic.ts
  • tests/adapter-usage.test.ts

Comment thread tests/adapter-usage.test.ts Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anthropic 400: tool_use ids found without tool_result blocks (messagesToAnthropicFormat)

1 participant