Repository navigation
feat(integrations): Defer setup of the remaining AI integrations until import - #8342
Draft
ericapisani wants to merge 4 commits into
Draft
ericapisani wants to merge 4 commits into
ericapisani wants to merge 4 commits into
Conversation
…b 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
Contributor
Coverage Results 📊✅ 139766 passed | ⏭️ 7960 skipped | Total: 147726 | Pass Rate: 94.61% | Execution Time: 827m 42s 📊 Comparison with Base Branch
All tests are passing successfully. ✅ Patch coverage is 92.59% (25 of 27 changed executable lines covered; target 80%). Changed files with executable lines (1)
Coverage diff@@ Coverage Diff @@
## master #8342 +/-##
==========================================
+ Coverage 90.23% 90.24% +0.01%
==========================================
Files 203 203 —
Tracked lines 27349 27442 +93
Branches 10252 10288 +36
==========================================
+ Hits 24675 24763 +88
- Misses 2674 2679 +5
- Partials 1557 1563 +6Generated by Coverage Action |
…mport 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
# Conflicts: # sentry_sdk/integrations/__init__.py
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
This was referenced Nov 20, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Extends the
deferred_auto_enabling_integrationsexperiment from #8341 to the remaining AI integrations. Stacked on #8341.Now deferred: anthropic, google_genai (trigger
google.genai), huggingface_hub (triggerhuggingface_hub.inference._client), openai_agents and pydantic_ai. With the flag on, deactivation by openai_agents and pydantic_ai is decided by whether their library is installed, before any hooks are registered, so importing the target before the deactivator no longer produces duplicate spans. Theagentstrigger only counts as installed when theopenai-agentsdistribution is, so an app's ownagentspackage doesn't deactivate OpenAI.The hook also attaches a submodule trigger to its parent package before running setup, because the import system only does that after
exec_modulereturns and some setups read it as an attribute.Known gaps:
init()(huggingface_hub+huggingface_hub.inference, ~4 ms).Benchmark
PY-2415 reporter's
pip freeze, CPython 3.12, median of 15 runs:init()timeinit()The
integration_deactivationtox env now installs openai-agents and pydantic-ai-slim on 3.10+, so its tests actually run in CI (the module was previously skipped entirely).Fixes TEL-134
Fixes TEL-135
Refs #6263