Comparisons / DSPy vs Pydantic AI
DSPy vs Pydantic AI: Which Agent Framework to Use?
DSPy vs Pydantic AI, head to head
DSPy and Pydantic AI both let you build an agent, but they sit in different parts of the stack and they assume different things about who's writing the code.
DSPy replaces hand-written prompts with compiled modules.
Pydantic AI is a type-safe agent framework built by the Pydantic team.
Underneath, both wrap the same thing: a model call, a tool dispatch, a loop. The decision is about which abstraction your team wants to think in day to day, and which ecosystem you're willing to inherit along with it. There's an honest, framework-free version of the same pattern in about 60 lines of Python in the lesson at the bottom of this page — useful as a baseline regardless of which framework wins.
Pick DSPy if
Pick DSPy if dSPy's real innovation is automated prompt optimization — replacing manual prompt engineering with algorithmic tuning. This is genuinely novel and valuable for production systems where prompt quality matters at scale. For simple agents or learning, hand-written prompts are easier to understand and modify. The tradeoffs in its intro should match how your team already thinks about agents; Pydantic AI will feel like translation if they don't.
Pick Pydantic AI if
Pick Pydantic AI if pydantic AI adds genuine value if you want compile-time type checking across your agent's tools, outputs, and dependencies. If you already use Pydantic in your stack, it fits naturally. But the core agent logic — loop, dispatch, validate — is still ~60 lines of Python you can own entirely. The tradeoffs in its intro should match how your team already thinks about agents; DSPy will feel like translation if they don't.
By the numbers
By the numbers
DSPy
33.4k
2.8k
Python
MIT
2023-01-09
Stanford NLP (Omar Khattab)
Pydantic AI
16.1k
1.9k
Python
MIT
2024-06-21
Pydantic (Samuel Colvin)
GitHub stats as of April 2026. Stars indicate community interest, not necessarily quality or fit for your use case.
| Concept | DSPy | Pydantic AI |
|---|---|---|
| Agent | `dspy.ReAct` module with signature and tools | `Agent()` class with typed `result_type`, system prompt, and `model` parameter |
| Prompts | `dspy.Signature` defines input/output fields, compiled to optimized prompts | — |
| Optimization | `dspy.BootstrapFewShot`, `MIPROv2` auto-tune prompts against a metric | — |
| Tools | Tools passed to `ReAct` module as callable list | `@agent.tool` decorator with typed parameters and Pydantic validation |
| Chaining | `dspy.ChainOfThought`, `dspy.Module` with `forward()` composition | — |
| Evaluation | `dspy.Evaluate` with metric functions and dev sets | — |
| Agent Loop | — | `agent.run()` handles the tool-call loop internally with typed dispatch |
| Structured Output | — | `result_type=MyModel` enforces Pydantic model on final LLM response |
| Model Switching | — | Swap `model='openai:gpt-4o'` to `model='anthropic:claude-sonnet'` in one line |
| Dependencies | — | `RunContext[DepsType]` injects typed dependencies into tools at runtime |
Or build your own in 60 lines
Both DSPy and Pydantic AI implement the same 8 patterns. An agent is a function. Tools are a dict. The loop is a while loop. The whole thing composes in ~60 lines of Python.
No framework. No dependencies. No opinions. Just the code.
Build it from scratch →