Skip to content
Merged
Show file tree
Hide file tree
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
21 changes: 10 additions & 11 deletions sentry_sdk/integrations/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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
Comment thread
alexander-alderman-webb marked this conversation as resolved.
span.__exit__(*exc_info)

async def new_iterator_async() -> "AsyncIterator[Any]":
Comment thread
sentry[bot] marked this conversation as resolved.
exc_info: "tuple[Any, Any, Any]" = (None, None, None)
Expand All @@ -1137,13 +1136,13 @@ 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)
Comment thread
cursor[bot] marked this conversation as resolved.
raise
finally:
# Ensure cleanup happens even if iterator is abandoned or fails
span.__exit__(*exc_info)

if str(type(result)) == "<class 'async_generator'>":
result = new_iterator_async()
Expand Down
2 changes: 0 additions & 2 deletions tests/integrations/langchain/test_langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
Loading