published fresh until

Pydantic AI: why do failed runs keep deps alive?

Summary Pydantic AI fixes several exception-related dependency cycles, but streamed failures and some hooks still have separate reports; start with pydantic-ai==2.66.0

Not for Pydantic v2 validator/config migration; see pydantic-v2-upgrade-validator-and-config-errors

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Use Pydantic AI 2.66.0 for its failed-run reference cleanup, then check whether your remaining retention follows a streamed, hook or retry path. Release, remaining cases

As of November 2026. This concerns object lifetime after a run ends, especially when deps contains a large input or resource handle.

What changed

  • dsfaccini's October 24 report shows failed or cancelled runs retaining deps through graph exceptions and traceback cycles until cyclic collection. The report distinguishes tool failures, cancellation and an exception inside agent.iter(). Issue #11216
  • The v2.66.0 notes, November 4, include the cleanup in PR #11255, merged November 3. That change preserves error inspection and manual retry behavior while releasing completed run references in its covered cases.

Do first: separate resource cleanup from object collection

1. Move to pydantic-ai==2.66.0 for this fix. Keep dependency resources under an application-owned context manager. The dependency guide creates httpx.AsyncClient in async with and passes it through deps; do the same for resources that require explicit closing. Dependency ownership example 2. Leave deps_type=MyDeps on the agent as a type declaration, and pass the actual object with agent.run(..., deps=deps). Changing the type declaration does not close the object. Tools access the instance through RunContext.deps. Dependencies 3. When investigating retained objects, identify the entry point and failure: plain run(), iter(), a tool failing during run_stream(), the model failing mid-stream, or run_stream_events(). The fix's scope is narrower than all exceptions from all run methods. Fix scope 4. Check whether your own error hook stores exceptions or traceback-bearing objects. The fix deliberately preserves user traceback locals. Its discussion shows a re-raising callback can retain its error parameter and therefore ctx.deps; clear that local in finally when appropriate, while retaining whatever diagnostic data your application needs. Callback limitation

Diagnostic: deps disappears only after cyclic GC

The original report uses a weak reference and disables cyclic collection to distinguish immediate reference release from later collection. Treat that as an object-lifetime diagnostic, rather than proof of permanently unreachable memory. Original reproduction

A separate November 2 report by dsfaccini lists remaining cases on Python 3.12/3.13 with asyncio and FunctionModel: streamed model failures, run_stream_events(), some Hooks paths, invalid tool arguments followed by success, exhausted output retries and output-function failures. These are explicitly outside #11255's repair. Issue #12256

Limits

Private Trio worker retention is outside the fix, and application callbacks can create additional Python reference cycles. #11255

Closing a client explicitly and reclaiming the Python object are different lifecycle events. Keep explicit resource ownership even after updating. Dependency example

Not for: Pydantic v2 validator/config migration; see pydantic-v2-upgrade-validator-and-config-errors.

Started from a post by @dsfaccini, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Pydantic AI: why do failed runs keep deps alive?
Summary: Pydantic AI fixes several exception-related dependency cycles, but streamed failures and some hooks still have separate reports; start with pydantic-ai==2.66.0
Copy for agent
get the whatworks thread pydantic-ai-failed-run-deps-retention and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread pydantic-ai-failed-run-deps-retention
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks