Skip to content

Upgrade pydantic-ai to 2.x #180

Description

@benh

Summary

Reboot pins pydantic-ai-slim==1.87.0 (reboot/requirements.in), which pins pydantic-graph==1.87.0. pydantic-ai 2.0 is stable and upstream has moved on (latest is 2.43.0). We should upgrade, both to keep up and to pick up fixes we're currently working around.

Why now

pydantic-graph 1.87.0 lets a cancelled agent run surface as anyio.ClosedResourceError instead of asyncio.CancelledError: a tracked task sends its result into a stream the cancelled run has already closed, and only BrokenResourceError is ignored there. A workflow that catches everything except cancellation then keeps running, which made //tests/reboot/examples/agent-wiki:test hang at shutdown. #179 works around it in _agent_run. The fix upstream first ships in pydantic-graph 2.2.0 (no 1.x release has it); once we're on it, the workaround in #179 can be removed.

Picking a target

pydantic-ai / pydantic-graph ClosedResourceError fix HTTP client Anthropic SDK (anthropic extra)
1.87.0 (current) – 1.99.0, 2.0.0 – 2.1.0 no httpx 0.x
2.2.0 – 2.30.x yes httpx 0.x
2.35.0 – 2.43.0 yes httpx2 >= 1.3
  • 2.2.0 – 2.30.x keeps reboot[anthropic] on anthropic==0.125.0 (per requirements.in / requirements-anthropic.in, Anthropic SDK 1.x rejects the httpx client pydantic-ai hands it before the httpx2 switch).
  • 2.35+ also means moving the anthropic extra to SDK 1.x and httpx2.

A reasonable path is to land 2.30.x first, then take the httpx2 / Anthropic SDK 1.x step separately.

What it takes

  1. Port Reboot's wrapper (reboot/agents/pydantic_ai/, ~2,300 lines) — most of the work.
    • Of the 50 names it imports from pydantic-ai, against both 2.2.0 and 2.43.0: pydantic_ai.tools.AgentBuiltinTool no longer exists and pydantic_ai.mcp.MCPServer no longer resolves; CompletedStreamedResponse moved from pydantic_ai.models.wrapper to pydantic_ai.models (deprecated in 2.43). pydantic_ai.toolsets.fastmcp wasn't checked with the fastmcp extra installed.
    • _agent.py mirrors the full signatures of run / iter / run_stream / run_stream_events, and wraps toolsets (_toolset.py) and models (_model.py). V2 is a harness-first redesign around capabilities (see the upgrade guide), so signatures, toolset wrapping and model wrapping all need checking against 2.x, not just imports.
  2. Pins and lock: pydantic-ai-slim (and possibly anthropic) in reboot/requirements.in and reboot/requirements-anthropic.in; regenerate reboot/requirements_lock.txt.
  3. Remove the Agents: raise a cancelled run's ClosedResourceError as a cancellation #179 workaround in reboot/agents/pydantic_ai/_run.py (_raised_while_cancelling and the ClosedResourceError conversion) and its CancellationTestCase tests, or keep the tests asserting a cancelled run raises CancelledError.
  4. Docs and examples: the Python skill references (reboot/plugin/skills/python/references/agent-pydantic-ai.md, agent-tools.md, testing-external-context.md), and examples that use agents (e.g. reboot/examples/agent-wiki, whose uv.lock resolves pydantic-ai-slim).
  5. Dashboard: the static analysis in reboot/dashboard/backend/code_watcher.py recognizes Agent's entry points and tool registration by name (RUN_NAMES, TOOL_REGISTRATION_NAMES, constructor and run arguments); check those still match 2.x, and its test fixture of the agents module in tests/reboot/dashboard/code_watcher_tests.py.

Testing

  • //tests/reboot/agents/pydantic_ai/...
  • //tests/reboot/examples/agent-wiki:test and other agent examples
  • //tests/reboot/dashboard:code_watcher_tests_py

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions