|
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 | +from sentry_sdk.utils import capture_internal_exceptions, parse_version |
5 | 5 |
|
6 | 6 | try: |
7 | 7 | import pydantic_ai # type: ignore # noqa: F401 |
|
10 | 10 | raise DidNotEnable("pydantic-ai not installed") |
11 | 11 |
|
12 | 12 |
|
| 13 | +from importlib.metadata import PackageNotFoundError, version |
13 | 14 | from typing import TYPE_CHECKING |
14 | 15 |
|
15 | 16 | from .patches import ( |
@@ -128,7 +129,7 @@ class PydanticAIIntegration(Integration): |
128 | 129 |
|
129 | 130 | identifier = "pydantic_ai" |
130 | 131 | origin = f"auto.ai.{identifier}" |
131 | | - are_request_hooks_available = True |
| 132 | + using_request_hooks = False |
132 | 133 |
|
133 | 134 | def __init__( |
134 | 135 | self, include_prompts: bool = True, handled_tool_call_exceptions: bool = True |
@@ -158,15 +159,29 @@ def setup_once() -> None: |
158 | 159 | _patch_agent_run() |
159 | 160 | _patch_tool_execution() |
160 | 161 |
|
| 162 | + PydanticAIIntegration.using_request_hooks = False |
161 | 163 | try: |
162 | | - from pydantic_ai.capabilities import Hooks |
163 | | - except ImportError: |
164 | | - Hooks = None |
165 | | - PydanticAIIntegration.are_request_hooks_available = False |
| 164 | + PYDANTIC_AI_VERSION = version("pydantic-ai-slim") |
| 165 | + except PackageNotFoundError: |
| 166 | + return |
| 167 | + |
| 168 | + PYDANTIC_AI_VERSION = parse_version(PYDANTIC_AI_VERSION) |
| 169 | + if PYDANTIC_AI_VERSION is None: |
| 170 | + return |
166 | 171 |
|
167 | | - if Hooks is None: |
| 172 | + # ModelRequestContext.model added in https://github.com/pydantic/pydantic-ai/commit/f1260dfe09907f17688eee1646daf898fc428d4c |
| 173 | + if PYDANTIC_AI_VERSION < ( |
| 174 | + 1, |
| 175 | + 73, |
| 176 | + ): |
168 | 177 | _patch_graph_nodes() |
169 | 178 | return |
170 | 179 |
|
| 180 | + try: |
| 181 | + from pydantic_ai.capabilities import Hooks |
| 182 | + except ImportError: |
| 183 | + return |
| 184 | + |
| 185 | + PydanticAIIntegration.using_request_hooks = True |
171 | 186 | hooks = Hooks() |
172 | 187 | register_hooks(hooks) |
0 commit comments