#unexpected `tool_use_id` found in `tool_result` blocks

5 messages · Page 1 of 1 (latest)

fair sable
#

Making this request via Open Router issues an error:

Minimum Repro

{
    "messages": [
        {
            "role": "assistant",
            "content": "",
            "tool_calls": [
                {
                    "id": "toolu_vrtx_01BUEnscXFBdeCwds9zSLXJB",
                    "type": "function",
                    "function": {
                        "name": "file_read",
                        "arguments": "abc.txt"
                    }
                }
            ]
        },
        {
            "role": "tool",
            "content": "Hello World!",
            "name": "file_read",
            "tool_call_id": "toolu_vrtx_01BUEnscXFBdeCwds9zSLXJB"
        }
    ],
    "model": "anthropic/claude-sonnet-4",
    "stream": false,
}

ERROR

{
    "error": {
        "message": "Provider returned error",
        "code": 400,
        "metadata": {
            "raw": "{\"type\":\"error\",\"error\":{\"type\":\"invalid_request_error\",\"message\":\"messages.0.content.0: unexpected `tool_use_id` found in `tool_result` blocks: toolu_vrtx_01BUEnscXFBdeCwds9zSLXJB. Each `tool_result` block must have a corresponding `tool_use` block in the previous message.\"}}",
            "provider_name": "Anthropic"
        }
    },
    "user_id": "user_2q36EtHSol1CeC92ASe8n5NfpRV"
}
fair sable
#

IMPORTANT: I don't face this issue when I am using the OpenAI APIs

lofty obsidian
#

I have the same problem. Have solved it?

lofty obsidian
#

I think I found the problem.

The message must have a user message before the assistants call.

###
POST https://openrouter.ai/api/v1/chat/completions
Content-Type: application/json
Authorization: Bearer {{$dotenv OPENROUTER_API_KEY}}

{
  "model": "anthropic/claude-sonnet-4",
  "messages": [
  {
    "role": "user",
    "content": "\nYour current task is to create the page store.\nAlways return a default export.\nYou must write to the file src/frontend/pages/Todo/store.ts.\n\nFirst read than write.\n"
  },
  {
    "role": "assistant",
    "tool_calls": [
      {
        "id": "toolu_01G86T4CrzisAWNR56RFKoNF",
        "index": 0,
        "type": "function",
        "function": {
          "name": "readFiles",
          "arguments": "{\"filePaths\": [\"src/frontend/api/openapi-types.ts\",\"src/frontend/pages/Home.tsx\"]}"
        }
      }
    ]
  },
  {
    "role": "tool",
    "content": [
      {
        "type": "text",
        "text": "// fake result, pretend to read the files",
        "cache_control": {
          "type": "ephemeral"
        }
      }
    ],
    "tool_call_id": "toolu_01G86T4CrzisAWNR56RFKoNF"
  }
]
  ,
  "provider": {
    "sort": "throughput"
  },
  "usage": {
    "include": true,
  }
}
``` this works, but removing the first messages returns the exact error you posted.