pydantic-ai-harness
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBuilding with Pydantic AI Harness
使用Pydantic AI Harness进行开发
Pydantic AI Harness is the official capability library for Pydantic AI. Capabilities that need model or
framework support — and those fundamental to every agent — live in core ; optional,
batteries-included capabilities live here. Both are composed onto an agent through the same
API.
pydantic-aicapabilities=[...]This skill covers the capabilities shipped by . For the core framework — agents,
tools, structured output, hooks, and testing — use the skill instead.
pydantic-ai-harnessbuilding-pydantic-ai-agentsPydantic AI Harness是Pydantic AI的官方功能库。需要模型或框架支持的功能——以及每个Agent必备的基础功能——都位于核心库中;可选的一站式功能则在此库中。两者均可通过相同的 API集成到Agent中。
pydantic-aicapabilities=[...]本技能涵盖提供的功能。若需了解核心框架(Agent、工具、结构化输出、钩子和测试),请使用技能。
pydantic-ai-harnessbuilding-pydantic-ai-agentsWhen to Use This Skill
何时使用本技能
Invoke this skill when:
- The user mentions ,
pydantic-ai-harness, code mode, or the Monty sandboxCodeMode - An agent makes many sequential tool calls that could collapse into one sandboxed Python execution
- The user wants the model to write Python that loops, branches, aggregates, or parallelizes tool calls with
asyncio.gather - The user asks to sandbox or constrain the code an agent runs
Do not use this skill for:
- Core Pydantic AI usage — building agents, adding tools, structured output, streaming, or testing (use )
building-pydantic-ai-agents - Capabilities that ship in core , such as web search, tool search, and thinking
pydantic-ai - The Pydantic validation library on its own (/
pydanticwithout agents)BaseModel
在以下场景调用本技能:
- 用户提及、
pydantic-ai-harness、code mode或Monty沙箱CodeMode - Agent进行多次连续工具调用,可合并为一次沙箱化Python执行
- 用户希望模型编写可通过实现循环、分支、聚合或并行化工具调用的Python代码
asyncio.gather - 用户要求对Agent运行的代码进行沙箱化或限制
请勿在以下场景使用本技能:
- Pydantic AI核心用法——构建Agent、添加工具、结构化输出、流式传输或测试(请使用)
building-pydantic-ai-agents - 核心库提供的功能,如网页搜索、工具搜索和思考功能
pydantic-ai - 单独使用Pydantic验证库(无Agent的/
pydantic)BaseModel
Supported Capabilities
支持的功能
| Capability | Description | Reference |
|---|---|---|
| Wraps eligible tools into a single sandboxed | Code Mode |
More capability areas are tracked in the
capability matrix; as they stabilize,
this skill grows to cover them.
| 功能 | 描述 | 参考文档 |
|---|---|---|
| 将符合条件的工具封装为单个沙箱化 | Code Mode |
更多功能领域在功能矩阵中跟踪;待功能稳定后,本技能将逐步覆盖这些内容。
Install
安装
bash
uv add pydantic-ai-harnessEach capability declares its own extra. Code Mode needs the Monty sandbox:
bash
uv add "pydantic-ai-harness[codemode]" # `code-mode` is also accepted as an aliasRequires Python 3.10+ and .
pydantic-ai-slim>=1.95.1bash
uv add pydantic-ai-harness每个功能都有自己的扩展依赖。Code Mode需要Monty沙箱:
bash
uv add "pydantic-ai-harness[codemode]" # `code-mode`也可作为别名使用要求Python 3.10+和。
pydantic-ai-slim>=1.95.1Quick Start
快速开始
A harness capability is added to the agent like any other. Here wraps an MCP server's tools into
a single tool that the model drives with Python.
CodeModerun_codepython
from pydantic_ai import Agent
from pydantic_ai.capabilities import MCP # MCP ships in core pydantic-ai
from pydantic_ai_harness import CodeMode
agent = Agent(
'anthropic:claude-sonnet-4-6',
capabilities=[
# native=False routes the MCP tools through a local toolset so CodeMode can wrap them.
# Without it, providers with native MCP run the tools server-side and bypass the sandbox.
MCP('https://hn.caseyjhand.com/mcp', native=False),
CodeMode(),
],
)
result = agent.run_sync(
'Across the top and best Hacker News feeds, find the most-discussed story with at '
'least 100 points and summarize its comment thread in one paragraph.'
)
print(result.output)
#> The most-discussed story clearing 100 points is ...Instead of one model round-trip per tool call, the model writes a single Python script that fetches both
feeds with , dedupes and ranks them in plain Python, and pulls the winning thread —
collapsing many calls into one .
asyncio.gatherrun_code可像添加其他功能一样,将Harness功能添加到Agent中。以下示例中,将MCP服务器的工具封装为单个工具,由模型通过Python驱动。
CodeModerun_codepython
from pydantic_ai import Agent
from pydantic_ai.capabilities import MCP # MCP属于pydantic-ai核心库
from pydantic_ai_harness import CodeMode
agent = Agent(
'anthropic:claude-sonnet-4-6',
capabilities=[
# native=False将MCP工具路由到本地工具集,以便CodeMode封装它们。
# 若不设置此参数,支持原生MCP的提供商将在服务器端运行工具,从而绕过沙箱。
MCP('https://hn.caseyjhand.com/mcp', native=False),
CodeMode(),
],
)
result = agent.run_sync(
'在Hacker News的头条和最佳feed中,找到讨论量最多且至少获得100分的文章,并将其评论线程总结为一段话。'
)
print(result.output)
#> 获得100分以上且讨论量最多的文章是 ...无需每次工具调用都进行一轮模型交互,模型只需编写单个Python脚本,通过获取两个feed,用纯Python去重并排序,然后提取最优线程——将多次调用合并为一次执行。
asyncio.gatherrun_codeKey Practices
关键实践
- Confirm a harness capability is actually needed. If core Pydantic AI tools and capabilities are enough, use the skill instead — don't reach for the harness by default.
building-pydantic-ai-agents - Read the reference before writing code. Each capability has its own configuration, constraints, and gotchas — load the linked reference (e.g. Code Mode) first.
- Install the capability's extra. Importing without
CodeModeraises anpydantic-ai-harness[codemode]; the Monty sandbox is an optional dependency.ImportError
- 确认是否确实需要Harness功能。若Pydantic AI核心工具和功能已足够,请使用技能——不要默认使用Harness。
building-pydantic-ai-agents - 编写代码前阅读参考文档。每个功能都有自己的配置、约束和注意事项——请先查看关联的参考文档(如Code Mode)。
- 安装功能对应的扩展依赖。若未安装就导入
pydantic-ai-harness[codemode],会触发CodeMode;Monty沙箱是可选依赖。ImportError
Common Gotchas
常见陷阱
- tools bypass
native=True. Provider-native MCP servers and web search execute server-side, soCodeModenever sees them. Construct them withrun_codeto keep them local and wrappable.native=False - The Monty sandbox is a Python subset. No class definitions, no third-party imports, and only a small stdlib allowlist — read Code Mode before debugging generated code that fails to run.
- needs its extra. Install
CodeMode, not the bare package.pydantic-ai-harness[codemode]
- 的工具会绕过
native=True。提供商原生的MCP服务器和网页搜索在服务器端执行,因此CodeMode无法感知它们。请将其构造为run_code,以保持本地可封装状态。native=False - Monty沙箱仅支持Python子集。不允许类定义、第三方导入,仅支持一小部分标准库白名单——调试无法运行的生成代码前,请阅读Code Mode。
- 需要对应的扩展依赖。请安装
CodeMode,而非基础包。pydantic-ai-harness[codemode]