Skip to content

Commit fb18c21

Browse files
fix(pydantic-ai): Make imports defensive to avoid ModuleNotFoundError (#5135)
Closes #5134
1 parent f945e38 commit fb18c21

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

sentry_sdk/integrations/pydantic_ai/patches/agent_run.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
from functools import wraps
22

33
import sentry_sdk
4+
from sentry_sdk.integrations import DidNotEnable
45

56
from ..spans import invoke_agent_span, update_invoke_agent_span
67
from ..utils import _capture_exception, pop_agent, push_agent
78

89
from typing import TYPE_CHECKING
9-
from pydantic_ai.agent import Agent # type: ignore
10+
11+
try:
12+
from pydantic_ai.agent import Agent # type: ignore
13+
except ImportError:
14+
raise DidNotEnable("pydantic-ai not installed")
1015

1116
if TYPE_CHECKING:
1217
from typing import Any, Callable, Optional

sentry_sdk/integrations/pydantic_ai/patches/graph_nodes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22
from functools import wraps
33

44
import sentry_sdk
5+
from sentry_sdk.integrations import DidNotEnable
56

67
from ..spans import (
78
ai_client_span,
89
update_ai_client_span,
910
)
10-
from pydantic_ai._agent_graph import ModelRequestNode # type: ignore
11+
12+
try:
13+
from pydantic_ai._agent_graph import ModelRequestNode # type: ignore
14+
except ImportError:
15+
raise DidNotEnable("pydantic-ai not installed")
1116

1217
from typing import TYPE_CHECKING
1318

sentry_sdk/integrations/pydantic_ai/patches/model_request.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
from functools import wraps
22
from typing import TYPE_CHECKING
33

4-
from pydantic_ai import models # type: ignore
4+
from sentry_sdk.integrations import DidNotEnable
5+
6+
try:
7+
from pydantic_ai import models # type: ignore
8+
except ImportError:
9+
raise DidNotEnable("pydantic-ai not installed")
510

611
from ..spans import ai_client_span, update_ai_client_span
712

sentry_sdk/integrations/pydantic_ai/patches/tools.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from functools import wraps
22

3-
from pydantic_ai._tool_manager import ToolManager # type: ignore
4-
3+
from sentry_sdk.integrations import DidNotEnable
54
import sentry_sdk
65

76
from ..spans import execute_tool_span, update_execute_tool_span
@@ -22,6 +21,11 @@
2221
except ImportError:
2322
HAS_MCP = False
2423

24+
try:
25+
from pydantic_ai._tool_manager import ToolManager # type: ignore
26+
except ImportError:
27+
raise DidNotEnable("pydantic-ai not installed")
28+
2529

2630
def _patch_tool_execution():
2731
# type: () -> None

0 commit comments

Comments
 (0)