Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions sentry_sdk/integrations/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@
except ImportError:
OllamaEmbeddings = None

# LangGraph raises GraphBubbleUp / GraphInterrupt for normal control flow
# (human-in-the-loop interrupts, etc.) — these should never be captured as errors.
_graph_bubble_up: "type | tuple[()]" = ()
try:
from langgraph.errors import GraphBubbleUp # type: ignore[import-untyped]

_graph_bubble_up = GraphBubbleUp
except ImportError:
pass


def _get_ai_system(all_params: "Dict[str, Any]") -> "Optional[str]":
ai_type = all_params.get("_type")
Expand Down Expand Up @@ -266,6 +276,12 @@ def _handle_error(self, run_id: "UUID", error: "Any") -> None:
if not run_id or run_id not in self.span_map:
return

# LangGraph raises GraphBubbleUp/GraphInterrupt for normal
# control flow (e.g. human-in-the-loop interrupts). These are
# not errors and should not be captured as Sentry issues.
if isinstance(error, _graph_bubble_up):
return
Comment on lines +282 to +283
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.


span = self.span_map[run_id]

sentry_sdk.capture_exception(
Expand Down