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 LangChain Adapter

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

Overview

The LangChainModelAdapter is a concrete implementation of the BaseLLMAdapter abstract class that wraps LangChain model instances for use within the Phoenix evaluator framework. It resides in the phoenix-evals sub-package and adapts LangChain's invoke/ainvoke, with_structured_output, and bind_tools interfaces to the four core adapter methods. The adapter is registered as a multi-provider component, supporting both OpenAI and Anthropic LangChain backends.

Description

The module defines the LangChainModelAdapter class and three supporting functions: identify_langchain_client(), get_anthropic_langchain_rate_limit_errors(), and get_openai_langchain_rate_limit_errors(). Key design characteristics:

  • Client identification: Detects LangChain models by checking for the "langchain" module namespace or the presence of invoke/predict methods.
  • Dual provider registration: The adapter is registered under both openai and anthropic providers via separate @register_provider decorators, each with its own client factory and rate limit error type.
  • Message format conversion: Converts Phoenix's internal Message TypedDicts (with MessageRole enums) to LangChain-native message objects (HumanMessage, AIMessage, SystemMessage). Falls back to langchain_community.adapters.openai.convert_openai_messages() for plain dict formats.
  • Capability detection: For structured output, the adapter dynamically checks the model's capabilities:
    • with_structured_output for native structured output
    • bind_tools or bind_functions for tool-based extraction
  • Graceful degradation: Under AUTO mode, the adapter attempts structured output first, then falls back to tool calling, logging warnings on each failure.
  • Schema normalization: Adds title and description fields to schemas when missing, as LangChain's structured output requires them.

Usage

from langchain_openai import ChatOpenAI
from phoenix.evals.llm.adapters.langchain.adapter import LangChainModelAdapter

# Create a LangChain model and wrap it
llm = ChatOpenAI(model="gpt-4o", api_key="sk-...")
adapter = LangChainModelAdapter(client=llm, model="gpt-4o")

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

# Structured output
schema = {
    "type": "object",
    "properties": {
        "answer": {"type": "string"},
        "confidence": {"type": "number"}
    },
    "required": ["answer", "confidence"]
}
result = adapter.generate_object("What is 2+2?", schema=schema)
from langchain_anthropic import ChatAnthropic

# Works with Anthropic LangChain models too
llm = ChatAnthropic(model="claude-sonnet-4-20250514")
adapter = LangChainModelAdapter(client=llm, model="claude-sonnet-4-20250514")

# Async text generation
response = await adapter.async_generate_text("Summarize this.")

Code Reference

Symbol Kind Location Lines
identify_langchain_client() Function packages/phoenix-evals/src/phoenix/evals/llm/adapters/langchain/adapter.py 17-25
get_anthropic_langchain_rate_limit_errors() Function packages/phoenix-evals/src/phoenix/evals/llm/adapters/langchain/adapter.py 28-31
get_openai_langchain_rate_limit_errors() Function packages/phoenix-evals/src/phoenix/evals/llm/adapters/langchain/adapter.py 34-37
LangChainModelAdapter Class packages/phoenix-evals/src/phoenix/evals/llm/adapters/langchain/adapter.py 56-436
LangChainModelAdapter.generate_text() Method packages/phoenix-evals/src/phoenix/evals/llm/adapters/langchain/adapter.py 72-87
LangChainModelAdapter.async_generate_text() Method packages/phoenix-evals/src/phoenix/evals/llm/adapters/langchain/adapter.py 89-106
LangChainModelAdapter.generate_object() Method packages/phoenix-evals/src/phoenix/evals/llm/adapters/langchain/adapter.py 108-186
LangChainModelAdapter.async_generate_object() Method packages/phoenix-evals/src/phoenix/evals/llm/adapters/langchain/adapter.py 188-273
LangChainModelAdapter._transform_messages_to_langchain() Method packages/phoenix-evals/src/phoenix/evals/llm/adapters/langchain/adapter.py 288-330
LangChainModelAdapter._normalize_schema_for_langchain() Method packages/phoenix-evals/src/phoenix/evals/llm/adapters/langchain/adapter.py 408-417

I/O Contract

generate_text / async_generate_text

Direction Type Description
Input Union[PromptLike, MultimodalPrompt] A string prompt, list of message dicts, list of Message TypedDicts, or legacy MultimodalPrompt
Input **kwargs Additional keyword arguments forwarded to the LangChain model's invoke() or ainvoke() method
Output str The string content from the LangChain response (from .content attribute or str() fallback)

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 defining the expected output structure
Input ObjectGenerationMethod AUTO (default: tries structured output then tool calling), STRUCTURED_OUTPUT, or TOOL_CALLING
Output Dict[str, Any] A dictionary from with_structured_output or extracted from tool_calls[0].args
Raises ValueError If the model supports neither structured output nor tool calling, or both methods fail

Usage Examples

LangChain Method Dispatch

The adapter uses a priority chain for text generation:

# Priority: invoke() -> predict() -> __call__()
# For async: ainvoke() -> apredict() -> sync fallback

llm = ChatOpenAI(model="gpt-4o")
adapter = LangChainModelAdapter(client=llm, model="gpt-4o")

# Uses llm.invoke() internally
result = adapter.generate_text("Hello")

# Uses llm.ainvoke() internally
result = await adapter.async_generate_text("Hello")

Model Name Resolution

# The adapter resolves model name from the LangChain model
adapter = LangChainModelAdapter(client=llm, model="gpt-4o")
print(adapter.model_name)  # "gpt-4o" (from client.model_name or client.model)

Related Pages

Page Connections

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