Skip to content

feat(integrations): Defer setup of the remaining AI integrations until import - #8342

Draft
ericapisani wants to merge 4 commits into
tel-133-defer-openai-integration-setup-until-thefrom
deferred-auto-enabling-integrations
Draft

ericapisani wants to merge 4 commits into
tel-133-defer-openai-integration-setup-until-thefrom
deferred-auto-enabling-integrations

Conversation

@ericapisani

@ericapisani ericapisani commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Extends the deferred_auto_enabling_integrations experiment from #8341 to the remaining AI integrations. Stacked on #8341.

Now deferred: anthropic, google_genai (trigger google.genai), huggingface_hub (trigger huggingface_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. The agents trigger only counts as installed when the openai-agents distribution is, so an app's own agents package 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_module returns and some setups read it as an attribute.

Known gaps:

  • If a deactivating library is installed but too old to enable, its targets are still deactivated (today they'd stay active).
  • Checking a dotted trigger imports its parent packages at init() (huggingface_hub + huggingface_hub.inference, ~4 ms).
  • langchain, langgraph and mcp stay eager: a single trigger module can't set them up safely (half-initialised partner packages, separate entry points). Tracked in TEL-136, which is where most of the PY-2415 saving is.

Benchmark

PY-2415 reporter's pip freeze, CPython 3.12, median of 15 runs:

init() time peak RSS modules after init()
flag off 2718 ms 410.0 MB 5666
flag on 2359 ms 377.6 MB 5137
flag on + langchain deferred (prototype, TEL-136) 263 ms 107.8 MB 1493

The integration_deactivation tox 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

…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
@linear-code

linear-code Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

TEL-134

TEL-135

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 139766 passed | ⏭️ 7960 skipped | Total: 147726 | Pass Rate: 94.61% | Execution Time: 827m 42s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +326
Passed Tests 📈 +271
Failed Tests —
Skipped Tests 📈 +55

All tests are passing successfully.

✅ Patch coverage is 92.59% (25 of 27 changed executable lines covered; target 80%).
Project statement coverage is 90.24% (up 0.01 percentage points from base (2ea4933) to head (23c925d)).

Changed files with executable lines (1)
File Patch coverage Changed executable lines
sentry_sdk/integrations/init.py 92.59% 25/27 covered; missed: 463, 464; partial branches: 387
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        +6

Generated 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 branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant