Skip to content

feat(integrations): Defer langchain, langgraph and mcp setup with multi-module triggers - #8343

Draft
ericapisani wants to merge 1 commit into
deferred-auto-enabling-integrationsfrom
ericapisani/tel-136-defer-langchain-langgraph-and-mcp-setup
Draft

ericapisani wants to merge 1 commit into
deferred-auto-enabling-integrationsfrom
ericapisani/tel-136-defer-langchain-langgraph-and-mcp-setup

Conversation

@ericapisani

Copy link
Copy Markdown
Member

Extends the deferred_auto_enabling_integrations experiment to langchain, langgraph and mcp, the last eager AI integrations. Stacked on #8342.

A single trigger module isn't enough for these: importing langchain_openai or fastmcp first fires the hook while that package is half-initialised, so OpenAIEmbeddings / the FastMCP methods are left unpatched, and langgraph has separate entry points (langgraph.graph, langgraph.func).

Each deferred integration now has a tuple of trigger modules (the first decides whether it's installed). Setup runs when the outermost trigger import in the current thread completes, and trigger imports during a setup are queued until it finishes. The queue matters for langchain v1: langchain.agents imports langgraph during langchain's setup, and setting langgraph up right then would hit a circular import of the sentry langchain module.

Known gaps:

  • If an outer trigger import fails, the queued setups wait until the next trigger import completes in that thread.
  • Two threads importing langchain packages concurrently during startup can still hand one setup a partial module (Python's import deadlock breaking). The single-trigger design had the same shape.

Benchmark

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

init() time peak RSS modules after init()
flag off 2684 ms 410.3 MB 5666
flag on, #8342 2398 ms 378.5 MB 5137
flag on, this PR 270 ms 107.9 MB 1493

Fixes TEL-136

…ti-module triggers

Behind the deferred_auto_enabling_integrations experiment, an integration
can now have several trigger modules. Setup waits until the outermost
trigger import in the current thread completes, so no package the
integration imports from is half-initialised when it runs. Trigger
imports during a setup are queued rather than set up halfway through
it, which keeps langgraph working when langchain v1's agents module
pulls it in during langchain's setup.

langchain triggers on langchain_core, langchain, langchain_classic and
the provider packages it patches; langgraph on langgraph.graph, .func
and .pregel (langgraph is a namespace package); mcp on mcp and fastmcp.
langgraph.py now imports the langchain integration after its own
langgraph check.

For the PY-2415 reporter's environment, init() drops from ~2.4 s to
270 ms and peak RSS from 379 MB to 108 MB with the flag on.

Fixes TEL-136
@linear-code

linear-code Bot commented Nov 20, 2026

Copy link
Copy Markdown

TEL-136

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 139831 passed | ⏭️ 8016 skipped | Total: 147847 | Pass Rate: 94.58% | Execution Time: 814m 25s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +447
Passed Tests 📈 +336
Failed Tests —
Skipped Tests 📈 +111

All tests are passing successfully.

✅ Patch coverage is 92.86% (39 of 42 changed executable lines covered; target 80%).
Project statement coverage is 90.22% (down 0.01 percentage points from base (23c925d) to head (25ccf82)).

Changed files with executable lines (2)
File Patch coverage Changed executable lines
sentry_sdk/integrations/init.py 92.68% 38/41 covered; missed: 430, 482, 527; partial branches: 463, 481
sentry_sdk/integrations/langgraph.py 100.00% 1/1 covered
Coverage diff
@@            Coverage Diff             @@
##        master     #8343       +/-##
==========================================
- Coverage    90.23%    90.22%    -0.01%
==========================================
  Files          203       203         —
  Tracked lines     27349     27462      +113
  Branches     10252     10290       +38
==========================================
+ Hits         24675     24775      +100
- Misses        2674      2687       +13
- Partials      1557      1562        +5

Generated by Coverage Action

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