AnthropicMessagesClient class provides a wrapper around the Anthropic Messages API using the AsyncAnthropic client.
Overview
This client implements theClient 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
- 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
Either a pre-configured
AsyncAnthropic client or a ClientConfig to create one.Example
Methods
setup_client
AsyncAnthropic client from a ClientConfig.
Configuration with API key, base URL, and other settings.
AsyncAnthropic instance.
close
AsyncAnthropic client connection.
to_native_prompt
List of Verifiers message objects (
SystemMessage, UserMessage, AssistantMessage, ToolMessage, or TextMessage).(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
systemparameter in extra_kwargs - Tool messages: Grouped into batches and converted to user messages with
tool_resultcontent 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
SystemMessage→ Extracted as system parameterUserMessage→AnthropicMessageParamwith role=“user”AssistantMessage→AnthropicMessageParamwith role=“assistant” (supports tool calls and thinking blocks)ToolMessage→ Converted to user message withtool_resultblocksTextMessage→AnthropicMessageParamwith role=“user”
to_native_tool
Tool to Anthropic’s tool parameter format.
Verifiers tool definition with name, description, and parameters.
AnthropicToolParam object.
Note: Anthropic tools use input_schema instead of parameters.
get_native_response
List of Anthropic message parameters.
Anthropic model identifier (e.g.,
"claude-3-5-sonnet-20241022", "claude-3-5-haiku-20241022").Sampling parameters.
max_tokens is required by Anthropic; defaults to 4096 if not provided.Optional list of tools in Anthropic format.
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
nandstop(not supported by Anthropic) - Internal state: Removes
statekey from kwargs (internal framework field)
raise_from_native_response
The Anthropic Message response.
from_native_response
Message to a Verifiers Response.
The Anthropic Message response.
Response object with:
id: Response ID from Anthropiccreated: Current timestamp (Anthropic doesn’t provide this)model: Model name from responseusage: Token counts (input_tokens, output_tokens, total)message: Response message with content, reasoning content, tool calls, thinking blocks, and finish reason
- text: Concatenated into
message.content - thinking: Concatenated into
message.reasoning_content, stored inthinking_blocks - redacted_thinking: Stored in
thinking_blocks - tool_use: Converted to Verifiers
ToolCallobjects
"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) returnthinking 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”
Image Handling
The client converts OpenAI-style image URLs to Anthropic’s base64 format:See Also
- Client - Base client class
- OpenAIChatCompletionsClient - OpenAI implementation
- ClientConfig - Configuration type
- Response - Response type
- Tool - Tool definition type