Skip to content
Open
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ typing = [
"starlette>=1.3.1",
"python-multipart>=0.0.32",
"pydantic>=2.13.4",
"langchain-core>=1.5.3",
]
test = [
"dataclasses ; python_full_version < '3.7'",
Expand Down
66 changes: 27 additions & 39 deletions sentry_sdk/integrations/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
manager,
)
from langchain_core.messages import BaseMessage
from langchain_core.outputs import LLMResult
from langchain_core.outputs import ChatGeneration, LLMResult

except ImportError:
raise DidNotEnable("langchain not installed")
Expand Down Expand Up @@ -247,7 +247,7 @@ def setup_once() -> None:
_patch_embeddings_provider(OllamaEmbeddings)


class SentryLangchainCallback(BaseCallbackHandler): # type: ignore[misc]
class SentryLangchainCallback(BaseCallbackHandler):
"""Callback handler that creates Sentry spans."""

def __init__(
Expand Down Expand Up @@ -561,44 +561,32 @@ def on_llm_end(
except IndexError:
generation = None

if generation is not None:
set_on_span = (
span.set_attribute
if isinstance(span, StreamedSpan)
else span.set_data
)
set_on_span = (
span.set_attribute if isinstance(span, StreamedSpan) else span.set_data
)

try:
response_model = generation.message.response_metadata.get(
"model_name"
if generation is not None and generation.generation_info is not None:
finish_reason = generation.generation_info.get("finish_reason")
if finish_reason is not None:
set_on_span(
SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS,
[finish_reason],
Comment thread
cursor[bot] marked this conversation as resolved.
)
if response_model is not None:
set_on_span(SPANDATA.GEN_AI_RESPONSE_MODEL, response_model)
except AttributeError:
pass

try:
Comment thread
cursor[bot] marked this conversation as resolved.
finish_reason = generation.generation_info.get("finish_reason")
if finish_reason is not None:
set_on_span(
SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS,
[finish_reason],
)
except AttributeError:
pass
if isinstance(generation, ChatGeneration):
response_model = generation.message.response_metadata.get("model_name")
if response_model is not None:
set_on_span(SPANDATA.GEN_AI_RESPONSE_MODEL, response_model)

try:
if should_send_default_pii() and self.include_prompts:
tool_calls = getattr(generation.message, "tool_calls", None)
if tool_calls is not None and tool_calls != []:
set_data_normalized(
span,
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
tool_calls,
unpack=False,
)
except AttributeError:
pass
if should_send_default_pii() and self.include_prompts:
tool_calls = getattr(generation.message, "tool_calls", None)
if tool_calls is not None and tool_calls != []:
set_data_normalized(
span,
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
tool_calls,
unpack=False,
)

if should_send_default_pii() and self.include_prompts:
set_data_normalized(
Expand All @@ -612,7 +600,7 @@ def on_llm_end(

def on_llm_error(
self: "SentryLangchainCallback",
error: "Union[Exception, KeyboardInterrupt]",
error: "BaseException",
*,
run_id: "UUID",
**kwargs: "Any",
Expand Down Expand Up @@ -719,8 +707,8 @@ def on_tool_end(

def on_tool_error(
self,
error: "SentryLangchainCallback",
*args: "Union[Exception, KeyboardInterrupt]",
error: "BaseException",
*,
run_id: "UUID",
**kwargs: "Any",
) -> "Any":
Expand Down
Loading
Loading