diff --git a/sentry_sdk/integrations/langchain.py b/sentry_sdk/integrations/langchain.py index 49fa04c034..46bcb39fc0 100644 --- a/sentry_sdk/integrations/langchain.py +++ b/sentry_sdk/integrations/langchain.py @@ -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 ( @@ -688,7 +688,9 @@ def on_tool_end( 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) + ) self._exit_span(span_data, run_id) diff --git a/sentry_sdk/integrations/openai_agents/spans/execute_tool.py b/sentry_sdk/integrations/openai_agents/spans/execute_tool.py index 26072cfdba..12a4fe30a5 100644 --- a/sentry_sdk/integrations/openai_agents/spans/execute_tool.py +++ b/sentry_sdk/integrations/openai_agents/spans/execute_tool.py @@ -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 @@ -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) diff --git a/sentry_sdk/integrations/openai_agents/utils.py b/sentry_sdk/integrations/openai_agents/utils.py index ee504b3496..7acc3506be 100644 --- a/sentry_sdk/integrations/openai_agents/utils.py +++ b/sentry_sdk/integrations/openai_agents/utils.py @@ -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, @@ -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) diff --git a/tests/integrations/openai_agents/test_openai_agents.py b/tests/integrations/openai_agents/test_openai_agents.py index 7310e86df5..b7038dcfb3 100644 --- a/tests/integrations/openai_agents/test_openai_agents.py +++ b/tests/integrations/openai_agents/test_openai_agents.py @@ -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" @@ -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 @@ -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"