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
66 changes: 27 additions & 39 deletions sentry_sdk/integrations/langgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

# This is fine because langgraph depends on langchain-base, and LangchainIntegration only imports from langchain-base.
from sentry_sdk.integrations.langchain import LangchainIntegration
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.utils import (
has_data_collection_enabled,
package_version,
safe_serialize,
)
Expand Down Expand Up @@ -51,24 +49,6 @@ def setup_once() -> None:
Pregel.ainvoke = _wrap_pregel_ainvoke(Pregel.ainvoke)


def _should_record_inputs(integration: "LanggraphIntegration") -> bool:
client = sentry_sdk.get_client()
if has_data_collection_enabled(client.options):
return bool(client.options["data_collection"]["gen_ai"]["inputs"])

# To remove once data collection has been fully rolled out
return should_send_default_pii()


def _should_record_outputs(integration: "LanggraphIntegration") -> bool:
client = sentry_sdk.get_client()
if has_data_collection_enabled(client.options):
return bool(client.options["data_collection"]["gen_ai"]["outputs"])

# To remove once data collection has been fully rolled out
return should_send_default_pii()


def _get_graph_name(graph_obj: "Any") -> "Optional[str]":
for attr in ["name", "graph_name", "__name__", "_name"]:
if hasattr(graph_obj, attr):
Expand Down Expand Up @@ -156,7 +136,10 @@ def new_invoke(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
input_messages = None
if len(args) > 0:
input_messages = _parse_langgraph_messages(args[0])
if input_messages and _should_record_inputs(integration):
if (
input_messages
and client.options["data_collection"]["gen_ai"]["inputs"]
):
normalized_input_messages = normalize_message_roles(input_messages)
set_data_normalized(
span,
Expand Down Expand Up @@ -199,7 +182,10 @@ async def new_ainvoke(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
input_messages = None
if len(args) > 0:
input_messages = _parse_langgraph_messages(args[0])
if input_messages and _should_record_inputs(integration):
if (
input_messages
and client.options["data_collection"]["gen_ai"]["inputs"]
):
normalized_input_messages = normalize_message_roles(input_messages)
set_data_normalized(
span,
Expand Down Expand Up @@ -277,20 +263,22 @@ def _set_response_attributes(
if new_messages is None:
return

if _should_record_outputs(integration):
llm_response_text = _extract_llm_response_text(new_messages)
if llm_response_text:
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, llm_response_text)
elif new_messages:
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, new_messages)
else:
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, result)

tool_calls = _extract_tool_calls(new_messages)
if tool_calls:
set_data_normalized(
span,
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
safe_serialize(tool_calls),
unpack=False,
)
if not sentry_sdk.get_client().options["data_collection"]["gen_ai"]["outputs"]:
return

llm_response_text = _extract_llm_response_text(new_messages)
if llm_response_text:
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, llm_response_text)
elif new_messages:
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, new_messages)
else:
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, result)

tool_calls = _extract_tool_calls(new_messages)
if tool_calls:
set_data_normalized(
span,
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
safe_serialize(tool_calls),
unpack=False,
)
Loading
Loading