pydanticai-docs
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePydantic AI Documentation Skill
Pydantic AI 文档技能
What is Pydantic AI?
什么是Pydantic AI?
Pydantic AI is a production-grade Python agent framework for building type-safe, dependency-injected Generative AI applications. It supports multiple LLM providers, structured outputs via Pydantic models, and composable multi-agent patterns.
Pydantic AI是一款用于构建类型安全、依赖注入式生成AI应用的生产级Python Agent框架。它支持多LLM提供商、通过Pydantic模型实现的结构化输出,以及可组合的多Agent模式。
Core Concepts
核心概念
1. Agent Instantiation
1. Agent实例化
python
from pydantic_ai import Agent
agent = Agent(
'openai:gpt-4o', # model string: provider:model-name
system_prompt='Be helpful.',
)
result = agent.run_sync('What is the capital of France?')
print(result.output)For full constructor parameters, run methods, and streaming: load .
references/AGENT.mdpython
from pydantic_ai import Agent
agent = Agent(
'openai:gpt-4o', # model string: provider:model-name
system_prompt='Be helpful.',
)
result = agent.run_sync('What is the capital of France?')
print(result.output)有关完整的构造函数参数、运行方法和流式处理,请加载。
references/AGENT.md2. Function Tools (@agent.tool
)
@agent.tool2. 函数工具(@agent.tool
)
@agent.toolpython
from pydantic_ai import Agent, RunContext
agent = Agent('openai:gpt-4o', deps_type=str)
@agent.tool
def get_user_name(ctx: RunContext[str]) -> str:
"""Return the current user's name."""
return ctx.deps
result = agent.run_sync('What is my name?', deps='Alice')Use when you don't need . For tool registration, return types, and retries: load .
@agent.tool_plainRunContextreferences/FUNCTION_TOOLS.mdpython
from pydantic_ai import Agent, RunContext
agent = Agent('openai:gpt-4o', deps_type=str)
@agent.tool
def get_user_name(ctx: RunContext[str]) -> str:
"""Return the current user's name."""
return ctx.deps
result = agent.run_sync('What is my name?', deps='Alice')当不需要时,请使用。有关工具注册、返回类型和重试,请加载。
RunContext@agent.tool_plainreferences/FUNCTION_TOOLS.md3. Dependency Injection (RunContext
)
RunContext3. 依赖注入(RunContext
)
RunContextpython
from dataclasses import dataclass
from pydantic_ai import Agent, RunContext
@dataclass
class MyDeps:
api_key: str
user_id: int
agent = Agent('openai:gpt-4o', deps_type=MyDeps)
@agent.tool
async def fetch_data(ctx: RunContext[MyDeps]) -> str:
return f'User {ctx.deps.user_id}'For fields, injection into system prompts and output validators: load .
RunContextreferences/DEPENDENCIES.mdpython
from dataclasses import dataclass
from pydantic_ai import Agent, RunContext
@dataclass
class MyDeps:
api_key: str
user_id: int
agent = Agent('openai:gpt-4o', deps_type=MyDeps)
@agent.tool
async def fetch_data(ctx: RunContext[MyDeps]) -> str:
return f'User {ctx.deps.user_id}'有关字段、注入到系统提示和输出验证器的内容,请加载。
RunContextreferences/DEPENDENCIES.md4. Structured Output
4. 结构化输出
python
from pydantic import BaseModel
from pydantic_ai import Agent
class CityInfo(BaseModel):
city: str
country: str
agent = Agent('openai:gpt-4o', output_type=CityInfo)
result = agent.run_sync('Where were the 2012 Olympics held?')
print(result.output) # CityInfo(city='London', country='United Kingdom')For union types, plain scalars, , and partial validation: load .
output_validatorreferences/OUTPUT.mdpython
from pydantic import BaseModel
from pydantic_ai import Agent
class CityInfo(BaseModel):
city: str
country: str
agent = Agent('openai:gpt-4o', output_type=CityInfo)
result = agent.run_sync('Where were the 2012 Olympics held?')
print(result.output) # CityInfo(city='London', country='United Kingdom')有关联合类型、普通标量、和部分验证,请加载。
output_validatorreferences/OUTPUT.mdAdditional Topics
其他主题
For these topics, load the named reference file or follow the doc link — no implementation code is provided here.
对于这些主题,请加载指定的参考文件或访问文档链接——此处不提供实现代码。
| 主题 | 参考文件 | 文档链接 |
|---|---|---|
| 消息历史 / 多轮对话 | | https://ai.pydantic.dev/message-history/index.md |
| 模型/提供商设置(所有提供商) | | https://ai.pydantic.dev/models/overview/index.md |
工具集( | | https://ai.pydantic.dev/toolsets/index.md |
| MCP服务器集成 | | https://ai.pydantic.dev/mcp/client/index.md |
| 多Agent应用 | 仅文档链接 | https://ai.pydantic.dev/multi-agent-applications/index.md |
| 图(pydantic-graph) | 仅文档链接 | https://ai.pydantic.dev/graph/index.md |
| 评估(pydantic-evals) | 仅文档链接 | https://ai.pydantic.dev/evals/index.md |
| 持久执行 | 仅文档链接 | https://ai.pydantic.dev/durable_execution/overview/index.md |
| 重试 | 仅文档链接 | https://ai.pydantic.dev/retries/index.md |
测试( | 仅文档链接 | https://ai.pydantic.dev/testing/index.md |
| Logfire集成 | 仅文档链接 | https://ai.pydantic.dev/logfire/index.md |
| 内置工具 | 仅文档链接 | https://ai.pydantic.dev/builtin-tools/index.md |
| 流式处理 | 仅文档链接 | https://ai.pydantic.dev/agent/index.md |
Agent Behavior Rules
Agent行为规则
- Default to this file — answer from core concepts first; load only the specific relevant to the user's question when more depth is needed.
references/<CONCEPT>.md - Never fabricate API details — always end with "For details, see: <URL>" using a link from the official index above.
- No implementation code for non-core topics — return a doc link only for topics listed in the Additional Topics table.
- Prefer specificity — route to the most specific page (e.g., ) when the user's question targets a specific provider, not the overview.
models/anthropic/index.md - Out of scope — do not debug user code passively, do not generate full production agent implementations, do not answer questions unrelated to the Pydantic AI ecosystem.
- 默认使用此文件——优先从核心概念中回答;当需要更深入的内容时,仅加载与用户问题相关的特定。
references/<CONCEPT>.md - 切勿编造API细节——始终以“详情请见:<URL>”结尾,并使用上述官方索引中的链接。
- 非核心主题不提供实现代码——对于“其他主题”表格中列出的主题,仅返回文档链接。
- 优先选择特定内容——当用户的问题针对特定提供商时,引导至最具体的页面(例如),而非概览页面。
models/anthropic/index.md - 超出范围的内容——不要被动调试用户代码,不要生成完整的生产级Agent实现,不要回答与Pydantic AI生态系统无关的问题。
Source
来源
Refreshed from on 2026-05-16.
Refresh with from the conejo-skills repo.
<!-- vendored:end -->
<!-- cross-ref:start -->dougtrajano/pydantic-ai-skills./scripts/update-vendored.sh pydanticai-docs于2026-05-16从更新。
从conejo-skills仓库使用进行更新。
<!-- vendored:end -->
<!-- cross-ref:start -->dougtrajano/pydantic-ai-skills./scripts/update-vendored.sh pydanticai-docsSee also (related skills — Pydantic AI family)
另请参阅(相关技能——Pydantic AI系列)
If your issue relates to:
- main Pydantic AI guide with reference files (start here) — check if appropriate.
pydantic-ai-agent-builder - common mistakes and debugging Pydantic AI agents — check if appropriate.
pydantic-ai-common-pitfalls - RunContext, deps_type, dependency injection patterns — check if appropriate.
pydantic-ai-dependency-injection - configuring providers, fallback models, streaming, settings — check if appropriate.
pydantic-ai-model-integration - TestModel, FunctionModel, VCR cassettes, inline snapshots — check if appropriate.
pydantic-ai-testing - registering tools, function calling, ctx handling — check if appropriate.
pydantic-ai-tool-system
如果你的问题涉及:
- 包含参考文件的Pydantic AI主指南(从这里开始)——如果合适,请查看。
pydantic-ai-agent-builder - Pydantic AI Agent的常见错误与调试——如果合适,请查看。
pydantic-ai-common-pitfalls - RunContext、deps_type、依赖注入模式——如果合适,请查看。
pydantic-ai-dependency-injection - 配置提供商、 fallback模型、流式处理、设置——如果合适,请查看。
pydantic-ai-model-integration - TestModel、FunctionModel、VCR cassettes、内联快照——如果合适,请查看。
pydantic-ai-testing - 注册工具、函数调用、ctx处理——如果合适,请查看。
pydantic-ai-tool-system