Use local Claude Code CLI as a Pydantic AI model provider with full support for structured responses, tools, and streaming
Project description
Pydantic AI Claude Code
Use your local Claude Code CLI as a Pydantic AI model provider.
This package provides a Pydantic AI-compatible model implementation that wraps the local Claude CLI, enabling you to use Claude locally with all Pydantic AI features including structured responses, tool calling, streaming, and multi-turn conversations.
Features
- Full Pydantic AI Compatibility: Drop-in replacement for any Pydantic AI model
- Structured Responses: Get validated, typed responses using Pydantic models
- Custom Tool Calling: Use your own Python functions as tools
- True Streaming: Real-time response streaming via Claude CLI's stream-json mode
- Local Execution: All processing happens locally on your machine
- Session Persistence: Maintain conversation context across multiple requests
- Configurable: Fine-tune permissions, working directories, and tool access
Installation
# Using uv (recommended)
uv add pydantic-ai-claude-code
# Using pip
pip install pydantic-ai-claude-code
Prerequisites: You must have Claude Code CLI installed and authenticated on your system.
Quick Start
Basic Usage (String Format - Simplest!)
import pydantic_ai_claude_code # Register the provider
from pydantic_ai import Agent
# Just use the string format - easiest way!
agent = Agent('claude-code:sonnet')
# Run a simple query
result = agent.run_sync("What is 2+2?")
print(result.output) # Output: 4
Structured Responses
import pydantic_ai_claude_code
from pydantic import BaseModel
from pydantic_ai import Agent
class Analysis(BaseModel):
complexity: int # 1-10
maintainability: str
suggestions: list[str]
# String format works with all Pydantic AI features
agent = Agent('claude-code:sonnet', output_type=Analysis)
result = agent.run_sync("Analyze this code: def foo(): pass")
print(f"Complexity: {result.output.complexity}")
Custom Tools
import pydantic_ai_claude_code
from pydantic_ai import Agent
def get_weather(city: str) -> str:
"""Get weather for a city."""
return f"Weather in {city}: Sunny, 22°C"
agent = Agent(
'claude-code:sonnet',
tools=[get_weather],
)
result = agent.run_sync("What's the weather in Paris?")
print(result.output)
Streaming
import pydantic_ai_claude_code
from pydantic_ai import Agent
agent = Agent('claude-code:sonnet')
async with agent.run_stream('Write a haiku about code') as result:
async for text in result.stream_text():
print(text, end='', flush=True)
Advanced Configuration
When you need fine-grained control, use the explicit model and provider:
from pydantic_ai import Agent
from pydantic_ai_claude_code import ClaudeCodeModel, ClaudeCodeProvider
# Custom provider with specific settings
provider = ClaudeCodeProvider(
working_directory="/path/to/project",
allowed_tools=["Read", "Edit", "Grep"], # Restrict tool access
permission_mode="acceptEdits",
max_turns=10,
use_temp_workspace=False, # Use specific directory instead of /tmp
)
model = ClaudeCodeModel("sonnet", provider=provider)
agent = Agent(model)
result = agent.run_sync("Analyze the codebase structure")
Temporary Workspace
from pydantic_ai_claude_code import ClaudeCodeModel, ClaudeCodeProvider
# Create isolated workspace that auto-cleans
with ClaudeCodeProvider(use_temp_workspace=True) as provider:
model = ClaudeCodeModel("sonnet", provider=provider)
agent = Agent(model)
result = agent.run_sync("Create a test file")
# Workspace automatically cleaned up
Available Models
claude-code:sonnet- Claude 3.5 Sonnet (default, recommended)claude-code:opus- Claude 3 Opus (most capable)claude-code:haiku- Claude 3.5 Haiku (fastest)
Or use full model names like claude-code:claude-sonnet-4-5-20250929
Integration with Existing Projects
Replace your current LLM calls with Claude Code:
Before:
agent = Agent('openai:gpt-4o')
# or
agent = Agent('anthropic:claude-3-5-sonnet-latest')
After:
import pydantic_ai_claude_code # Add this import
agent = Agent('claude-code:sonnet') # Change this line
Everything else stays the same! All your tools, structured outputs, dependencies, and streaming code works identically.
Key Differences from Cloud Providers
| Aspect | Cloud Providers | Claude Code Local |
|---|---|---|
| Execution | Remote API calls | Local on your machine |
| Cost | Per-token pricing | Uses Claude desktop subscription |
| Data Privacy | Data sent to cloud | Data stays local |
| Speed | Network latency | Local execution |
| API Key | Required | Not needed (uses local auth) |
Examples
See the examples/ directory for more demonstrations:
basic_example.py- Simple queries and usage trackingstructured_example.py- Structured output with Pydantic modelsasync_example.py- Async/await usage patternsadvanced_example.py- Custom provider configurationstools_and_streaming.py- Custom tools and streaming responses
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_ai_claude_code-0.2.2.tar.gz.
File metadata
- Download URL: pydantic_ai_claude_code-0.2.2.tar.gz
- Upload date:
- Size: 16.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c105205372caf5dfda768b199f967dc541eb3cfcb5bd13892ebaaa6159ce286
|
|
| MD5 |
d3c5d1ca24848e8530fc7150cabf5047
|
|
| BLAKE2b-256 |
f39c95cc483789812ebc81f6000ae1978fcfd51f55ae3ef1d33d81af74ac2a1c
|
File details
Details for the file pydantic_ai_claude_code-0.2.2-py3-none-any.whl.
File metadata
- Download URL: pydantic_ai_claude_code-0.2.2-py3-none-any.whl
- Upload date:
- Size: 22.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61a5df22cf20269228f411a4e11b9ba98a84a473a3f43d58b819a564b694544f
|
|
| MD5 |
1e941d6430448994e8718e2df4ffa431
|
|
| BLAKE2b-256 |
7b7beab8ad61f7d7ff921611feaabdddac43368b8925f090ab5b62264a657602
|