Implementation:Arize ai Phoenix LLM Base Types
Overview
The LLM Base Types module defines the foundational abstract types and data structures for the Phoenix evaluator framework's LLM adapter abstraction layer. It resides at phoenix.evals.llm.types and establishes the contract that all LLM provider adapters must implement. The module exports the BaseLLMAdapter abstract class, the ObjectGenerationMethod enum, and two dataclass registrations used by the adapter registry system.
Description
The module is compact (134 lines) and contains four key symbols:
ObjectGenerationMethod (Enum)
Defines the three strategies for structured object generation from LLMs:
| Value | Description |
|---|---|
AUTO |
The adapter chooses the best available method (typically structured output first, then tool calling) |
TOOL_CALLING |
Force the use of function/tool calling to extract structured data |
STRUCTURED_OUTPUT |
Force the use of native structured output (e.g., OpenAI's json_schema response format, Google's response_schema)
|
BaseLLMAdapter (Abstract Class)
The abstract base class that all provider-specific adapters must implement. It defines:
- Constructor:
__init__(self, client: Any, model: str)-- stores the provider client instance and the model identifier. - client_name(): Abstract class method returning the adapter's provider name string (e.g.,
"openai","anthropic"). - generate_text(): Synchronous text generation from a prompt.
- async_generate_text(): Asynchronous text generation from a prompt.
- generate_object(): Synchronous structured output generation conforming to a JSON schema.
- async_generate_object(): Asynchronous structured output generation conforming to a JSON schema.
- model_name (property): Returns a human-readable model identifier. The default implementation combines the adapter and client class names; concrete adapters typically override this.
All four core methods accept Union[PromptLike, MultimodalPrompt] as input, ensuring backward compatibility with the legacy prompt system while supporting the new Message-based format.
Registration Dataclasses
- AdapterRegistration: Binds an adapter class to an identification function and a name. Used by the
@register_adapterdecorator to register adapters that can be auto-detected from client objects. - ProviderRegistration: Binds an adapter class to a provider name, client factory, rate limit error retriever, and dependency list. Used by the
@register_providerdecorator to register adapters that can be instantiated from a provider string.
Usage
from phoenix.evals.llm.types import BaseLLMAdapter, ObjectGenerationMethod
# Implementing a custom adapter
class MyCustomAdapter(BaseLLMAdapter):
@classmethod
def client_name(cls) -> str:
return "my-provider"
def generate_text(self, prompt, **kwargs):
# Call custom LLM API
return self.client.complete(prompt)
async def async_generate_text(self, prompt, **kwargs):
return await self.client.acomplete(prompt)
def generate_object(self, prompt, schema, method=ObjectGenerationMethod.AUTO, **kwargs):
# Implement structured output extraction
...
async def async_generate_object(self, prompt, schema, method=ObjectGenerationMethod.AUTO, **kwargs):
...
# Using ObjectGenerationMethod to control generation strategy
from phoenix.evals.llm.types import ObjectGenerationMethod
# Force tool calling
result = adapter.generate_object(
prompt, schema=schema, method=ObjectGenerationMethod.TOOL_CALLING
)
# Force structured output
result = adapter.generate_object(
prompt, schema=schema, method=ObjectGenerationMethod.STRUCTURED_OUTPUT
)
Code Reference
I/O Contract
BaseLLMAdapter.generate_text() / async_generate_text()
| Direction | Type | Description |
|---|---|---|
| Input | Union[PromptLike, MultimodalPrompt] |
A string, list of message dicts, list of Message TypedDicts, or legacy MultimodalPrompt
|
| Input | **kwargs |
Provider-specific keyword arguments forwarded to the underlying API |
| Output | str |
The generated text response |
BaseLLMAdapter.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 for the structured output |
| Input | ObjectGenerationMethod |
The generation strategy (default: AUTO)
|
| Input | **kwargs |
Provider-specific keyword arguments |
| Output | Dict[str, Any] |
Structured data conforming to the provided schema |
AdapterRegistration Fields
| Field | Type | Description |
|---|---|---|
adapter_class |
Type[BaseLLMAdapter] |
The adapter class to instantiate |
identifier |
Callable[[Any], bool] |
Function that returns True if the client matches this adapter
|
name |
str |
Human-readable adapter name |
ProviderRegistration Fields
| Field | Type | Description |
|---|---|---|
provider |
str |
Provider identifier (e.g., "openai", "anthropic")
|
adapter_class |
Type[BaseLLMAdapter] |
The adapter class to instantiate |
client_name |
str |
The adapter's client name |
client_factory |
Callable[..., Any] |
Factory function to create a client from configuration |
get_rate_limit_errors |
Optional[Callable[..., List[Type[Exception]]]] |
Function returning rate limit exception types for this provider |
dependencies |
List[str] |
Python package names required by this provider |
Usage Examples
Registration Decorators
from phoenix.evals.registries import register_adapter, register_provider
def identify_my_client(client):
return hasattr(client, "my_custom_method")
def get_rate_limit_errors():
return [MyRateLimitError]
@register_adapter(identifier=identify_my_client, name="my-provider")
@register_provider(
provider="my-provider",
client_factory=create_my_client,
get_rate_limit_errors=get_rate_limit_errors,
dependencies=["my-sdk"],
)
class MyAdapter(BaseLLMAdapter):
...
Related Pages
- Arize_ai_Phoenix_Anthropic_Adapter - Concrete implementation for Anthropic Claude
- Arize_ai_Phoenix_OpenAI_Adapter - Concrete implementation for OpenAI
- Arize_ai_Phoenix_Google_Adapter - Concrete implementation for Google GenAI
- Arize_ai_Phoenix_LangChain_Adapter - Concrete implementation for LangChain models
- Arize_ai_Phoenix_LiteLLM_Adapter - Concrete implementation for LiteLLM
- Arize_ai_Phoenix_LLM_Prompts - Defines
PromptLiketype alias consumed byBaseLLMAdapter - Arize_ai_Phoenix_Evals_Rate_Limiters - Consumes
ProviderRegistration.get_rate_limit_errors