Skip to content

generateObject fails using Claude provider fails with "tool_use ids were found without tool_result" error on valid role:'tool' message structure #8318

@cezudas

Description

@cezudas

Description

Here's a sample project demonstrating that the same test messages work with OpenAI but fail with AI_APICallError using Anhropic Claude 4.

=== AI SDK Bug Reproduction ===

Input messages we provide to AI SDK:
Message 0 (user): {
  "role": "user",
  "content": "generate 10 items"
}
Message 1 (assistant): {
  "role": "assistant",
  "content": [
    {
      "type": "tool-call",
      "toolCallId": "tool-example-123",
      "toolName": "json",
      "input": {
        "message": "generate 10 items"
      }
    },
    {
      "type": "text",
      "text": "I generated code for 10 items."
    }
  ]
}
Message 2 (tool): {
  "role": "tool",
  "content": [
    {
      "type": "tool-result",
      "toolCallId": "tool-example-123",
      "toolName": "json",
      "output": {
        "type": "json",
        "value": {
          "code": "export const code = () => [...]",
          "packageJson": "{}"
        }
      }
    }
  ]
}
Message 3 (user): {
  "role": "user",
  "content": "generate 100 items"
}

=== Calling AI SDK generateObject ===

ERROR (expected):
{
  "name": "AI_APICallError",
  "url": "https://api.anthropic.com/v1/messages",
  "requestBodyValues": {
    "model": "claude-4-sonnet-20250514",
    "max_tokens": 4096,
    "system": [
      {
        "type": "text",
        "text": "You are a helpful assistant. Respond with JSON."
      }
    ],
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "generate 10 items"
          }
        ]
      },
      {
        "role": "assistant",
        "content": [
          {
            "type": "tool_use",
            "id": "tool-example-123",
            "name": "json",
            "input": {
              "message": "generate 10 items"
            }
          },
          {
            "type": "text",
            "text": "I generated code for 10 items."
          }
        ]
      },
      {
        "role": "user",
        "content": [
          {
            "type": "tool_result",
            "tool_use_id": "tool-example-123",
            "content": "{\"code\":\"export const code = () => [...]\",\"packageJson\":\"{}\"}"
          },
          {
            "type": "text",
            "text": "generate 100 items"
          }
        ]
      }
    ],
    "tools": [
      {
        "name": "json",
        "description": "Respond with a JSON object.",
        "input_schema": {
          "type": "object",
          "properties": {
            "response": {
              "type": "string",
              "description": "Your response to the user"
            }
          },
          "required": [
            "response"
          ],
          "additionalProperties": false,
          "$schema": "http://json-schema.org/draft-07/schema#"
        }
      }
    ],
    "tool_choice": {
      "type": "tool",
      "name": "json"
    }
  },
  "statusCode": 400,
  "responseHeaders": {
    "anthropic-organization-id": "afeff4d1-2dba-4bc5-93e7-afacdc41d499",
    "cf-cache-status": "DYNAMIC",
    "cf-ray": "975a040f5f30ca29-OTP",
    "connection": "keep-alive",
    "content-length": "306",
    "content-type": "application/json",
    "date": "Wed, 27 Aug 2025 07:57:03 GMT",
    "request-id": "req_011CSXrhod7Lyo6xF9pjaGJh",
    "server": "cloudflare",
    "strict-transport-security": "max-age=31536000; includeSubDomains; preload",
    "via": "1.1 google",
    "x-envoy-upstream-service-time": "15",
    "x-robots-tag": "none",
    "x-should-retry": "false"
  },
  "responseBody": "{\"type\":\"error\",\"error\":{\"type\":\"invalid_request_error\",\"message\":\"messages.1: `tool_use` ids were found without `tool_result` blocks immediately after: tool-example-123. Each `tool_use` block must have a corresponding `tool_result` block in the next message.\"},\"request_id\":\"req_011CSXrhod7Lyo6xF9pjaGJh\"}",
  "isRetryable": false,
  "data": {
    "type": "error",
    "error": {
      "type": "invalid_request_error",
      "message": "messages.1: `tool_use` ids were found without `tool_result` blocks immediately after: tool-example-123. Each `tool_use` block must have a corresponding `tool_result` block in the next message."
    }
  }
}

✅ BUG REPRODUCED: This confirms AI SDK is combining messages incorrectly
The error indicates Claude received malformed messages despite our correct input

AI SDK Version

"dependencies": {
"@ai-sdk/anthropic": "2.0.8",
"@ai-sdk/openai": "2.0.22",
"ai": "5.0.26"
}

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions