Skip to content

Commit e52c3b1

Browse files
authored
fix(langchain): don't record tool call output if not include_prompt / should_send_default_pii (#4836)
1 parent 3f4e2a8 commit e52c3b1

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

sentry_sdk/integrations/langchain.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,15 @@ def on_llm_end(self, response, *, run_id, **kwargs):
322322
pass
323323

324324
try:
325-
tool_calls = getattr(generation.message, "tool_calls", None)
326-
if tool_calls is not None and tool_calls != []:
327-
set_data_normalized(
328-
span,
329-
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
330-
tool_calls,
331-
unpack=False,
332-
)
325+
if should_send_default_pii() and self.include_prompts:
326+
tool_calls = getattr(generation.message, "tool_calls", None)
327+
if tool_calls is not None and tool_calls != []:
328+
set_data_normalized(
329+
span,
330+
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
331+
tool_calls,
332+
unpack=False,
333+
)
333334
except AttributeError:
334335
pass
335336

tests/integrations/langchain/test_langchain.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,19 @@ def test_langchain_agent(
221221
in chat_spans[1]["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
222222
)
223223
assert "5" in chat_spans[1]["data"][SPANDATA.GEN_AI_RESPONSE_TEXT]
224+
225+
# Verify tool calls are recorded when PII is enabled
226+
assert SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS in chat_spans[0].get(
227+
"data", {}
228+
), "Tool calls should be recorded when send_default_pii=True and include_prompts=True"
229+
tool_calls_data = chat_spans[0]["data"][SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS]
230+
assert isinstance(tool_calls_data, (list, str)) # Could be serialized
231+
if isinstance(tool_calls_data, str):
232+
assert "get_word_length" in tool_calls_data
233+
elif isinstance(tool_calls_data, list) and len(tool_calls_data) > 0:
234+
# Check if tool calls contain expected function name
235+
tool_call_str = str(tool_calls_data)
236+
assert "get_word_length" in tool_call_str
224237
else:
225238
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in chat_spans[0].get("data", {})
226239
assert SPANDATA.GEN_AI_RESPONSE_TEXT not in chat_spans[0].get("data", {})
@@ -229,6 +242,29 @@ def test_langchain_agent(
229242
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in tool_exec_span.get("data", {})
230243
assert SPANDATA.GEN_AI_RESPONSE_TEXT not in tool_exec_span.get("data", {})
231244

245+
# Verify tool calls are NOT recorded when PII is disabled
246+
assert SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS not in chat_spans[0].get(
247+
"data", {}
248+
), (
249+
f"Tool calls should NOT be recorded when send_default_pii={send_default_pii} "
250+
f"and include_prompts={include_prompts}"
251+
)
252+
assert SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS not in chat_spans[1].get(
253+
"data", {}
254+
), (
255+
f"Tool calls should NOT be recorded when send_default_pii={send_default_pii} "
256+
f"and include_prompts={include_prompts}"
257+
)
258+
259+
# Verify that available tools are always recorded regardless of PII settings
260+
for chat_span in chat_spans:
261+
span_data = chat_span.get("data", {})
262+
if SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS in span_data:
263+
tools_data = span_data[SPANDATA.GEN_AI_REQUEST_AVAILABLE_TOOLS]
264+
assert (
265+
tools_data is not None
266+
), "Available tools should always be recorded regardless of PII settings"
267+
232268

233269
def test_langchain_error(sentry_init, capture_events):
234270
sentry_init(

0 commit comments

Comments
 (0)