@@ -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
668722async 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