You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ref(pydantic-ai): Resolve version divergence in _compat and restructure modules
Introduce _compat.py, which resolves the installed pydantic-ai version and every version-dependent decision once at import time: the hooks-vs-graph-nodes model backend, the ToolManager tool-call method name, and the message part classes (now resolved individually so one upstream rename degrades only the paths needing that class). The using_request_hooks class flag and the circular-import workarounds are gone; setup_once is three composed calls with deferred imports, and importing the package loads nothing but __init__.
Restructure to the target layout: _spans.py absorbs utils.py and the spans/ package, _wrap_agent.py and _wrap_model.py (both model backends behind one install_model_backend()) and _wrap_tools.py (the two duplicated tool wrappers unified into one) replace patches/.
Review-driven fixes folded in: unknown ToolManager method names and a missing _agent_graph module now degrade gracefully instead of crashing sentry_sdk.init(), agent_run_scope removes exactly its own run from the stack so non-LIFO streaming exits cannot corrupt it, the streaming wrapper propagates exception suppression from the wrapped context manager, the after_model_request span close is guarded, and chat spans extract model info once instead of twice.
1. The user creates an Agent instance with configuration, including system instructions sent to every model call.
131
13
2. The user calls `Agent.run()` or `Agent.run_stream()` to start an agent run. The latter can be used to incrementally receive progress.
132
14
3. In a loop, the agent repeatedly calls the model, maintaining a conversation history that includes previous messages and tool results, which is passed to each call.
133
15
134
-
Internally, Pydantic AI maintains an execution graph in which ModelRequestNode are responsible for model calls, including retries.
135
-
Hooks using the decorators provided by `pydantic_ai.capabilities` create and manage spans for model calls when these hooks are available (newer library versions);
136
-
older versions are instrumented by patching the graph nodes directly (see patches/graph_nodes.py).
137
-
138
-
The wrappers around `Agent.run()` and `Agent.run_stream()` track each in-flight run on a contextvar stack (see _run_context.py); the tool patches and span
139
-
helpers read the current agent from there. The request hooks pair each chat span with its model request through the run's `RunContext.metadata` dict
140
-
(see register_hooks), which stays correct per run and also covers entry points the wrappers don't instrument, such as `Agent.iter()`.
16
+
How the integration is put together:
17
+
- _compat.py resolves the installed pydantic-ai version and every version-dependent decision, once, at import time.
18
+
- _extract.py is the only module that reads pydantic-ai object internals; it returns plain data structures.
19
+
- _spans.py creates spans and writes extracted data onto them.
20
+
- _run_context.py tracks each in-flight run on a contextvar stack; the tool wrapper and span helpers read the current agent from there.
- _wrap_model.py emits chat spans for model requests via one of two backends chosen in _compat: request hooks (>= 1.73), paired per run through RunContext.metadata, or graph-node patching (older versions).
23
+
- _wrap_tools.py instruments the single ToolManager method all tool calls flow through (execute_tool spans).
0 commit comments