Skip to content
Closed
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
6 changes: 4 additions & 2 deletions sentry_sdk/integrations/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
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.utils import capture_internal_exceptions, logger
from sentry_sdk.utils import capture_internal_exceptions, logger, safe_repr

if TYPE_CHECKING:
from typing import (
Expand Down Expand Up @@ -688,7 +688,9 @@
span = span_data.span

if should_send_default_pii() and self.include_prompts:
set_data_normalized(span, SPANDATA.GEN_AI_TOOL_OUTPUT, output)
set_data_normalized(
span, SPANDATA.GEN_AI_TOOL_OUTPUT, safe_repr(output)
)

Check warning on line 693 in sentry_sdk/integrations/langchain.py

View check run for this annotation

@sentry/warden / warden: code-review

safe_repr with set_data_normalized double-quotes string tool outputs

Using `safe_repr(output)` when `output` is a string adds Python repr quotes (e.g., `"hello"` becomes `"'hello'"`). The existing test at line 736 of test_langchain.py expects to call `int()` on the tool output - this will fail if the output `"5"` becomes `"'5'"`. Other integrations use `safe_repr` directly with `span.set_data()`, not with `set_data_normalized()` which already handles type conversion.
Comment on lines +691 to +693
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

safe_repr with set_data_normalized double-quotes string tool outputs

Using safe_repr(output) when output is a string adds Python repr quotes (e.g., "hello" becomes "'hello'"). The existing test at line 736 of test_langchain.py expects to call int() on the tool output - this will fail if the output "5" becomes "'5'". Other integrations use safe_repr directly with span.set_data(), not with set_data_normalized() which already handles type conversion.

Verification

Traced through safe_repr (sentry_sdk/utils.py line 562-566) which calls repr() on values. For strings, repr() adds quotes. Verified set_data_normalized (sentry_sdk/ai/utils.py lines 492-499) passes strings through unchanged. Confirmed existing test at tests/integrations/langchain/test_langchain.py:736 calls int() on tool output, which would fail with quoted strings.

Identified by Warden code-review · RLV-WJ8


self._exit_span(span_data, run_id)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sentry_sdk
from sentry_sdk.consts import OP, SPANDATA, SPANSTATUS
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.utils import safe_repr

from ..consts import SPAN_ORIGIN
from ..utils import _set_agent_data
Expand Down Expand Up @@ -47,7 +48,7 @@ def update_execute_tool_span(
span.set_status(SPANSTATUS.INTERNAL_ERROR)

if should_send_default_pii():
span.set_data(SPANDATA.GEN_AI_TOOL_OUTPUT, result)
span.set_data(SPANDATA.GEN_AI_TOOL_OUTPUT, safe_repr(result))

# Add conversation ID from agent
conv_id = getattr(agent, "_sentry_conversation_id", None)
Expand Down
4 changes: 2 additions & 2 deletions sentry_sdk/integrations/openai_agents/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from sentry_sdk.integrations import DidNotEnable
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.tracing_utils import set_span_errored
from sentry_sdk.utils import event_from_exception, safe_serialize
from sentry_sdk.utils import event_from_exception, safe_serialize, safe_repr
from sentry_sdk.ai._openai_completions_api import _transform_system_instructions
from sentry_sdk.ai._openai_responses_api import (
_is_system_instruction,
Expand Down Expand Up @@ -231,7 +231,7 @@ def _create_mcp_execute_tool_spans(
SPANDATA.GEN_AI_TOOL_INPUT, output.arguments
)
execute_tool_span.set_data(
SPANDATA.GEN_AI_TOOL_OUTPUT, output.output
SPANDATA.GEN_AI_TOOL_OUTPUT, safe_repr(output.output)
)
if output.error:
execute_tool_span.set_status(SPANSTATUS.INTERNAL_ERROR)
7 changes: 4 additions & 3 deletions tests/integrations/openai_agents/test_openai_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ def simple_test_tool(message: str) -> str:
assert tool_span["data"]["gen_ai.tool.description"] == "A simple tool"
assert tool_span["data"]["gen_ai.tool.input"] == '{"message": "hello"}'
assert tool_span["data"]["gen_ai.tool.name"] == "simple_test_tool"
assert tool_span["data"]["gen_ai.tool.output"] == "Tool executed with: hello"
assert tool_span["data"]["gen_ai.tool.output"] == "'Tool executed with: hello'"
assert ai_client_span2["description"] == "chat gpt-4"
assert ai_client_span2["data"]["gen_ai.agent.name"] == "test_agent"
assert ai_client_span2["data"]["gen_ai.operation.name"] == "chat"
Expand Down Expand Up @@ -1904,7 +1904,8 @@ async def test_mcp_tool_execution_spans(
assert mcp_tool_span["data"]["gen_ai.tool.name"] == "test_mcp_tool"
assert mcp_tool_span["data"]["gen_ai.tool.input"] == '{"query": "search term"}'
assert (
mcp_tool_span["data"]["gen_ai.tool.output"] == "MCP tool executed successfully"
mcp_tool_span["data"]["gen_ai.tool.output"]
== "'MCP tool executed successfully'"
)

# Verify no error status since error was None
Expand Down Expand Up @@ -2031,7 +2032,7 @@ async def test_mcp_tool_execution_with_error(
assert mcp_tool_span["description"] == "execute_tool failing_mcp_tool"
assert mcp_tool_span["data"]["gen_ai.tool.name"] == "failing_mcp_tool"
assert mcp_tool_span["data"]["gen_ai.tool.input"] == '{"query": "test"}'
assert mcp_tool_span["data"]["gen_ai.tool.output"] is None
assert mcp_tool_span["data"]["gen_ai.tool.output"] == "None"

# Verify error status was set
assert mcp_tool_span["status"] == "internal_error"
Expand Down
Loading