From a5c79b2e68599c04274f787545fd67bf17ce5c17 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Wed, 20 May 2026 08:49:55 +0200 Subject: [PATCH 1/2] fix(langchain): Stop setting transaction status when child span fails --- sentry_sdk/integrations/langchain.py | 18 ++++++++++-------- tests/integrations/langchain/test_langchain.py | 2 -- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/sentry_sdk/integrations/langchain.py b/sentry_sdk/integrations/langchain.py index 9488731b52..4dab6c376c 100644 --- a/sentry_sdk/integrations/langchain.py +++ b/sentry_sdk/integrations/langchain.py @@ -19,7 +19,7 @@ from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.integrations import DidNotEnable, Integration from sentry_sdk.scope import should_send_default_pii -from sentry_sdk.tracing_utils import _get_value, set_span_errored +from sentry_sdk.tracing_utils import _get_value from sentry_sdk.utils import capture_internal_exceptions, logger if TYPE_CHECKING: @@ -273,11 +273,10 @@ def _handle_error(self, run_id: "UUID", error: "Any") -> None: span_data = self.span_map[run_id] span = span_data.span - set_span_errored(span) sentry_sdk.capture_exception(error, span.scope) - span.__exit__(None, None, None) + span.__exit__(type(error), error, error.__traceback__) del self.span_map[run_id] def _normalize_langchain_message(self, message: "BaseMessage") -> "Any": @@ -1112,13 +1111,13 @@ def new_iterator() -> "Iterator[Any]": and integration.include_prompts ): set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, output) + + span.__exit__(None, None, None) except Exception: exc_info = sys.exc_info() - set_span_errored(span) + with capture_internal_exceptions(): + span.__exit__(*exc_info) raise - finally: - # Ensure cleanup happens even if iterator is abandoned or fails - span.__exit__(*exc_info) async def new_iterator_async() -> "AsyncIterator[Any]": exc_info: "tuple[Any, Any, Any]" = (None, None, None) @@ -1137,9 +1136,12 @@ async def new_iterator_async() -> "AsyncIterator[Any]": and integration.include_prompts ): set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, output) + + span.__exit__(None, None, None) except Exception: exc_info = sys.exc_info() - set_span_errored(span) + with capture_internal_exceptions(): + span.__exit__(*exc_info) raise finally: # Ensure cleanup happens even if iterator is abandoned or fails diff --git a/tests/integrations/langchain/test_langchain.py b/tests/integrations/langchain/test_langchain.py index a1872b56d2..69c58f5409 100644 --- a/tests/integrations/langchain/test_langchain.py +++ b/tests/integrations/langchain/test_langchain.py @@ -2561,8 +2561,6 @@ def _llm_type(self) -> str: assert transaction["spans"][0]["status"] == "internal_error" assert transaction["spans"][0]["tags"]["status"] == "internal_error" - assert transaction["contexts"]["trace"]["status"] == "internal_error" - def test_manual_callback_no_duplication(sentry_init): """ From 84a160a93b76bd05a4401c4daaedc34fba236e66 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Wed, 20 May 2026 10:26:33 +0200 Subject: [PATCH 2/2] remove finally --- sentry_sdk/integrations/langchain.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/sentry_sdk/integrations/langchain.py b/sentry_sdk/integrations/langchain.py index 4dab6c376c..dfd46649e9 100644 --- a/sentry_sdk/integrations/langchain.py +++ b/sentry_sdk/integrations/langchain.py @@ -1143,9 +1143,6 @@ async def new_iterator_async() -> "AsyncIterator[Any]": with capture_internal_exceptions(): span.__exit__(*exc_info) raise - finally: - # Ensure cleanup happens even if iterator is abandoned or fails - span.__exit__(*exc_info) if str(type(result)) == "": result = new_iterator_async()