Skip to content

Fix HTTP 400 errors after Claude Code CLI tool call cancellation#2

Merged
hao1939 merged 2 commits intomasterfrom
copilot/fix-1
Jul 31, 2025
Merged

Fix HTTP 400 errors after Claude Code CLI tool call cancellation#2
hao1939 merged 2 commits intomasterfrom
copilot/fix-1

Conversation

Copy link
Copy Markdown

Copilot AI commented Jul 31, 2025

This PR fixes a critical issue where cancelling tool calls in Claude Code CLI would cause all subsequent API requests to fail with HTTP 400 errors.

Problem

When a user cancels a tool call in Claude Code CLI, the conversation history retains tool_use blocks without corresponding tool_result blocks. The Anthropic API strictly requires that every tool_use block be followed by a matching tool_result block in the next message. Without this pairing, all subsequent requests fail with:

HTTP 400: tool_use ids were found without tool_result blocks immediately after: toolu_vrtx_01GG3T6npgARqCjxkn9Fm9KW. 
Each tool_use block must have a corresponding tool_result block in the next message.

This creates a persistent error state where the API becomes unusable until the conversation is reset.

Solution

Added intelligent tool conversation validation and cleanup that:

  1. Detects abandoned tool calls: Identifies tool_use blocks followed by user messages without corresponding tool_result blocks
  2. Preserves valid pending calls: Keeps tool_use blocks at conversation end (still awaiting results)
  3. Removes only abandoned calls: Only strips tool_use blocks when there's clear evidence of cancellation
  4. Maintains conversation integrity: Preserves all other message content and structure

Example

Before the fix, this conversation would cause persistent 400 errors:

{
  "messages": [
    {"role": "user", "content": "What's the weather?"},
    {
      "role": "assistant", 
      "content": [
        {"type": "text", "text": "I'll check for you."},
        {"type": "tool_use", "id": "call_123", "name": "get_weather", "input": {...}}
      ]
    },
    {"role": "user", "content": "Actually, never mind."}
  ]
}

After the fix, the abandoned tool_use block is automatically removed, preventing API rejection.

Implementation

  • Minimal changes: Only 6 lines modified in existing translation logic
  • Surgical approach: New isolated cleanup module with comprehensive test coverage
  • Zero impact: No effect on valid tool conversations or pending tool calls
  • Smart detection: Distinguishes between abandoned vs. pending tool calls

The fix integrates seamlessly into the existing Anthropic message translation pipeline, ensuring all problematic payloads are cleaned before reaching the API.

Fixes #1.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • aur.archlinux.org
    • Triggering command: bun run ./src/main.ts --help (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] after human cancel on claude code cli, the copilot-api keep return 400 Fix HTTP 400 errors after Claude Code CLI tool call cancellation Jul 31, 2025
Copilot AI requested a review from hao1939 July 31, 2025 12:16
@hao1939
Copy link
Copy Markdown
Owner

hao1939 commented Jul 31, 2025

run the test

Copy link
Copy Markdown
Author

Copilot AI commented Jul 31, 2025

run the test

Tests have been successfully executed. All 36 tests pass across 5 test files, including the tool conversation cleanup functionality. The code also passes linting and builds successfully.

@hao1939
Copy link
Copy Markdown
Owner

hao1939 commented Jul 31, 2025

LSTM

@hao1939 hao1939 marked this pull request as ready for review July 31, 2025 13:35
@hao1939 hao1939 merged commit 258d9e3 into master Jul 31, 2025
1 check passed
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.

after human cancel on claude code cli, the copilot-api keep return 400

2 participants