Skip to content

Commit 659bd84

Browse files
authored
fix(openai-agents): add input messages to errored spans as well (#5077)
1 parent 26391d6 commit 659bd84

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

sentry_sdk/integrations/openai_agents/spans/ai_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ def ai_client_span(agent, get_response_kwargs):
3030
span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
3131

3232
_set_agent_data(span, agent)
33+
_set_input_data(span, get_response_kwargs)
3334

3435
return span
3536

3637

3738
def update_ai_client_span(span, agent, get_response_kwargs, result):
3839
# type: (sentry_sdk.tracing.Span, Agent, dict[str, Any], Any) -> None
3940
_set_usage_data(span, result.usage)
40-
_set_input_data(span, get_response_kwargs)
4141
_set_output_data(span, result)
4242
_create_mcp_execute_tool_spans(span, result)

tests/integrations/openai_agents/test_openai_agents.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,60 @@ async def test_error_handling(sentry_init, capture_events, test_agent):
664664
assert ai_client_span["tags"]["status"] == "internal_error"
665665

666666

667+
@pytest.mark.asyncio
668+
async def test_error_captures_input_data(sentry_init, capture_events, test_agent):
669+
"""
670+
Test that input data is captured even when the API call raises an exception.
671+
This verifies that _set_input_data is called before the API call.
672+
"""
673+
with patch.dict(os.environ, {"OPENAI_API_KEY": "test-key"}):
674+
with patch(
675+
"agents.models.openai_responses.OpenAIResponsesModel.get_response"
676+
) as mock_get_response:
677+
mock_get_response.side_effect = Exception("API Error")
678+
679+
sentry_init(
680+
integrations=[OpenAIAgentsIntegration()],
681+
traces_sample_rate=1.0,
682+
send_default_pii=True,
683+
)
684+
685+
events = capture_events()
686+
687+
with pytest.raises(Exception, match="API Error"):
688+
await agents.Runner.run(
689+
test_agent, "Test input", run_config=test_run_config
690+
)
691+
692+
(
693+
error_event,
694+
transaction,
695+
) = events
696+
697+
assert error_event["exception"]["values"][0]["type"] == "Exception"
698+
assert error_event["exception"]["values"][0]["value"] == "API Error"
699+
700+
spans = transaction["spans"]
701+
ai_client_span = [s for s in spans if s["op"] == "gen_ai.chat"][0]
702+
703+
assert ai_client_span["description"] == "chat gpt-4"
704+
assert ai_client_span["tags"]["status"] == "internal_error"
705+
706+
assert "gen_ai.request.messages" in ai_client_span["data"]
707+
request_messages = safe_serialize(
708+
[
709+
{
710+
"role": "system",
711+
"content": [
712+
{"type": "text", "text": "You are a helpful test assistant."}
713+
],
714+
},
715+
{"role": "user", "content": [{"type": "text", "text": "Test input"}]},
716+
]
717+
)
718+
assert ai_client_span["data"]["gen_ai.request.messages"] == request_messages
719+
720+
667721
@pytest.mark.asyncio
668722
async def test_span_status_error(sentry_init, capture_events, test_agent):
669723
with patch.dict(os.environ, {"OPENAI_API_KEY": "test-key"}):

0 commit comments

Comments
 (0)