Skip to content

fix(langchain): filter GraphBubbleUp control-flow exceptions in _handle_error#6495

Open
wahajahmed010 wants to merge 1 commit into
getsentry:masterfrom
wahajahmed010:fix/6384-filter-graphbubbleup-langchain
Open

fix(langchain): filter GraphBubbleUp control-flow exceptions in _handle_error#6495
wahajahmed010 wants to merge 1 commit into
getsentry:masterfrom
wahajahmed010:fix/6384-filter-graphbubbleup-langchain

Conversation

@wahajahmed010
Copy link
Copy Markdown

Summary

LangGraph raises GraphBubbleUp / GraphInterrupt for normal control flow (e.g. langgraph.types.interrupt() for human-in-the-loop). Sentry's SentryLangchainCallback._handle_error was capturing these as errors via on_tool_error, creating false-positive Sentry issues.

Changes

  • Added a lazy module-level import of GraphBubbleUp from langgraph.errors (graceful fallback if langgraph is not installed)
  • Added an isinstance guard in _handle_error to skip capturing GraphBubbleUp instances

Testing

  • The change is a simple guard addition; existing behavior is preserved for all non-GraphBubbleUp exceptions
  • When langgraph is not installed, _graph_bubble_up defaults to empty tuple and no filtering is applied

Fixes #6384

AI assistance disclosure: this PR was co-authored by an AI assistant (OpenClaw Buck) under human supervision.

…le_error

LangGraph raises GraphBubbleUp/GraphInterrupt for normal control flow
(human-in-the-loop interrupts via langgraph.types.interrupt()). Sentry's
SentryLangchainCallback was capturing these as errors via on_tool_error,
creating false-positive issues.

Add a lazy module-level import of GraphBubbleUp from langgraph.errors,
and skip _handle_error for isinstance checks against this type.

Fixes: getsentry#6384
Co-authored-by: AI assistant (OpenClaw Buck)
@wahajahmed010 wahajahmed010 requested a review from a team as a code owner June 3, 2026 13:47
Comment on lines +282 to +283
if isinstance(error, _graph_bubble_up):
return
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: When handling GraphBubbleUp exceptions, the code returns early without cleaning up the associated span from self.span_map, causing a memory leak.
Severity: MEDIUM

Suggested Fix

When a _graph_bubble_up exception is handled, call a cleanup function like self._exit_span(span, run_id) before returning. This will ensure the span is properly closed and removed from the map without being captured as a Sentry error.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: sentry_sdk/integrations/langchain.py#L282-L283

Potential issue: In the Langchain integration's `_handle_error` function, when an error
is an instance of `_graph_bubble_up` (a control-flow exception used in LangGraph), the
function returns early. This is correct for not capturing it as a Sentry issue, but it
skips the necessary cleanup logic that closes the span and removes it from
`self.span_map`. In applications that use features like human-in-the-loop interrupts,
this will be triggered frequently, causing the `span_map` to grow indefinitely. This
results in a memory leak, as the garbage collection for the map is disabled by default.

Did we get this right? 👍 / 👎 to inform future reviews.

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.

LangchainIntegration reports GraphInterrupt / GraphBubbleUp as errors via on_tool_error

1 participant