Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Arize ai Phoenix Anthropic Adapter

From Leeroopedia
Revision as of 12:03, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Arize_ai_Phoenix_Anthropic_Adapter.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Overview

The AnthropicAdapter is a concrete implementation of the BaseLLMAdapter abstract class that integrates the Anthropic Claude API into the Phoenix evaluator framework. It resides in the phoenix-evals sub-package and provides both synchronous and asynchronous methods for text generation and structured object extraction via Anthropic's Messages API. The adapter is auto-registered using the @register_adapter and @register_provider decorators, enabling automatic detection when an Anthropic client is passed to the evaluation pipeline.

Description

The module defines the AnthropicAdapter class along with two supporting functions: identify_anthropic_client() and get_anthropic_rate_limit_errors(). Together they enable the Phoenix evaluator framework to:

  • Identify whether a given client object is an Anthropic client (by checking for the AnthropicClientWrapper type, the anthropic module namespace, or the presence of messages.create).
  • Retrieve Anthropic-specific rate limit exception types for integration with the rate limiting subsystem.
  • Adapt Anthropic's Messages API to the four core adapter methods: generate_text(), async_generate_text(), generate_object(), and async_generate_object().

The adapter handles several Anthropic-specific concerns:

  • System message extraction: Anthropic requires system messages to be passed as a separate system parameter rather than within the messages array. The adapter extracts system messages automatically.
  • max_tokens requirement: Anthropic mandates the max_tokens parameter. The adapter injects a default of 4096 if not provided.
  • Tool calling for structured output: Since Anthropic does not support native structured output, the adapter uses tool calling (via tool_choice with forced tool use) for all object generation methods. The STRUCTURED_OUTPUT method raises a ValueError.
  • Sync/async client detection: The adapter inspects the client class name (looking for "Async") or uses inspect.iscoroutinefunction to determine whether the underlying client is synchronous or asynchronous, and enforces correct method usage.

Usage

The adapter is typically instantiated automatically by the Phoenix evaluator framework when a user provides an Anthropic client. Direct instantiation is also possible:

from anthropic import Anthropic
from phoenix.evals.llm.adapters.anthropic.adapter import AnthropicAdapter

# Synchronous usage
client = Anthropic(api_key="sk-...")
adapter = AnthropicAdapter(client=client, model="claude-sonnet-4-20250514")

# Text generation
response = adapter.generate_text("What is the capital of France?")

# Structured output via tool calling
schema = {
    "type": "object",
    "properties": {
        "label": {"type": "string", "enum": ["relevant", "irrelevant"]},
        "explanation": {"type": "string"}
    },
    "required": ["label", "explanation"]
}
result = adapter.generate_object("Classify this text.", schema=schema)
from anthropic import AsyncAnthropic
from phoenix.evals.llm.adapters.anthropic.adapter import AnthropicAdapter

# Asynchronous usage
async_client = AsyncAnthropic(api_key="sk-...")
adapter = AnthropicAdapter(client=async_client, model="claude-sonnet-4-20250514")

response = await adapter.async_generate_text("Summarize this document.")
result = await adapter.async_generate_object(prompt, schema=schema)

Code Reference

Symbol Kind Location Lines
identify_anthropic_client() Function packages/phoenix-evals/src/phoenix/evals/llm/adapters/anthropic/adapter.py 14-25
get_anthropic_rate_limit_errors() Function packages/phoenix-evals/src/phoenix/evals/llm/adapters/anthropic/adapter.py 28-31
AnthropicAdapter Class packages/phoenix-evals/src/phoenix/evals/llm/adapters/anthropic/adapter.py 44-379
AnthropicAdapter.generate_text() Method packages/phoenix-evals/src/phoenix/evals/llm/adapters/anthropic/adapter.py 75-96
AnthropicAdapter.async_generate_text() Method packages/phoenix-evals/src/phoenix/evals/llm/adapters/anthropic/adapter.py 98-123
AnthropicAdapter.generate_object() Method packages/phoenix-evals/src/phoenix/evals/llm/adapters/anthropic/adapter.py 125-154
AnthropicAdapter.async_generate_object() Method packages/phoenix-evals/src/phoenix/evals/llm/adapters/anthropic/adapter.py 156-179
AnthropicAdapter._build_messages() Method packages/phoenix-evals/src/phoenix/evals/llm/adapters/anthropic/adapter.py 308-360
AnthropicAdapter._schema_to_tool() Method packages/phoenix-evals/src/phoenix/evals/llm/adapters/anthropic/adapter.py 235-244

I/O Contract

generate_text / async_generate_text

Direction Type Description
Input Union[PromptLike, MultimodalPrompt] A string prompt, a list of message dicts, a list of Message TypedDicts, or a legacy MultimodalPrompt
Input **kwargs Additional keyword arguments forwarded to client.messages.create()
Output str The text content from the first content block of the Anthropic response
Raises ValueError If sync method is called on async client (or vice versa), or if the response has an unexpected format

generate_object / async_generate_object

Direction Type Description
Input Union[PromptLike, MultimodalPrompt] A prompt in any supported format
Input Dict[str, Any] A JSON schema that the returned object must conform to
Input ObjectGenerationMethod The generation strategy: AUTO (default), TOOL_CALLING, or STRUCTURED_OUTPUT (not supported, raises error)
Output Dict[str, Any] A dictionary extracted from the tool call input field in the response
Raises ValueError If STRUCTURED_OUTPUT is requested, schema validation fails, or no tool use is found in the response

Usage Examples

Using with the Evaluator Framework

from anthropic import Anthropic
from phoenix.evals import Evaluator

# The framework auto-detects the Anthropic client via identify_anthropic_client()
client = Anthropic()
evaluator = Evaluator(client=client, model="claude-sonnet-4-20250514")

Message Format Conversion

The adapter converts various prompt formats to Anthropic's expected message structure. System messages are extracted and passed separately:

from phoenix.evals.llm.prompts import Message, MessageRole

messages = [
    Message(role=MessageRole.SYSTEM, content="You are a helpful assistant."),
    Message(role=MessageRole.USER, content="Hello!"),
]

# Internally, _build_messages() returns:
# messages = [{"role": "user", "content": "Hello!"}]
# system = "You are a helpful assistant."

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment