pydantic-ai
Build type-safe AI agents with typed agent frameworks — structured outputs via data models, dependency injection, tool validation, and testable agent code. Use when agent reliability matters and you want types enforcing contracts.
- License 许可
- MIT
- 最近更新
- 2026-11-07
- Stars
- ★ 0
- Forks
- 1
01 前言 Front Matter
- name
- pydantic-ai
- category
- ai-research
- description
- Build type-safe AI agents with typed agent frameworks — structured outputs via data models, dependency injection, tool validation, and testable agent code. Use when agent reliability matters and you want types enforcing contracts.
03 SKILL.md 正文
Type-Safe Agents with Typed Frameworks
Dynamic agent code fails at runtime in ways types could have caught: malformed tool arguments, wrong output shapes, missing dependencies. Typed agent frameworks bring data models, dependency injection, and structured validation to agent building — so contracts are checked by the type system, not by hope.
Overview
The pattern: define your agent's world as types. Tool inputs and outputs are data models with validation. The agent's dependencies (API clients, database handles) are injected through a typed context rather than globals. Model outputs are parsed into models, with retries on validation failure. The result is agent code you can test like normal code — unit-test tools, type-check the wiring, and mock dependencies — instead of integration-testing everything through the model.
When to use
- Agents with many tools where argument mismatches are a real risk.
- Production agents where runtime type errors are unacceptable.
- Codebases where agents must integrate with existing typed services.
- Teams that want agents testable with standard unit-testing practices.
Core concepts
- Data models as contracts: tool parameters and results defined as validated models. The framework validates before the tool runs and after it returns.
- Structured outputs: the model returns data matching a schema; validation failures trigger automatic retry with the error explained. No regex parsing.
- Dependency injection: shared resources (clients, config, state) passed through a typed context object. Tools declare what they need; the framework provides it.
- Tool registration: tools as decorated functions with type hints — the schema is derived from the signature, keeping docs and types in sync automatically.
- Testability: tools are plain functions — unit-test them without the model. Agent logic is testable with mocked model responses.
- Streaming + validation: stream partial results for UX while still validating the final structured output.
Practical workflow
- Model the domain: define data models for every tool's inputs/outputs and the agent's final result type.
- Write tools as typed functions; let the framework derive schemas from signatures.
- Define the dependency context: what resources tools need, injected once, typed throughout.
- Unit-test every tool standalone; test the agent flow with scripted model responses.
- Enable validation-retry on structured outputs; log validation failures — they reveal prompt/schema mismatches.
- Add integration tests hitting the real model on your eval set; types don't replace behavioral testing.
Anatomy of a typed agent:
MODELS: OrderLookupArgs {order_id: str}, OrderStatus {status, eta}
CTX: AppContext {db: Database, config: Settings}
TOOLS: get_order_status(ctx, args: OrderLookupArgs) -> OrderStatus
AGENT: system prompt + tools + result_type=TaskResult
TESTS: unit (tools) + scripted (agent flow) + eval (real model)
Common pitfalls
- Types without validation: annotations that are never checked at runtime. Use validated models at every boundary.
- Over-typed rigidity: modeling every nuance makes tools brittle. Type the contract; keep internals flexible.
- Skipping unit tests: "the types will catch it." Types catch shape errors, not logic errors. Test behavior too.
- Leaky context: stuffing everything into the dependency context. Inject only what tools actually need.
- Validation-retry loops: the model repeatedly failing validation burns tokens. After 2–3 retries, surface the error instead.
- No eval set: type safety is not behavioral correctness. You still need task-level evals with the real model.
04 同仓库更多 Skills
web-scrape
Turn web pages into LLM-ready markdown via the Firecrawl API (renders JS, anti-bot handling). Use when browser_open/fetch fails on JS-heavy pages, or when you need clean markdown/structured extraction from a site. Requires FIRECRAWL_API_KEY.
design-taste-frontend
tasteskill v2, the anti-slop frontend skill. Art direction for websites, distilled from a study of 247 curated gallery sites (One Page Love, Awwwards Sites of the Day). Deals researched directions (idea, composition, type, palette, photographs, section blueprints) from a library, enforces a strict no-slop house style with a real-browser inspector, and runs a look-and-fix loop on screenshots. Use this whenever you build, design, redesign, restyle, polish or review a landing page, marketing site, portfolio, product page, one-pager, hero or any public-facing web page, in any stack (HTML/CSS, React, Next.js, Vue, Svelte, Astro, Tailwind) — also when the user says a page looks generic, templated, "AI-made", sloppy, or asks to make it look designed, premium, award-level or less like every other site. Not for dashboards or app screens.
test-driven-development
Use when implementing any feature or bugfix, before writing implementation code
systematic-debugging
Use when encountering any bug, test failure, or unexpected behavior, before proposing fixes
requesting-code-review
Use when completing tasks, implementing major features, or before merging to verify work meets requirements
brainstorming
You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).