pydantic-ai-model-integration by existential-birds/beagle
npx skills add https://github.com/existential-birds/beagle --skill pydantic-ai-model-integration格式:provider:model-name
from pydantic_ai import Agent
# OpenAI
Agent('openai:gpt-4o')
Agent('openai:gpt-4o-mini')
Agent('openai:o1-preview')
# Anthropic
Agent('anthropic:claude-sonnet-4-5')
Agent('anthropic:claude-haiku-4-5')
# Google (API Key)
Agent('google-gla:gemini-2.0-flash')
Agent('google-gla:gemini-2.0-pro')
# Google (Vertex AI)
Agent('google-vertex:gemini-2.0-flash')
# Groq
Agent('groq:llama-3.3-70b-versatile')
Agent('groq:mixtral-8x7b-32768')
# Mistral
Agent('mistral:mistral-large-latest')
# 其他提供商
Agent('cohere:command-r-plus')
Agent('bedrock:anthropic.claude-3-sonnet')
from pydantic_ai import Agent
from pydantic_ai.settings import ModelSettings
agent = Agent(
'openai:gpt-4o',
model_settings=ModelSettings(
temperature=0.7,
max_tokens=1000,
top_p=0.9,
timeout=30.0, # 请求超时时间
)
)
# 每次运行时覆盖
result = await agent.run(
'生成创意文本',
model_settings=ModelSettings(temperature=1.0)
)
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
为增强鲁棒性而链式调用模型:
from pydantic_ai.models.fallback import FallbackModel
# 按顺序尝试模型直到一个成功
fallback = FallbackModel(
'openai:gpt-4o',
'anthropic:claude-sonnet-4-5',
'google-gla:gemini-2.0-flash'
)
agent = Agent(fallback)
result = await agent.run('你好')
# 自定义备用条件
from pydantic_ai.exceptions import ModelAPIError
def should_fallback(error: Exception) -> bool:
"""仅在遇到速率限制或服务器错误时启用备用。"""
if isinstance(error, ModelAPIError):
return error.status_code in (429, 500, 502, 503)
return False
fallback = FallbackModel(
'openai:gpt-4o',
'anthropic:claude-sonnet-4-5',
fallback_on=should_fallback
)
async def stream_response():
async with agent.run_stream('给我讲个故事') as response:
# 流式输出文本
async for chunk in response.stream_output():
print(chunk, end='', flush=True)
# 流式传输后访问最终结果
print(f"\n使用的令牌数: {response.usage().total_tokens}")
from pydantic import BaseModel
class Story(BaseModel):
title: str
content: str
moral: str
agent = Agent('openai:gpt-4o', output_type=Story)
async with agent.run_stream('写一个寓言故事') as response:
# 对于结构化输出,stream_output 产生部分 JSON
async for partial in response.stream_output():
print(partial) # 解析后的部分 Story 对象
# 最终验证后的结果
story = response.output
import os
# 基于环境变量的选择
model = os.getenv('PYDANTIC_AI_MODEL', 'openai:gpt-4o')
agent = Agent(model)
# 运行时模型覆盖
result = await agent.run(
'你好',
model='anthropic:claude-sonnet-4-5' # 覆盖默认值
)
# 上下文管理器覆盖
with agent.override(model='google-gla:gemini-2.0-flash'):
result = agent.run_sync('你好')
为测试延迟模型验证:
# 默认:立即验证模型(检查环境变量)
agent = Agent('openai:gpt-4o')
# 延迟:仅在第一次运行时验证
agent = Agent('openai:gpt-4o', defer_model_check=True)
# 用于带覆盖的测试
with agent.override(model=TestModel()):
result = agent.run_sync('测试') # 无需 OpenAI 密钥
result = await agent.run('你好')
# 请求使用量(最后一次请求)
usage = result.usage()
print(f"输入令牌数: {usage.input_tokens}")
print(f"输出令牌数: {usage.output_tokens}")
print(f"总令牌数: {usage.total_tokens}")
# 完整运行使用量(运行中的所有请求)
run_usage = result.run_usage()
print(f"总请求数: {run_usage.requests}")
from pydantic_ai.usage import UsageLimits
# 限制令牌使用量
result = await agent.run(
'生成内容',
usage_limits=UsageLimits(
total_tokens=1000,
request_tokens=500,
response_tokens=500,
)
)
from pydantic_ai.models.openai import OpenAIModel
model = OpenAIModel(
'gpt-4o',
api_key='your-key', # 或使用 OPENAI_API_KEY 环境变量
base_url='https://custom-endpoint.com' # 用于 Azure、代理
)
from pydantic_ai.models.anthropic import AnthropicModel
model = AnthropicModel(
'claude-sonnet-4-5',
api_key='your-key' # 或 ANTHROPIC_API_KEY
)
| 使用场景 | 推荐 |
|---|---|
| 通用目的 | openai:gpt-4o 或 anthropic:claude-sonnet-4-5 |
| 快速/廉价 | openai:gpt-4o-mini 或 anthropic:claude-haiku-4-5 |
| 长上下文 | anthropic:claude-sonnet-4-5 (200k) 或 google-gla:gemini-2.0-flash |
| 推理 | openai:o1-preview |
| 成本敏感的生产环境 | 使用 FallbackModel,快速模型优先 |
每周安装量
85
代码仓库
GitHub 星标数
45
首次出现
2026年1月20日
安全审计
安装于
opencode69
gemini-cli67
codex67
claude-code60
github-copilot60
cursor59
Format: provider:model-name
from pydantic_ai import Agent
# OpenAI
Agent('openai:gpt-4o')
Agent('openai:gpt-4o-mini')
Agent('openai:o1-preview')
# Anthropic
Agent('anthropic:claude-sonnet-4-5')
Agent('anthropic:claude-haiku-4-5')
# Google (API Key)
Agent('google-gla:gemini-2.0-flash')
Agent('google-gla:gemini-2.0-pro')
# Google (Vertex AI)
Agent('google-vertex:gemini-2.0-flash')
# Groq
Agent('groq:llama-3.3-70b-versatile')
Agent('groq:mixtral-8x7b-32768')
# Mistral
Agent('mistral:mistral-large-latest')
# Other providers
Agent('cohere:command-r-plus')
Agent('bedrock:anthropic.claude-3-sonnet')
from pydantic_ai import Agent
from pydantic_ai.settings import ModelSettings
agent = Agent(
'openai:gpt-4o',
model_settings=ModelSettings(
temperature=0.7,
max_tokens=1000,
top_p=0.9,
timeout=30.0, # Request timeout
)
)
# Override per-run
result = await agent.run(
'Generate creative text',
model_settings=ModelSettings(temperature=1.0)
)
Chain models for resilience:
from pydantic_ai.models.fallback import FallbackModel
# Try models in order until one succeeds
fallback = FallbackModel(
'openai:gpt-4o',
'anthropic:claude-sonnet-4-5',
'google-gla:gemini-2.0-flash'
)
agent = Agent(fallback)
result = await agent.run('Hello')
# Custom fallback conditions
from pydantic_ai.exceptions import ModelAPIError
def should_fallback(error: Exception) -> bool:
"""Only fallback on rate limits or server errors."""
if isinstance(error, ModelAPIError):
return error.status_code in (429, 500, 502, 503)
return False
fallback = FallbackModel(
'openai:gpt-4o',
'anthropic:claude-sonnet-4-5',
fallback_on=should_fallback
)
async def stream_response():
async with agent.run_stream('Tell me a story') as response:
# Stream text output
async for chunk in response.stream_output():
print(chunk, end='', flush=True)
# Access final result after streaming
print(f"\nTokens used: {response.usage().total_tokens}")
from pydantic import BaseModel
class Story(BaseModel):
title: str
content: str
moral: str
agent = Agent('openai:gpt-4o', output_type=Story)
async with agent.run_stream('Write a fable') as response:
# For structured output, stream_output yields partial JSON
async for partial in response.stream_output():
print(partial) # Partial Story object as parsed
# Final validated result
story = response.output
import os
# Environment-based selection
model = os.getenv('PYDANTIC_AI_MODEL', 'openai:gpt-4o')
agent = Agent(model)
# Runtime model override
result = await agent.run(
'Hello',
model='anthropic:claude-sonnet-4-5' # Override default
)
# Context manager override
with agent.override(model='google-gla:gemini-2.0-flash'):
result = agent.run_sync('Hello')
Delay model validation for testing:
# Default: Validates model immediately (checks env vars)
agent = Agent('openai:gpt-4o')
# Deferred: Validates only on first run
agent = Agent('openai:gpt-4o', defer_model_check=True)
# Useful for testing with override
with agent.override(model=TestModel()):
result = agent.run_sync('Test') # No OpenAI key needed
result = await agent.run('Hello')
# Request usage (last request)
usage = result.usage()
print(f"Input tokens: {usage.input_tokens}")
print(f"Output tokens: {usage.output_tokens}")
print(f"Total tokens: {usage.total_tokens}")
# Full run usage (all requests in run)
run_usage = result.run_usage()
print(f"Total requests: {run_usage.requests}")
from pydantic_ai.usage import UsageLimits
# Limit token usage
result = await agent.run(
'Generate content',
usage_limits=UsageLimits(
total_tokens=1000,
request_tokens=500,
response_tokens=500,
)
)
from pydantic_ai.models.openai import OpenAIModel
model = OpenAIModel(
'gpt-4o',
api_key='your-key', # Or use OPENAI_API_KEY env var
base_url='https://custom-endpoint.com' # For Azure, proxies
)
from pydantic_ai.models.anthropic import AnthropicModel
model = AnthropicModel(
'claude-sonnet-4-5',
api_key='your-key' # Or ANTHROPIC_API_KEY
)
| Use Case | Recommendation |
|---|---|
| General purpose | openai:gpt-4o or anthropic:claude-sonnet-4-5 |
| Fast/cheap | openai:gpt-4o-mini or anthropic:claude-haiku-4-5 |
| Long context | anthropic:claude-sonnet-4-5 (200k) or google-gla:gemini-2.0-flash |
| Reasoning | openai:o1-preview |
Weekly Installs
85
Repository
GitHub Stars
45
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode69
gemini-cli67
codex67
claude-code60
github-copilot60
cursor59
AI Elements:基于shadcn/ui的AI原生应用组件库,快速构建对话界面
67,500 周安装
| Cost-sensitive prod | FallbackModel with fast model first |