Skip to main content
The AnthropicMessagesClient class provides a wrapper around the Anthropic Messages API using the AsyncAnthropic client.

Overview

This client implements the Client interface for Anthropic’s Messages API, handling:
  • Message format conversion between Verifiers and Anthropic formats
  • System message extraction and formatting
  • Tool calling support
  • Thinking blocks (extended thinking / chain-of-thought reasoning)
  • Image content handling (data URLs to base64)
  • Context length error handling

Class Definition

Generic type parameters:
  • ClientT: AsyncAnthropic - The Anthropic async client
  • MessagesT: list[AnthropicMessageParam] - List of Anthropic message parameters
  • ResponseT: AnthropicMessage - Anthropic Message object
  • ToolT: AnthropicToolParam - Anthropic tool parameter type

Constructor

AsyncAnthropic | ClientConfig
required
Either a pre-configured AsyncAnthropic client or a ClientConfig to create one.

Example

Methods

setup_client

Creates an AsyncAnthropic client from a ClientConfig.
ClientConfig
required
Configuration with API key, base URL, and other settings.
Returns: Configured AsyncAnthropic instance.

close

Closes the underlying AsyncAnthropic client connection.

to_native_prompt

Converts Verifiers messages to Anthropic’s message format.
Messages
required
List of Verifiers message objects (SystemMessage, UserMessage, AssistantMessage, ToolMessage, or TextMessage).
Returns: Tuple of (anthropic_messages, extra_kwargs) where extra_kwargs contains {"system": "<system_content>"} if system messages are present. Special handling:
  • System messages: Extracted and concatenated, passed as system parameter in extra_kwargs
  • Tool messages: Grouped into batches and converted to user messages with tool_result content blocks
  • Thinking blocks: Preserved in assistant messages (for extended thinking models)
  • Images: Data URLs converted to base64 format with proper media type detection
  • Audio: Replaced with [audio] placeholder text
Supported message types:
  • SystemMessage → Extracted as system parameter
  • UserMessageAnthropicMessageParam with role=“user”
  • AssistantMessageAnthropicMessageParam with role=“assistant” (supports tool calls and thinking blocks)
  • ToolMessage → Converted to user message with tool_result blocks
  • TextMessageAnthropicMessageParam with role=“user”

to_native_tool

Converts a Verifiers Tool to Anthropic’s tool parameter format.
Tool
required
Verifiers tool definition with name, description, and parameters.
Returns: AnthropicToolParam object. Note: Anthropic tools use input_schema instead of parameters.

get_native_response

Calls the Anthropic Messages API and returns the native response.
list[AnthropicMessageParam]
required
List of Anthropic message parameters.
str
required
Anthropic model identifier (e.g., "claude-3-5-sonnet-20241022", "claude-3-5-haiku-20241022").
SamplingArgs
required
Sampling parameters. max_tokens is required by Anthropic; defaults to 4096 if not provided.
list[AnthropicToolParam] | None
default:"None"
Optional list of tools in Anthropic format.
Returns: Anthropic Message object. Raises: OverlongPromptError if the prompt exceeds the model’s context length. Special handling:
  • max_tokens: Required by Anthropic API, defaults to 4096 with a warning if not provided
  • Filtered parameters: Removes n and stop (not supported by Anthropic)
  • Internal state: Removes state key from kwargs (internal framework field)

raise_from_native_response

Validates the Anthropic response. Currently a no-op (passes all responses).
AnthropicMessage
required
The Anthropic Message response.

from_native_response

Converts an Anthropic Message to a Verifiers Response.
AnthropicMessage
required
The Anthropic Message response.
Returns: Verifiers Response object with:
  • id: Response ID from Anthropic
  • created: Current timestamp (Anthropic doesn’t provide this)
  • model: Model name from response
  • usage: Token counts (input_tokens, output_tokens, total)
  • message: Response message with content, reasoning content, tool calls, thinking blocks, and finish reason
Parsed content blocks:
  • text: Concatenated into message.content
  • thinking: Concatenated into message.reasoning_content, stored in thinking_blocks
  • redacted_thinking: Stored in thinking_blocks
  • tool_use: Converted to Verifiers ToolCall objects
Finish reasons:
  • "end_turn""stop"
  • "max_tokens""length"
  • "tool_use""tool_calls"
  • Other → None

Usage Example

Thinking Blocks Support

Anthropic models with extended thinking (like Claude 3.7 Sonnet with thinking) return thinking and redacted_thinking content blocks. These are:
  • Extracted as reasoning content: Available in response.message.reasoning_content
  • Preserved as thinking blocks: Available in response.message.thinking_blocks
  • Maintained across turns: Thinking blocks from assistant messages are preserved when converting back to native format

Example

Error Handling

Overlong Prompt Errors

The @_handle_anthropic_overlong_prompt decorator catches BadRequestError and converts context length errors to OverlongPromptError. It detects phrases like:
  • “prompt is too long”
  • “exceed context limit”
  • “too many total text bytes”
  • “context length”
  • “input is too long”
Authentication and permission errors are re-raised without wrapping.

Image Handling

The client converts OpenAI-style image URLs to Anthropic’s base64 format:

See Also