From 74ead4e90c8a444407e687118537ee573adfcc44 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Fri, 21 Nov 2025 15:06:13 +0100 Subject: [PATCH] fix(pydantic-ai): Make imports defensive to avoid ModuleNotFoundError --- sentry_sdk/integrations/pydantic_ai/patches/agent_run.py | 7 ++++++- .../integrations/pydantic_ai/patches/graph_nodes.py | 7 ++++++- .../integrations/pydantic_ai/patches/model_request.py | 7 ++++++- sentry_sdk/integrations/pydantic_ai/patches/tools.py | 8 ++++++-- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/sentry_sdk/integrations/pydantic_ai/patches/agent_run.py b/sentry_sdk/integrations/pydantic_ai/patches/agent_run.py index 5b58d8f128..daa2da112c 100644 --- a/sentry_sdk/integrations/pydantic_ai/patches/agent_run.py +++ b/sentry_sdk/integrations/pydantic_ai/patches/agent_run.py @@ -1,12 +1,17 @@ from functools import wraps import sentry_sdk +from sentry_sdk.integrations import DidNotEnable from ..spans import invoke_agent_span, update_invoke_agent_span from ..utils import _capture_exception, pop_agent, push_agent from typing import TYPE_CHECKING -from pydantic_ai.agent import Agent # type: ignore + +try: + from pydantic_ai.agent import Agent # type: ignore +except ImportError: + raise DidNotEnable("pydantic-ai not installed") if TYPE_CHECKING: from typing import Any, Callable, Optional diff --git a/sentry_sdk/integrations/pydantic_ai/patches/graph_nodes.py b/sentry_sdk/integrations/pydantic_ai/patches/graph_nodes.py index e10770d357..6de4c3e80a 100644 --- a/sentry_sdk/integrations/pydantic_ai/patches/graph_nodes.py +++ b/sentry_sdk/integrations/pydantic_ai/patches/graph_nodes.py @@ -2,12 +2,17 @@ from functools import wraps import sentry_sdk +from sentry_sdk.integrations import DidNotEnable from ..spans import ( ai_client_span, update_ai_client_span, ) -from pydantic_ai._agent_graph import ModelRequestNode # type: ignore + +try: + from pydantic_ai._agent_graph import ModelRequestNode # type: ignore +except ImportError: + raise DidNotEnable("pydantic-ai not installed") from typing import TYPE_CHECKING diff --git a/sentry_sdk/integrations/pydantic_ai/patches/model_request.py b/sentry_sdk/integrations/pydantic_ai/patches/model_request.py index f4676654cd..f33a031b07 100644 --- a/sentry_sdk/integrations/pydantic_ai/patches/model_request.py +++ b/sentry_sdk/integrations/pydantic_ai/patches/model_request.py @@ -1,7 +1,12 @@ from functools import wraps from typing import TYPE_CHECKING -from pydantic_ai import models # type: ignore +from sentry_sdk.integrations import DidNotEnable + +try: + from pydantic_ai import models # type: ignore +except ImportError: + raise DidNotEnable("pydantic-ai not installed") from ..spans import ai_client_span, update_ai_client_span diff --git a/sentry_sdk/integrations/pydantic_ai/patches/tools.py b/sentry_sdk/integrations/pydantic_ai/patches/tools.py index 1940be811f..e3872a5bbc 100644 --- a/sentry_sdk/integrations/pydantic_ai/patches/tools.py +++ b/sentry_sdk/integrations/pydantic_ai/patches/tools.py @@ -1,7 +1,6 @@ from functools import wraps -from pydantic_ai._tool_manager import ToolManager # type: ignore - +from sentry_sdk.integrations import DidNotEnable import sentry_sdk from ..spans import execute_tool_span, update_execute_tool_span @@ -22,6 +21,11 @@ except ImportError: HAS_MCP = False +try: + from pydantic_ai._tool_manager import ToolManager # type: ignore +except ImportError: + raise DidNotEnable("pydantic-ai not installed") + def _patch_tool_execution(): # type: () -> None