Skip to main content

When to use Pydantic AI

  • You want type-safe agent development
  • You prefer Pydantic’s validation and schema generation
  • You want a simpler API than LangChain
  • You need structured outputs from your agents

Create an agent

rayai create-agent my_agent --framework pydantic

Run Pydantic AI tools on Ray

Wrap a Pydantic AI Tool to execute on Ray workers:
from pydantic_ai import Agent, Tool
from rayai.adapters import from_pydantic_tool

# Existing Pydantic AI Tool
my_tool = Tool(some_function, name="my_tool")

# Wrap for Ray execution
ray_tool = from_pydantic_tool(my_tool, num_cpus=2)

# Use directly with Pydantic AI agent
agent = Agent("openai:gpt-4o-mini", tools=[ray_tool])

Use Ray tools with Pydantic AI

Wrap Ray tools for use with Pydantic AI agents:
from pydantic_ai import Agent
from rayai import tool
from rayai.adapters import RayToolWrapper, AgentFramework

@tool(desc="Search the web", num_cpus=1)
def search_web(query: str) -> str:
    return f"Results for: {query}"

# Wrap for Pydantic AI
wrapper = RayToolWrapper(framework=AgentFramework.PYDANTIC)
pydantic_tools = wrapper.wrap_tools([search_web])

# Use with Pydantic AI agent
agent = Agent("openai:gpt-4o-mini", tools=pydantic_tools)

Cross-framework tools

Use tools from any framework with your Pydantic AI agents. RayToolWrapper auto-detects the source framework and converts tools to Pydantic AI-compatible callables:
from langchain_community.tools import DuckDuckGoSearchRun
from pydantic_ai import Agent
from rayai.adapters import RayToolWrapper, AgentFramework

# LangChain tool
langchain_search = DuckDuckGoSearchRun()

# Plain Python function
def calculate(a: int, b: int) -> int:
    """Add two numbers together."""
    return a + b

# Convert ANY tools to Pydantic AI format
wrapper = RayToolWrapper(framework=AgentFramework.PYDANTIC)
tools = wrapper.wrap_tools([langchain_search, calculate], num_cpus=1)

# Use with Pydantic AI agent - all tools execute on Ray
agent = Agent("openai:gpt-4o-mini", tools=tools)
The wrapper auto-detects: Ray @tool functions, LangChain BaseTool, Pydantic AI Tool, and plain Python callables. Type annotations and docstrings are preserved for schema generation.

Next steps