@@ -4521,3 +4521,165 @@ async def test_no_conversation_id_when_not_provided(
45214521 )
45224522 assert "gen_ai.conversation.id" not in invoke_agent_span .get ("data" , {})
45234523 assert "gen_ai.conversation.id" not in ai_client_span .get ("data" , {})
4524+
4525+
4526+ @pytest .mark .parametrize ("stream_gen_ai_spans" , [True , False ])
4527+ @pytest .mark .asyncio
4528+ async def test_runner_run_with_starting_agent_kwarg (
4529+ sentry_init ,
4530+ capture_events ,
4531+ test_agent ,
4532+ nonstreaming_responses_model_response ,
4533+ get_model_response ,
4534+ stream_gen_ai_spans ,
4535+ ):
4536+ """Runner.run(starting_agent=agent, input=...) must not crash.
4537+
4538+ Regression test for https://github.com/getsentry/sentry-python/issues/6418
4539+ """
4540+ client = AsyncOpenAI (api_key = "test-key" )
4541+ model = OpenAIResponsesModel (model = "gpt-4" , openai_client = client )
4542+ agent = test_agent .clone (model = model )
4543+
4544+ response = get_model_response (
4545+ nonstreaming_responses_model_response , serialize_pydantic = True
4546+ )
4547+
4548+ with patch .object (
4549+ agent .model ._client ._client ,
4550+ "send" ,
4551+ return_value = response ,
4552+ ):
4553+ sentry_init (
4554+ integrations = [OpenAIAgentsIntegration ()],
4555+ traces_sample_rate = 1.0 ,
4556+ stream_gen_ai_spans = stream_gen_ai_spans ,
4557+ )
4558+
4559+ events = capture_events ()
4560+
4561+ result = await agents .run .DEFAULT_AGENT_RUNNER .run (
4562+ starting_agent = agent ,
4563+ input = "Test input" ,
4564+ run_config = test_run_config ,
4565+ )
4566+
4567+ assert result is not None
4568+ assert result .final_output == "Hello, how can I help you?"
4569+
4570+ (transaction ,) = events
4571+ assert transaction ["transaction" ] == "test_agent workflow"
4572+
4573+
4574+ @pytest .mark .parametrize ("stream_gen_ai_spans" , [True , False ])
4575+ @pytest .mark .asyncio
4576+ async def test_runner_run_streamed_with_starting_agent_kwarg (
4577+ sentry_init ,
4578+ capture_events ,
4579+ test_agent ,
4580+ async_iterator ,
4581+ server_side_event_chunks ,
4582+ get_model_response ,
4583+ stream_gen_ai_spans ,
4584+ ):
4585+ """Runner.run_streamed(starting_agent=agent, input=...) must not crash.
4586+
4587+ Regression test for https://github.com/getsentry/sentry-python/issues/6418
4588+ """
4589+ client = AsyncOpenAI (api_key = "test-key" )
4590+ model = OpenAIResponsesModel (model = "gpt-4" , openai_client = client )
4591+ agent = test_agent .clone (model = model )
4592+
4593+ request_headers = {}
4594+ if parse_version (OPENAI_AGENTS_VERSION ) >= (0 , 10 , 3 ) and hasattr (
4595+ agent .model ._client .responses , "with_streaming_response"
4596+ ):
4597+ request_headers ["X-Stainless-Raw-Response" ] = "stream"
4598+
4599+ response = get_model_response (
4600+ async_iterator (
4601+ server_side_event_chunks (
4602+ [
4603+ ResponseCreatedEvent (
4604+ response = Response (
4605+ id = "chat-id" ,
4606+ output = [],
4607+ parallel_tool_calls = False ,
4608+ tool_choice = "none" ,
4609+ tools = [],
4610+ created_at = 10000000 ,
4611+ model = "gpt-4" ,
4612+ object = "response" ,
4613+ ),
4614+ type = "response.created" ,
4615+ sequence_number = 0 ,
4616+ ),
4617+ ResponseCompletedEvent (
4618+ response = Response (
4619+ id = "chat-id" ,
4620+ output = [
4621+ ResponseOutputMessage (
4622+ id = "message-id" ,
4623+ content = [
4624+ ResponseOutputText (
4625+ annotations = [],
4626+ text = "Hello, how can I help you?" ,
4627+ type = "output_text" ,
4628+ ),
4629+ ],
4630+ role = "assistant" ,
4631+ status = "completed" ,
4632+ type = "message" ,
4633+ ),
4634+ ],
4635+ parallel_tool_calls = False ,
4636+ tool_choice = "none" ,
4637+ tools = [],
4638+ created_at = 10000000 ,
4639+ model = "gpt-4" ,
4640+ object = "response" ,
4641+ usage = ResponseUsage (
4642+ input_tokens = 10 ,
4643+ input_tokens_details = InputTokensDetails (
4644+ cached_tokens = 0 ,
4645+ ),
4646+ output_tokens = 20 ,
4647+ output_tokens_details = OutputTokensDetails (
4648+ reasoning_tokens = 5 ,
4649+ ),
4650+ total_tokens = 30 ,
4651+ ),
4652+ ),
4653+ type = "response.completed" ,
4654+ sequence_number = 1 ,
4655+ ),
4656+ ]
4657+ )
4658+ ),
4659+ request_headers = request_headers ,
4660+ )
4661+
4662+ with patch .object (
4663+ agent .model ._client ._client ,
4664+ "send" ,
4665+ return_value = response ,
4666+ ):
4667+ sentry_init (
4668+ integrations = [OpenAIAgentsIntegration ()],
4669+ traces_sample_rate = 1.0 ,
4670+ stream_gen_ai_spans = stream_gen_ai_spans ,
4671+ )
4672+
4673+ events = capture_events ()
4674+
4675+ result = agents .run .DEFAULT_AGENT_RUNNER .run_streamed (
4676+ starting_agent = agent ,
4677+ input = "Test input" ,
4678+ run_config = test_run_config ,
4679+ )
4680+
4681+ async for _event in result .stream_events ():
4682+ pass
4683+
4684+ (transaction ,) = events
4685+ assert transaction ["transaction" ] == "test_agent workflow"
0 commit comments