Skip to content

Conversation

@lingjiekong
Copy link

@lingjiekong lingjiekong commented Dec 26, 2025

Summary

This PR enables the tool_runner API on AWS Bedrock by adding message sanitization to strip Bedrock-unsupported fields.

Fixes #1106

Problem

When using client.beta.messages.tool_runner() with Bedrock, the SDK adds internal tracking fields that Bedrock's stricter API validation rejects with 400 errors:

Error code: 400 - {'message': 'messages.1.content.1.text.parsed_output: Extra inputs are not permitted'}
Error code: 400 - {'message': 'messages.1.content.2.tool_use.caller: Extra inputs are not permitted'}

Root Cause

  1. tool_runner not exposed: The Bedrock Messages class only delegates create, not tool_runner
  2. parsed_output rejection: The SDK's .parse() method adds parsed_output to text blocks for structured output tracking
  3. caller rejection: The SDK adds caller to tool_use blocks for code execution features

Solution

  1. Add tool_runner delegation - Expose the tool_runner API on Bedrock clients
  2. Add _sanitize_for_bedrock() helper - Strip unsupported fields from message content blocks
  3. Wrap create, parse, stream methods - Apply sanitization transparently before API calls

The sanitization is applied at the Bedrock layer so it doesn't affect direct Anthropic API usage.

Changes

  • Add BEDROCK_UNSUPPORTED_FIELDS set for extensibility
  • Add _sanitize_for_bedrock() sanitization function
  • Wrap create, parse, stream methods with sanitization
  • Add tool_runner delegation to both sync and async classes

Tested Features

  • tool_runner API access on Bedrock
  • Automatic tool execution loop
  • Compaction control (context management with token_threshold)
  • parsed_output field sanitization
  • caller field sanitization
  • Sync and async clients

Example Usage

from anthropic import AsyncAnthropicBedrock

client = AsyncAnthropicBedrock(aws_region="us-west-2")

# Now tool_runner works with Bedrock!
result = await client.beta.messages.tool_runner(
    model="us.anthropic.claude-sonnet-4-5-20250929-v1:0",
    max_tokens=16384,
    tools=[...],
    messages=[...],
    compaction_control={
        "type": "auto",
        "enabled": True,
        "token_threshold": 20000,
    }
)

Test plan

  • Verified tool_runner works with Bedrock after patch
  • Verified compaction control triggers correctly at token threshold
  • Verified no 400 errors from parsed_output or caller fields
  • Verified existing create functionality still works

🤖 Generated with Claude Code

This PR enables the `tool_runner` API on AWS Bedrock by:

1. Adding `tool_runner` delegation to Messages and AsyncMessages classes
2. Wrapping `create`, `parse`, and `stream` methods with sanitization
3. Stripping Bedrock-unsupported fields (`parsed_output`, `caller`)

## Problem

When using `client.beta.messages.tool_runner()` with Bedrock, the SDK
adds internal tracking fields that Bedrock's stricter API validation
rejects with 400 errors:

- `parsed_output` on text blocks (structured output tracking)
- `caller` on tool_use blocks (code execution features)

## Solution

The `_sanitize_for_bedrock()` function strips these fields from message
content blocks before sending to Bedrock. The sanitization is applied
at the Bedrock layer so it doesn't affect direct Anthropic API usage.

## Tested Features

- tool_runner API access
- Automatic tool execution loop
- Compaction control (context management)
- Sync and async clients

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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.

Feature Request: Add tool_runner support for AnthropicBedrock client

1 participant