|
1 | 1 | import functools |
2 | 2 |
|
3 | 3 | from sentry_sdk.integrations import DidNotEnable, Integration |
| 4 | +from sentry_sdk.utils import capture_internal_exceptions |
4 | 5 |
|
5 | 6 | try: |
6 | 7 | import pydantic_ai # type: ignore # noqa: F401 |
|
24 | 25 | from typing import Any |
25 | 26 | from pydantic_ai import ModelRequestContext, RunContext |
26 | 27 | from pydantic_ai.messages import ModelResponse # type: ignore |
| 28 | + from pydantic_ai.capabilities import Hooks # type: ignore |
| 29 | + |
| 30 | + |
| 31 | +def register_hooks(hooks: "Hooks"): |
| 32 | + """ |
| 33 | + Creates hooks for chat model calls and register the hooks by adding the hooks to the `capabilities` argument passed to `Agent.__init__()`. |
| 34 | + """ |
| 35 | + |
| 36 | + @hooks.on.before_model_request # type: ignore |
| 37 | + async def on_request( |
| 38 | + ctx: "RunContext[None]", request_context: "ModelRequestContext" |
| 39 | + ) -> "ModelRequestContext": |
| 40 | + span = ai_client_span( |
| 41 | + messages=request_context.messages, |
| 42 | + agent=None, |
| 43 | + model=request_context.model, |
| 44 | + model_settings=request_context.model_settings, |
| 45 | + ) |
| 46 | + run_context_metadata = ctx.metadata |
| 47 | + if isinstance(run_context_metadata, dict): |
| 48 | + run_context_metadata["_sentry_span"] = span |
| 49 | + |
| 50 | + span.__enter__() |
| 51 | + |
| 52 | + return request_context |
| 53 | + |
| 54 | + @hooks.on.after_model_request # type: ignore |
| 55 | + async def on_response( |
| 56 | + ctx: "RunContext[None]", |
| 57 | + *, |
| 58 | + request_context: "ModelRequestContext", |
| 59 | + response: "ModelResponse", |
| 60 | + ) -> "ModelResponse": |
| 61 | + run_context_metadata = ctx.metadata |
| 62 | + if not isinstance(run_context_metadata, dict): |
| 63 | + return response |
| 64 | + |
| 65 | + span = run_context_metadata.pop("_sentry_span", None) |
| 66 | + if span is None: |
| 67 | + return response |
| 68 | + |
| 69 | + update_ai_client_span(span, response) |
| 70 | + span.__exit__(None, None, None) |
| 71 | + |
| 72 | + return response |
| 73 | + |
| 74 | + @hooks.on.model_request_error # type: ignore |
| 75 | + async def on_error( |
| 76 | + ctx: "RunContext[None]", |
| 77 | + *, |
| 78 | + request_context: "ModelRequestContext", |
| 79 | + error: "Exception", |
| 80 | + ) -> "ModelResponse": |
| 81 | + run_context_metadata = ctx.metadata |
| 82 | + |
| 83 | + if not isinstance(run_context_metadata, dict): |
| 84 | + raise error |
| 85 | + |
| 86 | + span = run_context_metadata.pop("_sentry_span", None) |
| 87 | + if span is None: |
| 88 | + raise error |
| 89 | + |
| 90 | + with capture_internal_exceptions(): |
| 91 | + span.__exit__(type(error), error, error.__traceback__) |
| 92 | + |
| 93 | + raise error |
| 94 | + |
| 95 | + original_init = Agent.__init__ |
| 96 | + |
| 97 | + @functools.wraps(original_init) |
| 98 | + def patched_init(self: "Agent[Any, Any]", *args: "Any", **kwargs: "Any") -> None: |
| 99 | + caps = list(kwargs.get("capabilities") or []) |
| 100 | + caps.append(hooks) |
| 101 | + kwargs["capabilities"] = caps |
| 102 | + return original_init(self, *args, **kwargs) |
| 103 | + |
| 104 | + Agent.__init__ = patched_init |
27 | 105 |
|
28 | 106 |
|
29 | 107 | class PydanticAIIntegration(Integration): |
| 108 | + """ |
| 109 | + Typical interaction with the library: |
| 110 | + 1. The user creates an Agent instance with configuration, including system instructions sent to every model call. |
| 111 | + 2. The user calls `Agent.run()` or `Agent.run_stream()` to start an agent run. The latter can be used to incrementally receive progress. |
| 112 | + - Each run invocation has `RunContext` objects that are passed to the library hooks. |
| 113 | + 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. |
| 114 | +
|
| 115 | + Internally, Pydantic AI maintains an execution graph in which ModelRequestNode are responsible for model calls, including retries. |
| 116 | + Hooks created with the decorators provided by `pydantic_ai.capabilities` are used to create spans for model calls when these hooks are available (newer library versions). |
| 117 | + The span is created in `on_request` and stored in the metadata of the shared `RunContext` object that is passed to `on_response` and `on_error`. |
| 118 | +
|
| 119 | + The metadata dictionary on the RunContext instance is initialized with `{"_sentry_span": None}` in the `_create_run_wrapper()` and `_create_streaming_wrapper()` wrappers that |
| 120 | + instrument `Agent.run()` and `Agent.run_stream()`, respectively. A non-empty dictionary is required for the metadata object to be a shared reference between hooks. |
| 121 | + """ |
| 122 | + |
30 | 123 | identifier = "pydantic_ai" |
31 | 124 | origin = f"auto.ai.{identifier}" |
32 | 125 | are_request_hooks_available = True |
@@ -70,73 +163,5 @@ def setup_once() -> None: |
70 | 163 | _patch_model_request() |
71 | 164 | return |
72 | 165 |
|
73 | | - # Assumptions: |
74 | | - # - Model requests within a run are sequential. |
75 | | - # - ctx.metadata is a shared dictionary instance between hooks. |
76 | 166 | hooks = Hooks() |
77 | | - |
78 | | - @hooks.on.before_model_request # type: ignore |
79 | | - async def on_request( |
80 | | - ctx: "RunContext[None]", request_context: "ModelRequestContext" |
81 | | - ) -> "ModelRequestContext": |
82 | | - span = ai_client_span( |
83 | | - messages=request_context.messages, |
84 | | - agent=None, |
85 | | - model=request_context.model, |
86 | | - model_settings=request_context.model_settings, |
87 | | - ) |
88 | | - run_context_metadata = ctx.metadata |
89 | | - if isinstance(run_context_metadata, dict): |
90 | | - run_context_metadata["_sentry_span"] = span |
91 | | - |
92 | | - span.__enter__() |
93 | | - |
94 | | - return request_context |
95 | | - |
96 | | - @hooks.on.after_model_request # type: ignore |
97 | | - async def on_response( |
98 | | - ctx: "RunContext[None]", |
99 | | - *, |
100 | | - request_context: "ModelRequestContext", |
101 | | - response: "ModelResponse", |
102 | | - ) -> "ModelResponse": |
103 | | - run_context_metadata = ctx.metadata |
104 | | - if not isinstance(run_context_metadata, dict): |
105 | | - return response |
106 | | - |
107 | | - span = run_context_metadata["_sentry_span"] |
108 | | - if span is None: |
109 | | - return response |
110 | | - |
111 | | - update_ai_client_span(span, response) |
112 | | - span.__exit__(None, None, None) |
113 | | - del run_context_metadata["_sentry_span"] |
114 | | - |
115 | | - return response |
116 | | - |
117 | | - @hooks.on.model_request_error # type: ignore |
118 | | - async def on_error( |
119 | | - ctx: "RunContext[None]", |
120 | | - *, |
121 | | - request_context: "ModelRequestContext", |
122 | | - error: "Exception", |
123 | | - ) -> "ModelResponse": |
124 | | - run_context_metadata = ctx.metadata |
125 | | - if isinstance(run_context_metadata, dict): |
126 | | - span = run_context_metadata.pop("_sentry_span", None) |
127 | | - if span is not None: |
128 | | - span.__exit__(type(error), error, error.__traceback__) |
129 | | - raise error |
130 | | - |
131 | | - original_init = Agent.__init__ |
132 | | - |
133 | | - @functools.wraps(original_init) |
134 | | - def patched_init( |
135 | | - self: "Agent[Any, Any]", *args: "Any", **kwargs: "Any" |
136 | | - ) -> None: |
137 | | - caps = list(kwargs.get("capabilities") or []) |
138 | | - caps.append(hooks) |
139 | | - kwargs["capabilities"] = caps |
140 | | - return original_init(self, *args, **kwargs) |
141 | | - |
142 | | - Agent.__init__ = patched_init |
| 167 | + register_hooks(hooks) |
0 commit comments