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
depsthrough graph exceptions and traceback cycles until cyclic collection. The report distinguishes tool failures, cancellation and an exception insideagent.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