From a0a0490a23a909752acb02073e9afccc4df9f983 Mon Sep 17 00:00:00 2001 From: Erica Pisani Date: Fri, 20 Nov 2026 11:21:13 -0500 Subject: [PATCH 1/3] feat(integrations): Defer Anthropic, Google GenAI and Hugging Face Hub setup until import Behind the deferred_auto_enabling_integrations experiment, init() no longer imports anthropic, google.genai or huggingface_hub's inference client just because their integrations are auto-enabling. Each is set up when the application first imports its trigger module. Dotted triggers needed two fixes in the hook: find_spec raising when the parent package isn't installed no longer crashes init(), and the submodule is bound to its parent before setup runs, since the import system only does that after exec_module returns. MCP and LangGraph stay eager. With an mcp trigger, fastmcp 2.x/3.x fire the hook while fastmcp is half-initialised and FastMCP is left unpatched. LangGraph has no single trigger that covers both the graph and functional APIs without hitting a half-initialised langgraph.graph. Refs TEL-134 --- sentry_sdk/integrations/__init__.py | 15 ++++- .../integrations/anthropic/test_anthropic.py | 18 ++++++ .../google_genai/test_google_genai.py | 18 ++++++ .../huggingface_hub/test_huggingface_hub.py | 18 ++++++ .../integrations/langchain/test_langchain.py | 20 ++++++ tests/integrations/openai/test_openai.py | 63 +++++-------------- tests/integrations/utils.py | 54 ++++++++++++++++ 7 files changed, 157 insertions(+), 49 deletions(-) diff --git a/sentry_sdk/integrations/__init__.py b/sentry_sdk/integrations/__init__.py index 8e26a4e216..a09dbd383e 100644 --- a/sentry_sdk/integrations/__init__.py +++ b/sentry_sdk/integrations/__init__.py @@ -138,6 +138,9 @@ def iter_default_integrations( # integrations are only set up once the application imports the trigger module. # This can't live on the integration class, since loading the class imports the library. _DEFERRED_INTEGRATION_TRIGGERS = { + "sentry_sdk.integrations.anthropic.AnthropicIntegration": "anthropic", + "sentry_sdk.integrations.google_genai.GoogleGenAIIntegration": "google.genai", + "sentry_sdk.integrations.huggingface_hub.HuggingfaceHubIntegration": "huggingface_hub.inference._client", "sentry_sdk.integrations.openai.OpenAIIntegration": "openai", } @@ -253,7 +256,10 @@ def setup_integrations( for import_string, trigger in _DEFERRED_INTEGRATION_TRIGGERS.items(): if trigger in sys.modules: continue - spec = importlib.util.find_spec(trigger) + try: + spec = importlib.util.find_spec(trigger) + except ImportError: + spec = None if spec is None: deferred[import_string] = None elif hasattr(spec.loader, "exec_module"): @@ -402,6 +408,13 @@ def exec_module(self, module: "ModuleType") -> None: module.__loader__ = self._loader if module.__spec__ is not None: module.__spec__.loader = self._loader + + # The import system only binds a submodule to its parent after exec_module + # returns, but setup may already access it as an attribute of the parent. + parent, _, child = self._trigger.rpartition(".") + if parent: + setattr(sys.modules[parent], child, sys.modules.get(self._trigger, module)) + _set_up_deferred_integration(self._trigger) def __getattr__(self, name: str) -> "Any": diff --git a/tests/integrations/anthropic/test_anthropic.py b/tests/integrations/anthropic/test_anthropic.py index 7fa10b4e9b..f2e67cdbb7 100644 --- a/tests/integrations/anthropic/test_anthropic.py +++ b/tests/integrations/anthropic/test_anthropic.py @@ -78,6 +78,7 @@ async def __call__(self, *args, **kwargs): from sentry_sdk.integrations.stdlib import StdlibIntegration from sentry_sdk.traces import SpanStatus from sentry_sdk.utils import package_version +from tests.integrations.utils import run_deferred_setup_app ANTHROPIC_VERSION = package_version("anthropic") @@ -10015,3 +10016,20 @@ def test_stream_messages_reasoning_tokens( assert span_data[SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 50 assert span_data[SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 150 assert span_data[SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 + + +def test_deferred_setup_on_import(tmp_path): + result = run_deferred_setup_app( + tmp_path, + identifier="anthropic", + trigger="anthropic", + app_imports="from anthropic.resources import Messages", + patched="Messages.create", + ) + + assert result == { + "imported_during_init": False, + "integration_enabled": True, + "patched": True, + "reported_in_event": True, + } diff --git a/tests/integrations/google_genai/test_google_genai.py b/tests/integrations/google_genai/test_google_genai.py index 444333e45f..b3c4bba889 100644 --- a/tests/integrations/google_genai/test_google_genai.py +++ b/tests/integrations/google_genai/test_google_genai.py @@ -12,6 +12,7 @@ from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.integrations.google_genai import GoogleGenAIIntegration from sentry_sdk.integrations.google_genai.utils import extract_contents_messages +from tests.integrations.utils import run_deferred_setup_app @pytest.fixture @@ -4762,3 +4763,20 @@ async def test_async_embed_content_data_collection( assert span_data[SPANDATA.GEN_AI_OPERATION_NAME] == "embeddings" assert span_data[SPANDATA.GEN_AI_REQUEST_MODEL] == "text-embedding-004" + + +def test_deferred_setup_on_import(tmp_path): + result = run_deferred_setup_app( + tmp_path, + identifier="google_genai", + trigger="google.genai", + app_imports="from google.genai.models import Models", + patched="Models.generate_content", + ) + + assert result == { + "imported_during_init": False, + "integration_enabled": True, + "patched": True, + "reported_in_event": True, + } diff --git a/tests/integrations/huggingface_hub/test_huggingface_hub.py b/tests/integrations/huggingface_hub/test_huggingface_hub.py index 5242e40fdb..46b9310ac2 100644 --- a/tests/integrations/huggingface_hub/test_huggingface_hub.py +++ b/tests/integrations/huggingface_hub/test_huggingface_hub.py @@ -10,6 +10,7 @@ from sentry_sdk.integrations.huggingface_hub import HuggingfaceHubIntegration from sentry_sdk.utils import package_version, safe_serialize from tests.conftest import ApproxDict +from tests.integrations.utils import run_deferred_setup_app try: from huggingface_hub.utils._errors import HfHubHTTPError @@ -2712,3 +2713,20 @@ def test_chat_completion_streaming_data_collection_tools( assert span_data[SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 183 assert span_data[SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 14 assert span_data[SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 197 + + +def test_deferred_setup_on_import(tmp_path): + result = run_deferred_setup_app( + tmp_path, + identifier="huggingface_hub", + trigger="huggingface_hub.inference._client", + app_imports="from huggingface_hub import InferenceClient", + patched="InferenceClient.chat_completion", + ) + + assert result == { + "imported_during_init": False, + "integration_enabled": True, + "patched": True, + "reported_in_event": True, + } diff --git a/tests/integrations/langchain/test_langchain.py b/tests/integrations/langchain/test_langchain.py index 0863aa5e8e..150889973e 100644 --- a/tests/integrations/langchain/test_langchain.py +++ b/tests/integrations/langchain/test_langchain.py @@ -38,6 +38,7 @@ ) from sentry_sdk.integrations.stdlib import StdlibIntegration from sentry_sdk.utils import package_version +from tests.integrations.utils import run_deferred_setup_app try: # langchain v1+ @@ -7338,3 +7339,22 @@ async def mock_aembed_query(self, text): # Data collection never gates non-PII attributes assert span_data[SPANDATA.GEN_AI_OPERATION_NAME] == "embeddings" assert span_data[SPANDATA.GEN_AI_REQUEST_MODEL] == "text-embedding-ada-002" + + +def test_deferred_setup_skips_deactivated_integration(tmp_path): + pytest.importorskip("google.genai") + + result = run_deferred_setup_app( + tmp_path, + identifier="google_genai", + trigger="google.genai", + app_imports="from google.genai.models import Models", + patched="Models.generate_content", + ) + + assert result == { + "imported_during_init": False, + "integration_enabled": False, + "patched": False, + "reported_in_event": False, + } diff --git a/tests/integrations/openai/test_openai.py b/tests/integrations/openai/test_openai.py index dae084c95c..bbf6cc9e1f 100644 --- a/tests/integrations/openai/test_openai.py +++ b/tests/integrations/openai/test_openai.py @@ -1,8 +1,4 @@ import json -import os -import subprocess -import sys -from textwrap import dedent import pytest @@ -81,6 +77,7 @@ from sentry_sdk.integrations.openai import OpenAIIntegration from sentry_sdk.integrations.stdlib import StdlibIntegration from sentry_sdk.utils import safe_serialize +from tests.integrations.utils import run_deferred_setup_app try: from unittest.mock import AsyncMock @@ -8278,50 +8275,20 @@ async def test_streaming_responses_api_ttft_async( def test_deferred_setup_on_import(tmp_path): - app = tmp_path / "app.py" - app.write_text( - dedent( - """ - import json - import sys - - import sentry_sdk - - events = [] - - def before_send(event, hint): - events.append(event) - return None - - sentry_sdk.init( - before_send=before_send, - _experiments={"deferred_auto_enabling_integrations": True}, - ) - imported_during_init = "openai" in sys.modules - - from openai.resources.chat.completions import Completions - - sentry_sdk.capture_message("hi") - - print(json.dumps({ - "imported_during_init": imported_during_init, - "integration_enabled": sentry_sdk.get_client().get_integration("openai") is not None, - "patched_by": Completions.create.__code__.co_filename, - "sdk_integrations": events[0]["sdk"]["integrations"], - })) - """ - ) - ) - - output = subprocess.check_output([sys.executable, str(app)]) - result = json.loads(output) - - assert result["imported_during_init"] is False - assert result["integration_enabled"] is True - assert result["patched_by"].endswith( - os.path.join("sentry_sdk", "integrations", "openai.py") - ) - assert "openai" in result["sdk_integrations"] + result = run_deferred_setup_app( + tmp_path, + identifier="openai", + trigger="openai", + app_imports="from openai.resources.chat.completions import Completions", + patched="Completions.create", + ) + + assert result == { + "imported_during_init": False, + "integration_enabled": True, + "patched": True, + "reported_in_event": True, + } def test_deferred_setup_when_already_imported(sentry_init): diff --git a/tests/integrations/utils.py b/tests/integrations/utils.py index 2f0cbbaff7..2c4c210209 100644 --- a/tests/integrations/utils.py +++ b/tests/integrations/utils.py @@ -1,3 +1,8 @@ +import json +import subprocess +import sys +from textwrap import dedent + import pytest from sentry_sdk.utils import SENSITIVE_DATA_SUBSTITUTE @@ -118,3 +123,52 @@ id="data_collection_queues_on_with_no_pii", ), ] + + +def run_deferred_setup_app(tmp_path, identifier, trigger, app_imports, patched): + """ + Runs an app in a fresh interpreter that calls ``init()`` with deferred setup of + auto-enabling integrations on, and only afterwards runs ``app_imports``. + """ + app = tmp_path / "app.py" + app.write_text( + dedent( + """ + import json + import os + import sys + + import sentry_sdk + + events = [] + + def before_send(event, hint): + events.append(event) + return None + + sentry_sdk.init( + before_send=before_send, + _experiments={{"deferred_auto_enabling_integrations": True}}, + ) + imported_during_init = {trigger!r} in sys.modules + + {app_imports} + + sentry_sdk.capture_message("hi") + + print(json.dumps({{ + "imported_during_init": imported_during_init, + "integration_enabled": sentry_sdk.get_client().get_integration({identifier!r}) is not None, + "patched": os.path.join("sentry_sdk", "integrations") in {patched}.__code__.co_filename, + "reported_in_event": {identifier!r} in events[0]["sdk"]["integrations"], + }})) + """ + ).format( + identifier=identifier, + trigger=trigger, + app_imports=app_imports, + patched=patched, + ) + ) + + return json.loads(subprocess.check_output([sys.executable, str(app)])) From cbce609ecd9d06a2acfcc743534322ebaa3f4477 Mon Sep 17 00:00:00 2001 From: Erica Pisani Date: Fri, 20 Nov 2026 12:08:11 -0500 Subject: [PATCH 2/3] feat(integrations): Defer OpenAI Agents and Pydantic AI setup until import Behind the deferred_auto_enabling_integrations experiment, init() no longer imports agents or pydantic_ai just because their integrations are auto-enabling. Both deactivate other integrations. A deferred integration isn't active during init(), so it now deactivates its targets whenever its library is installed. Importing openai before agents or pydantic_ai no longer produces duplicate spans. If the library is installed but can't be enabled (too old, or a local agents module shadows openai-agents), its targets are now deactivated, whereas without the flag they stay active. LangChain stays eager. Its integration imports the installed partner packages, which import langchain_core first, so setup would run while e.g. langchain_openai is half-initialised and leave its embeddings unpatched. The LangGraph integration also imports langchain_core during init() whenever it's installed. The integration_deactivation tox env now installs openai-agents, pydantic-ai-slim and google-genai. Without them the whole test module was skipped. Refs TEL-135 --- scripts/populate_tox/tox.jinja | 5 +- sentry_sdk/integrations/__init__.py | 15 +- .../openai_agents/test_openai_agents.py | 18 +++ .../pydantic_ai/test_pydantic_ai.py | 18 +++ tests/test_ai_integration_deactivation.py | 135 ++++++++++++++++++ tox.ini | 5 +- 6 files changed, 191 insertions(+), 5 deletions(-) diff --git a/scripts/populate_tox/tox.jinja b/scripts/populate_tox/tox.jinja index 12e5d44abf..43056328ce 100644 --- a/scripts/populate_tox/tox.jinja +++ b/scripts/populate_tox/tox.jinja @@ -23,7 +23,7 @@ envlist = {py3.6,py3.8,py3.10,py3.11,py3.12}-gevent # === Integration Deactivation === - {py3.9,py3.10,py3.11,py3.12,py3.13,py3.14}-integration_deactivation + {py3.10,py3.11,py3.12,py3.13,py3.14}-integration_deactivation # === Shadowed Module === {py3.7,py3.8,py3.9,py3.10,py3.11,py3.12,py3.13,py3.14,py3.14t,py3.15,py3.15t}-shadowed_module @@ -125,6 +125,9 @@ deps = integration_deactivation: openai integration_deactivation: anthropic integration_deactivation: langchain + integration_deactivation: google-genai + integration_deactivation: openai-agents + integration_deactivation: pydantic-ai-slim # === Integrations === diff --git a/sentry_sdk/integrations/__init__.py b/sentry_sdk/integrations/__init__.py index a09dbd383e..0ebf8be81d 100644 --- a/sentry_sdk/integrations/__init__.py +++ b/sentry_sdk/integrations/__init__.py @@ -142,6 +142,8 @@ def iter_default_integrations( "sentry_sdk.integrations.google_genai.GoogleGenAIIntegration": "google.genai", "sentry_sdk.integrations.huggingface_hub.HuggingfaceHubIntegration": "huggingface_hub.inference._client", "sentry_sdk.integrations.openai.OpenAIIntegration": "openai", + "sentry_sdk.integrations.openai_agents.OpenAIAgentsIntegration": "agents", + "sentry_sdk.integrations.pydantic_ai.PydanticAIIntegration": "pydantic_ai", } iter_default_integrations = _generate_default_integrations_iterator( @@ -290,11 +292,18 @@ def setup_integrations( } deactivated_identifiers = set() + # A deferred integration can't be set up yet, so it deactivates its targets + # whenever its library is installed, even if it later turns out too old to enable. + installed_deferred_identifiers = { + import_string.rsplit(".", 2)[1] + for import_string, trigger in deferred.items() + if trigger is not None + } + for integration, targets_to_deactivate in _INTEGRATION_DEACTIVATES.items(): if ( - integration in integrations - and integration not in disabled_integration_identifiers - ): + integration in integrations or integration in installed_deferred_identifiers + ) and integration not in disabled_integration_identifiers: for target in targets_to_deactivate: if target not in user_provided_integrations: deactivated_identifiers.add(target) diff --git a/tests/integrations/openai_agents/test_openai_agents.py b/tests/integrations/openai_agents/test_openai_agents.py index b36436592b..7059bdd21b 100644 --- a/tests/integrations/openai_agents/test_openai_agents.py +++ b/tests/integrations/openai_agents/test_openai_agents.py @@ -75,6 +75,7 @@ from sentry_sdk.integrations.openai_agents.utils import _set_input_data, safe_serialize from sentry_sdk.integrations.stdlib import StdlibIntegration from sentry_sdk.utils import package_version, parse_version +from tests.integrations.utils import run_deferred_setup_app OPENAI_VERSION = package_version("openai") @@ -7678,3 +7679,20 @@ async def test_runner_run_streamed_with_starting_agent_kwarg( (transaction,) = events assert transaction["transaction"] == "test_agent workflow" + + +def test_deferred_setup_on_import(tmp_path): + result = run_deferred_setup_app( + tmp_path, + identifier="openai_agents", + trigger="agents", + app_imports="from agents.run import DEFAULT_AGENT_RUNNER", + patched="DEFAULT_AGENT_RUNNER.run", + ) + + assert result == { + "imported_during_init": False, + "integration_enabled": True, + "patched": True, + "reported_in_event": True, + } diff --git a/tests/integrations/pydantic_ai/test_pydantic_ai.py b/tests/integrations/pydantic_ai/test_pydantic_ai.py index c67a399d40..46238196f6 100644 --- a/tests/integrations/pydantic_ai/test_pydantic_ai.py +++ b/tests/integrations/pydantic_ai/test_pydantic_ai.py @@ -27,6 +27,7 @@ from sentry_sdk.integrations.pydantic_ai.spans.ai_client import _set_input_messages from sentry_sdk.integrations.pydantic_ai.spans.utils import _set_usage_data from sentry_sdk.utils import package_version +from tests.integrations.utils import run_deferred_setup_app PYDANTIC_AI_VERSION = package_version("pydantic-ai") @@ -4869,3 +4870,20 @@ def add_numbers(a: int, b: int) -> int: assert tool_messages[0]["content"] == [ {"type": "text", "text": expected_tool_return} ] + + +def test_deferred_setup_on_import(tmp_path): + result = run_deferred_setup_app( + tmp_path, + identifier="pydantic_ai", + trigger="pydantic_ai", + app_imports="from pydantic_ai import Agent", + patched="Agent.run", + ) + + assert result == { + "imported_during_init": False, + "integration_enabled": True, + "patched": True, + "reported_in_event": True, + } diff --git a/tests/test_ai_integration_deactivation.py b/tests/test_ai_integration_deactivation.py index 3c13ef7546..810d0c2450 100644 --- a/tests/test_ai_integration_deactivation.py +++ b/tests/test_ai_integration_deactivation.py @@ -1,3 +1,8 @@ +import json +import subprocess +import sys +from textwrap import dedent + import pytest from sentry_sdk import get_client @@ -249,3 +254,133 @@ def test_only_auto_enabling_integrations_without_defaults( if LangchainIntegration in integration_types: assert OpenAIIntegration not in integration_types assert AnthropicIntegration not in integration_types + + +OPENAI_CALL = ( + "openai.OpenAI(api_key='x', base_url='http://localhost:1', max_retries=0)" + ".chat.completions.create(model='gpt-4o', messages=[{'role': 'user', 'content': 'hi'}])" +) +ANTHROPIC_CALL = ( + "anthropic.Anthropic(api_key='x', base_url='http://localhost:1', max_retries=0)" + ".messages.create(model='claude', max_tokens=1, messages=[{'role': 'user', 'content': 'hi'}])" +) +GOOGLE_GENAI_CALL = ( + "google.genai.Client(api_key='x', http_options={'base_url': 'http://localhost:1', 'retry_options': {'attempts': 1}})" + ".models.generate_content(model='gemini', contents='hi')" +) + + +@pytest.mark.parametrize( + "deactivator_trigger, target, target_module, target_call", + [ + pytest.param( + "langchain_core", "openai", "openai", OPENAI_CALL, id="langchain-openai" + ), + pytest.param( + "langchain_core", + "anthropic", + "anthropic", + ANTHROPIC_CALL, + id="langchain-anthropic", + ), + pytest.param( + "langchain_core", + "google_genai", + "google.genai", + GOOGLE_GENAI_CALL, + id="langchain-google_genai", + ), + pytest.param( + "agents", "openai", "openai", OPENAI_CALL, id="openai_agents-openai" + ), + pytest.param( + "pydantic_ai", "openai", "openai", OPENAI_CALL, id="pydantic_ai-openai" + ), + pytest.param( + "pydantic_ai", + "anthropic", + "anthropic", + ANTHROPIC_CALL, + id="pydantic_ai-anthropic", + ), + ], +) +@pytest.mark.parametrize( + "explicit_target", + [ + pytest.param(False, id="auto_enabled_target"), + pytest.param(True, id="explicit_target"), + ], +) +def test_deactivator_imported_after_target( + tmp_path, deactivator_trigger, target, target_module, target_call, explicit_target +): + target_integration = { + "openai": "OpenAIIntegration", + "anthropic": "AnthropicIntegration", + "google_genai": "GoogleGenAIIntegration", + }[target] + + app = tmp_path / "app.py" + app.write_text( + dedent( + """ + import json + import sys + + import sentry_sdk + + # Makes the other deactivating libraries look uninstalled + for name in {other_deactivator_triggers!r}: + sys.modules[name] = None + + integrations = [] + if {explicit_target}: + from sentry_sdk.integrations.{target} import {target_integration} + + integrations.append({target_integration}()) + + transactions = [] + + def before_send_transaction(event, hint): + transactions.append(event) + return None + + sentry_sdk.init( + traces_sample_rate=1.0, + before_send_transaction=before_send_transaction, + integrations=integrations, + _experiments={{"deferred_auto_enabling_integrations": True}}, + ) + + import {target_module} + import {deactivator_trigger} + + with sentry_sdk.start_transaction(name="ai"): + try: + {target_call} + except Exception: + pass + + print(json.dumps([ + span["origin"] + for span in transactions[0]["spans"] + if span["origin"].startswith("auto.ai.") + ])) + """ + ).format( + target=target, + other_deactivator_triggers=sorted( + {"agents", "langchain_core", "pydantic_ai"} - {deactivator_trigger} + ), + explicit_target=explicit_target, + target_integration=target_integration, + target_module=target_module, + deactivator_trigger=deactivator_trigger, + target_call=target_call, + ) + ) + + ai_span_origins = json.loads(subprocess.check_output([sys.executable, str(app)])) + + assert ai_span_origins == ([f"auto.ai.{target}"] if explicit_target else []) diff --git a/tox.ini b/tox.ini index 42a737b34f..4e831e8a36 100644 --- a/tox.ini +++ b/tox.ini @@ -23,7 +23,7 @@ envlist = {py3.6,py3.8,py3.10,py3.11,py3.12}-gevent # === Integration Deactivation === - {py3.9,py3.10,py3.11,py3.12,py3.13,py3.14}-integration_deactivation + {py3.10,py3.11,py3.12,py3.13,py3.14}-integration_deactivation # === Shadowed Module === {py3.7,py3.8,py3.9,py3.10,py3.11,py3.12,py3.13,py3.14,py3.14t,py3.15,py3.15t}-shadowed_module @@ -458,6 +458,9 @@ deps = integration_deactivation: openai integration_deactivation: anthropic integration_deactivation: langchain + integration_deactivation: google-genai + integration_deactivation: openai-agents + integration_deactivation: pydantic-ai-slim # === Integrations === From 23c925d6b7a20b07d9f2bf9a78095ab08067e143 Mon Sep 17 00:00:00 2001 From: Erica Pisani Date: Fri, 20 Nov 2026 13:08:47 -0500 Subject: [PATCH 3/3] fix(integrations): Guard the agents trigger and harden deferred setup Only treat the `agents` trigger as installed when the openai-agents distribution is, so a local `agents` package doesn't defer OpenAI Agents or deactivate OpenAI. Keep binding the trigger to its parent package from raising into the user's import. Restore Python 3.9 coverage of the integration deactivation tests, only installing and requiring openai-agents and pydantic-ai on newer Pythons. Refs TEL-134 TEL-135 --- scripts/populate_tox/tox.jinja | 7 +- sentry_sdk/integrations/__init__.py | 20 ++- tests/integrations/openai/test_openai.py | 15 +- tests/integrations/utils.py | 10 +- tests/test_ai_integration_deactivation.py | 191 ++++++++-------------- tox.ini | 7 +- 6 files changed, 111 insertions(+), 139 deletions(-) diff --git a/scripts/populate_tox/tox.jinja b/scripts/populate_tox/tox.jinja index 43056328ce..d169bfc4d7 100644 --- a/scripts/populate_tox/tox.jinja +++ b/scripts/populate_tox/tox.jinja @@ -23,7 +23,7 @@ envlist = {py3.6,py3.8,py3.10,py3.11,py3.12}-gevent # === Integration Deactivation === - {py3.10,py3.11,py3.12,py3.13,py3.14}-integration_deactivation + {py3.9,py3.10,py3.11,py3.12,py3.13,py3.14}-integration_deactivation # === Shadowed Module === {py3.7,py3.8,py3.9,py3.10,py3.11,py3.12,py3.13,py3.14,py3.14t,py3.15,py3.15t}-shadowed_module @@ -125,9 +125,8 @@ deps = integration_deactivation: openai integration_deactivation: anthropic integration_deactivation: langchain - integration_deactivation: google-genai - integration_deactivation: openai-agents - integration_deactivation: pydantic-ai-slim + {py3.10,py3.11,py3.12,py3.13,py3.14}-integration_deactivation: openai-agents + {py3.10,py3.11,py3.12,py3.13,py3.14}-integration_deactivation: pydantic-ai-slim # === Integrations === diff --git a/sentry_sdk/integrations/__init__.py b/sentry_sdk/integrations/__init__.py index 0b5f15bef7..84d1ca49b2 100644 --- a/sentry_sdk/integrations/__init__.py +++ b/sentry_sdk/integrations/__init__.py @@ -8,7 +8,7 @@ from typing import TYPE_CHECKING import sentry_sdk -from sentry_sdk.utils import capture_internal_exceptions, logger +from sentry_sdk.utils import capture_internal_exceptions, logger, package_version if TYPE_CHECKING: from collections.abc import Container, Sequence @@ -271,7 +271,10 @@ def setup_integrations( spec = importlib.util.find_spec(trigger) except Exception: spec = None - if spec is None: + # `agents` is a generic module name, so make sure it's openai-agents' + if spec is None or ( + trigger == "agents" and package_version("openai-agents") is None + ): excluded.add(import_string) elif hasattr(spec.loader, "exec_module"): excluded.add(import_string) @@ -414,11 +417,14 @@ def exec_module(self, module: "ModuleType") -> None: if module.__spec__ is not None: module.__spec__.loader = self._loader - # The import system only binds a submodule to its parent after exec_module - # returns, but setup may already access it as an attribute of the parent. - parent, _, child = self._trigger.rpartition(".") - if parent: - setattr(sys.modules[parent], child, sys.modules.get(self._trigger, module)) + # Setup may access the trigger as an attribute of its parent package + # before the import system binds it there + with capture_internal_exceptions(): + parent, _, child = self._trigger.rpartition(".") + if parent: + setattr( + sys.modules[parent], child, sys.modules.get(self._trigger, module) + ) _set_up_deferred_integration(self._trigger) diff --git a/tests/integrations/openai/test_openai.py b/tests/integrations/openai/test_openai.py index bbf6cc9e1f..c1247b1ce0 100644 --- a/tests/integrations/openai/test_openai.py +++ b/tests/integrations/openai/test_openai.py @@ -8274,7 +8274,20 @@ async def test_streaming_responses_api_ttft_async( assert ttft > 0 -def test_deferred_setup_on_import(tmp_path): +@pytest.mark.parametrize( + "local_agents_package", + [ + pytest.param(False, id="without_local_agents_package"), + pytest.param(True, id="with_local_agents_package"), + ], +) +def test_deferred_setup_on_import(tmp_path, local_agents_package): + if local_agents_package: + if package_version("openai-agents") is not None: + pytest.skip("openai-agents is installed") + (tmp_path / "agents").mkdir() + (tmp_path / "agents" / "__init__.py").write_text("") + result = run_deferred_setup_app( tmp_path, identifier="openai", diff --git a/tests/integrations/utils.py b/tests/integrations/utils.py index 2c4c210209..d08f6b8fa4 100644 --- a/tests/integrations/utils.py +++ b/tests/integrations/utils.py @@ -125,10 +125,13 @@ ] -def run_deferred_setup_app(tmp_path, identifier, trigger, app_imports, patched): +def run_deferred_setup_app( + tmp_path, identifier, trigger, app_imports, patched, before_init="" +): """ Runs an app in a fresh interpreter that calls ``init()`` with deferred setup of auto-enabling integrations on, and only afterwards runs ``app_imports``. + ``before_init`` runs before ``init()`` and can append to ``integrations``. """ app = tmp_path / "app.py" app.write_text( @@ -140,6 +143,9 @@ def run_deferred_setup_app(tmp_path, identifier, trigger, app_imports, patched): import sentry_sdk + integrations = [] + {before_init} + events = [] def before_send(event, hint): @@ -148,6 +154,7 @@ def before_send(event, hint): sentry_sdk.init( before_send=before_send, + integrations=integrations, _experiments={{"deferred_auto_enabling_integrations": True}}, ) imported_during_init = {trigger!r} in sys.modules @@ -168,6 +175,7 @@ def before_send(event, hint): trigger=trigger, app_imports=app_imports, patched=patched, + before_init=before_init, ) ) diff --git a/tests/test_ai_integration_deactivation.py b/tests/test_ai_integration_deactivation.py index 810d0c2450..1c5064f9c9 100644 --- a/tests/test_ai_integration_deactivation.py +++ b/tests/test_ai_integration_deactivation.py @@ -1,12 +1,8 @@ -import json -import subprocess -import sys -from textwrap import dedent - import pytest from sentry_sdk import get_client from sentry_sdk.integrations import _INTEGRATION_DEACTIVATES +from tests.integrations.utils import run_deferred_setup_app try: from sentry_sdk.integrations.langchain import LangchainIntegration @@ -46,13 +42,7 @@ pytestmark = pytest.mark.skipif( - not ( - has_langchain - and has_openai - and has_anthropic - and has_openai_agents - and has_pydantic_ai - ), + not (has_langchain and has_openai and has_anthropic), reason="Requires langchain, openai, and anthropic packages to be installed", ) @@ -135,6 +125,10 @@ def test_user_can_override_with_both_explicit_integrations( assert AnthropicIntegration in integration_types +@pytest.mark.skipif( + not (has_openai_agents and has_pydantic_ai), + reason="Requires openai-agents and pydantic-ai", +) def test_disabling_integrations_allows_openai_and_anthropic( sentry_init, reset_integrations ): @@ -256,131 +250,84 @@ def test_only_auto_enabling_integrations_without_defaults( assert AnthropicIntegration not in integration_types -OPENAI_CALL = ( - "openai.OpenAI(api_key='x', base_url='http://localhost:1', max_retries=0)" - ".chat.completions.create(model='gpt-4o', messages=[{'role': 'user', 'content': 'hi'}])" -) -ANTHROPIC_CALL = ( - "anthropic.Anthropic(api_key='x', base_url='http://localhost:1', max_retries=0)" - ".messages.create(model='claude', max_tokens=1, messages=[{'role': 'user', 'content': 'hi'}])" -) -GOOGLE_GENAI_CALL = ( - "google.genai.Client(api_key='x', http_options={'base_url': 'http://localhost:1', 'retry_options': {'attempts': 1}})" - ".models.generate_content(model='gemini', contents='hi')" +@pytest.mark.parametrize( + "explicit_target", + [ + pytest.param(False, id="target_auto_enabled"), + pytest.param(True, id="target_in_integrations"), + ], ) - - @pytest.mark.parametrize( - "deactivator_trigger, target, target_module, target_call", + "deactivator_trigger, target, target_integration, target_imports, patched", [ pytest.param( - "langchain_core", "openai", "openai", OPENAI_CALL, id="langchain-openai" - ), - pytest.param( - "langchain_core", - "anthropic", - "anthropic", - ANTHROPIC_CALL, - id="langchain-anthropic", - ), - pytest.param( - "langchain_core", - "google_genai", - "google.genai", - GOOGLE_GENAI_CALL, - id="langchain-google_genai", - ), - pytest.param( - "agents", "openai", "openai", OPENAI_CALL, id="openai_agents-openai" + "agents", + "openai", + "OpenAIIntegration", + "from openai.resources.chat.completions import Completions", + "Completions.create", + id="deferred_auto_enabling_integrations-openai_agents-openai", + marks=pytest.mark.skipif( + not has_openai_agents, reason="Requires openai-agents" + ), ), pytest.param( - "pydantic_ai", "openai", "openai", OPENAI_CALL, id="pydantic_ai-openai" + "pydantic_ai", + "openai", + "OpenAIIntegration", + "from openai.resources.chat.completions import Completions", + "Completions.create", + id="deferred_auto_enabling_integrations-pydantic_ai-openai", + marks=pytest.mark.skipif( + not has_pydantic_ai, reason="Requires pydantic-ai" + ), ), pytest.param( "pydantic_ai", "anthropic", - "anthropic", - ANTHROPIC_CALL, - id="pydantic_ai-anthropic", + "AnthropicIntegration", + "from anthropic.resources import Messages", + "Messages.create", + id="deferred_auto_enabling_integrations-pydantic_ai-anthropic", + marks=pytest.mark.skipif( + not has_pydantic_ai, reason="Requires pydantic-ai" + ), ), ], ) -@pytest.mark.parametrize( - "explicit_target", - [ - pytest.param(False, id="auto_enabled_target"), - pytest.param(True, id="explicit_target"), - ], -) def test_deactivator_imported_after_target( - tmp_path, deactivator_trigger, target, target_module, target_call, explicit_target + tmp_path, + deactivator_trigger, + target, + target_integration, + target_imports, + patched, + explicit_target, ): - target_integration = { - "openai": "OpenAIIntegration", - "anthropic": "AnthropicIntegration", - "google_genai": "GoogleGenAIIntegration", - }[target] - - app = tmp_path / "app.py" - app.write_text( - dedent( - """ - import json - import sys - - import sentry_sdk - - # Makes the other deactivating libraries look uninstalled - for name in {other_deactivator_triggers!r}: - sys.modules[name] = None - - integrations = [] - if {explicit_target}: - from sentry_sdk.integrations.{target} import {target_integration} - - integrations.append({target_integration}()) - - transactions = [] - - def before_send_transaction(event, hint): - transactions.append(event) - return None - - sentry_sdk.init( - traces_sample_rate=1.0, - before_send_transaction=before_send_transaction, - integrations=integrations, - _experiments={{"deferred_auto_enabling_integrations": True}}, - ) - - import {target_module} - import {deactivator_trigger} - - with sentry_sdk.start_transaction(name="ai"): - try: - {target_call} - except Exception: - pass - - print(json.dumps([ - span["origin"] - for span in transactions[0]["spans"] - if span["origin"].startswith("auto.ai.") - ])) - """ - ).format( - target=target, - other_deactivator_triggers=sorted( - {"agents", "langchain_core", "pydantic_ai"} - {deactivator_trigger} - ), - explicit_target=explicit_target, - target_integration=target_integration, - target_module=target_module, - deactivator_trigger=deactivator_trigger, - target_call=target_call, - ) + hidden_deactivators = sorted( + {"agents", "langchain_core", "pydantic_ai"} - {deactivator_trigger} ) + before_init = ( + f"for name in {hidden_deactivators!r}:\n sys.modules[name] = None\n" + ) + if explicit_target: + before_init += ( + f"from sentry_sdk.integrations.{target} import {target_integration}\n" + f"integrations.append({target_integration}())\n" + ) - ai_span_origins = json.loads(subprocess.check_output([sys.executable, str(app)])) + result = run_deferred_setup_app( + tmp_path, + identifier=target, + trigger=target, + app_imports=f"{target_imports}\nimport {deactivator_trigger}", + patched=patched, + before_init=before_init, + ) - assert ai_span_origins == ([f"auto.ai.{target}"] if explicit_target else []) + assert result == { + "imported_during_init": explicit_target, + "integration_enabled": explicit_target, + "patched": explicit_target, + "reported_in_event": explicit_target, + } diff --git a/tox.ini b/tox.ini index 4e831e8a36..fb11e26410 100644 --- a/tox.ini +++ b/tox.ini @@ -23,7 +23,7 @@ envlist = {py3.6,py3.8,py3.10,py3.11,py3.12}-gevent # === Integration Deactivation === - {py3.10,py3.11,py3.12,py3.13,py3.14}-integration_deactivation + {py3.9,py3.10,py3.11,py3.12,py3.13,py3.14}-integration_deactivation # === Shadowed Module === {py3.7,py3.8,py3.9,py3.10,py3.11,py3.12,py3.13,py3.14,py3.14t,py3.15,py3.15t}-shadowed_module @@ -458,9 +458,8 @@ deps = integration_deactivation: openai integration_deactivation: anthropic integration_deactivation: langchain - integration_deactivation: google-genai - integration_deactivation: openai-agents - integration_deactivation: pydantic-ai-slim + {py3.10,py3.11,py3.12,py3.13,py3.14}-integration_deactivation: openai-agents + {py3.10,py3.11,py3.12,py3.13,py3.14}-integration_deactivation: pydantic-ai-slim # === Integrations ===