Skip to content

Commit 014add2

Browse files
simplify openai-agents
1 parent 5b1480a commit 014add2

1 file changed

Lines changed: 64 additions & 77 deletions

File tree

tests/integrations/openai_agents/test_openai_agents.py

Lines changed: 64 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -102,45 +102,6 @@ def mock_usage():
102102
)
103103

104104

105-
@pytest.fixture
106-
def mock_model_response():
107-
return Response(
108-
id="resp_123",
109-
output=[
110-
ResponseOutputMessage(
111-
id="msg_123",
112-
type="message",
113-
status="completed",
114-
content=[
115-
ResponseOutputText(
116-
text="Hello, how can I help you?",
117-
type="output_text",
118-
annotations=[],
119-
)
120-
],
121-
role="assistant",
122-
)
123-
],
124-
parallel_tool_calls=False,
125-
tool_choice="none",
126-
tools=[],
127-
created_at=10000000,
128-
model="gpt-4",
129-
object="response",
130-
usage=ResponseUsage(
131-
input_tokens=10,
132-
input_tokens_details=InputTokensDetails(
133-
cached_tokens=0,
134-
),
135-
output_tokens=20,
136-
output_tokens_details=OutputTokensDetails(
137-
reasoning_tokens=5,
138-
),
139-
total_tokens=30,
140-
),
141-
)
142-
143-
144105
@pytest.fixture
145106
def test_agent():
146107
"""Create a real Agent instance for testing."""
@@ -198,13 +159,19 @@ def test_agent_custom_model():
198159

199160
@pytest.mark.asyncio
200161
async def test_agent_invocation_span_no_pii(
201-
sentry_init, capture_events, test_agent, mock_model_response, get_model_response
162+
sentry_init,
163+
capture_events,
164+
test_agent,
165+
nonstreaming_responses_model_response,
166+
get_model_response,
202167
):
203168
client = AsyncOpenAI(api_key="test-key")
204169
model = OpenAIResponsesModel(model="gpt-4", openai_client=client)
205170
agent = test_agent.clone(model=model)
206171

207-
response = get_model_response(mock_model_response, serialize_pydantic=True)
172+
response = get_model_response(
173+
nonstreaming_responses_model_response, serialize_pydantic=True
174+
)
208175

209176
with patch.object(
210177
agent.model._client._client,
@@ -340,7 +307,7 @@ async def test_agent_invocation_span(
340307
sentry_init,
341308
capture_events,
342309
test_agent_with_instructions,
343-
mock_model_response,
310+
nonstreaming_responses_model_response,
344311
instructions,
345312
input,
346313
request,
@@ -353,7 +320,9 @@ async def test_agent_invocation_span(
353320
model = OpenAIResponsesModel(model="gpt-4", openai_client=client)
354321
agent = test_agent_with_instructions(instructions).clone(model=model)
355322

356-
response = get_model_response(mock_model_response, serialize_pydantic=True)
323+
response = get_model_response(
324+
nonstreaming_responses_model_response, serialize_pydantic=True
325+
)
357326

358327
with patch.object(
359328
agent.model._client._client,
@@ -503,7 +472,7 @@ async def test_client_span_custom_model(
503472
sentry_init,
504473
capture_events,
505474
test_agent_custom_model,
506-
mock_model_response,
475+
nonstreaming_responses_model_response,
507476
get_model_response,
508477
):
509478
"""
@@ -514,7 +483,9 @@ async def test_client_span_custom_model(
514483
model = OpenAIResponsesModel(model="my-custom-model", openai_client=client)
515484
agent = test_agent_custom_model.clone(model=model)
516485

517-
response = get_model_response(mock_model_response, serialize_pydantic=True)
486+
response = get_model_response(
487+
nonstreaming_responses_model_response, serialize_pydantic=True
488+
)
518489

519490
with patch.object(
520491
agent.model._client._client,
@@ -547,7 +518,7 @@ def test_agent_invocation_span_sync_no_pii(
547518
sentry_init,
548519
capture_events,
549520
test_agent,
550-
mock_model_response,
521+
nonstreaming_responses_model_response,
551522
get_model_response,
552523
):
553524
"""
@@ -557,7 +528,9 @@ def test_agent_invocation_span_sync_no_pii(
557528
model = OpenAIResponsesModel(model="gpt-4", openai_client=client)
558529
agent = test_agent.clone(model=model)
559530

560-
response = get_model_response(mock_model_response, serialize_pydantic=True)
531+
response = get_model_response(
532+
nonstreaming_responses_model_response, serialize_pydantic=True
533+
)
561534

562535
with patch.object(
563536
agent.model._client._client,
@@ -687,7 +660,7 @@ def test_agent_invocation_span_sync(
687660
sentry_init,
688661
capture_events,
689662
test_agent_with_instructions,
690-
mock_model_response,
663+
nonstreaming_responses_model_response,
691664
instructions,
692665
input,
693666
request,
@@ -700,7 +673,9 @@ def test_agent_invocation_span_sync(
700673
model = OpenAIResponsesModel(model="gpt-4", openai_client=client)
701674
agent = test_agent_with_instructions(instructions).clone(model=model)
702675

703-
response = get_model_response(mock_model_response, serialize_pydantic=True)
676+
response = get_model_response(
677+
nonstreaming_responses_model_response, serialize_pydantic=True
678+
)
704679

705680
with patch.object(
706681
agent.model._client._client,
@@ -1370,7 +1345,11 @@ def simple_test_tool(message: str) -> str:
13701345

13711346
@pytest.mark.asyncio
13721347
async def test_hosted_mcp_tool_propagation_header_streamed(
1373-
sentry_init, test_agent, async_iterator, server_side_event_chunks
1348+
sentry_init,
1349+
test_agent,
1350+
get_model_response,
1351+
async_iterator,
1352+
server_side_event_chunks,
13741353
):
13751354
"""
13761355
Test responses API is given trace propagation headers with HostedMCPTool.
@@ -1402,24 +1381,18 @@ async def test_hosted_mcp_tool_propagation_header_streamed(
14021381
release="d08ebdb9309e1b004c6f52202de58a09c2268e42",
14031382
)
14041383

1405-
request = httpx.Request(
1406-
"POST",
1407-
"/responses",
1408-
)
1409-
1384+
request_headers = {}
14101385
# openai-agents calls with_streaming_response() if available starting with
14111386
# https://github.com/openai/openai-agents-python/commit/159beb56130f7d85192acfd593c9168757984dc0.
14121387
# When using with_streaming_response() the header set below changes the response type:
14131388
# https://github.com/openai/openai-python/blob/656e3cab4a18262a49b961d41293367e45ee71b9/src/openai/_response.py#L67.
14141389
if parse_version(OPENAI_AGENTS_VERSION) >= (0, 10, 3) and hasattr(
14151390
agent_with_tool.model._client.responses, "with_streaming_response"
14161391
):
1417-
request.headers["X-Stainless-Raw-Response"] = "stream"
1392+
request_headers["X-Stainless-Raw-Response"] = "stream"
14181393

1419-
response = httpx.Response(
1420-
200,
1421-
request=request,
1422-
content=async_iterator(
1394+
response = get_model_response(
1395+
async_iterator(
14231396
server_side_event_chunks(
14241397
[
14251398
ResponseCreatedEvent(
@@ -1478,6 +1451,7 @@ async def test_hosted_mcp_tool_propagation_header_streamed(
14781451
]
14791452
)
14801453
),
1454+
request_headers=request_headers,
14811455
)
14821456

14831457
# Patching https://github.com/openai/openai-python/blob/656e3cab4a18262a49b961d41293367e45ee71b9/src/openai/_base_client.py#L1604
@@ -2230,7 +2204,11 @@ async def test_mcp_tool_execution_without_pii(
22302204

22312205
@pytest.mark.asyncio
22322206
async def test_multiple_agents_asyncio(
2233-
sentry_init, capture_events, test_agent, mock_model_response, get_model_response
2207+
sentry_init,
2208+
capture_events,
2209+
test_agent,
2210+
nonstreaming_responses_model_response,
2211+
get_model_response,
22342212
):
22352213
"""
22362214
Test that multiple agents can be run at the same time in asyncio tasks
@@ -2240,7 +2218,9 @@ async def test_multiple_agents_asyncio(
22402218
model = OpenAIResponsesModel(model="gpt-4", openai_client=client)
22412219
agent = test_agent.clone(model=model)
22422220

2243-
response = get_model_response(mock_model_response, serialize_pydantic=True)
2221+
response = get_model_response(
2222+
nonstreaming_responses_model_response, serialize_pydantic=True
2223+
)
22442224

22452225
with patch.object(
22462226
agent.model._client._client,
@@ -3154,7 +3134,11 @@ async def test_streaming_span_update_captures_response_data(
31543134

31553135
@pytest.mark.asyncio
31563136
async def test_streaming_ttft_on_chat_span(
3157-
sentry_init, test_agent, async_iterator, server_side_event_chunks
3137+
sentry_init,
3138+
test_agent,
3139+
get_model_response,
3140+
async_iterator,
3141+
server_side_event_chunks,
31583142
):
31593143
"""
31603144
Test that time-to-first-token (TTFT) is recorded on chat spans during streaming.
@@ -3182,24 +3166,18 @@ async def test_streaming_ttft_on_chat_span(
31823166
traces_sample_rate=1.0,
31833167
)
31843168

3185-
request = httpx.Request(
3186-
"POST",
3187-
"/responses",
3188-
)
3189-
3169+
request_headers = {}
31903170
# openai-agents calls with_streaming_response() if available starting with
31913171
# https://github.com/openai/openai-agents-python/commit/159beb56130f7d85192acfd593c9168757984dc0.
31923172
# When using with_streaming_response() the header set below changes the response type:
31933173
# https://github.com/openai/openai-python/blob/656e3cab4a18262a49b961d41293367e45ee71b9/src/openai/_response.py#L67.
31943174
if parse_version(OPENAI_AGENTS_VERSION) >= (0, 10, 3) and hasattr(
31953175
agent_with_tool.model._client.responses, "with_streaming_response"
31963176
):
3197-
request.headers["X-Stainless-Raw-Response"] = "stream"
3177+
request_headers["X-Stainless-Raw-Response"] = "stream"
31983178

3199-
response = httpx.Response(
3200-
200,
3201-
request=request,
3202-
content=async_iterator(
3179+
response = get_model_response(
3180+
async_iterator(
32033181
server_side_event_chunks(
32043182
[
32053183
ResponseCreatedEvent(
@@ -3276,6 +3254,7 @@ async def test_streaming_ttft_on_chat_span(
32763254
]
32773255
)
32783256
),
3257+
request_headers=request_headers,
32793258
)
32803259

32813260
# Patching https://github.com/openai/openai-python/blob/656e3cab4a18262a49b961d41293367e45ee71b9/src/openai/_base_client.py#L1604
@@ -3313,7 +3292,11 @@ async def test_streaming_ttft_on_chat_span(
33133292
)
33143293
@pytest.mark.asyncio
33153294
async def test_conversation_id_on_all_spans(
3316-
sentry_init, capture_events, test_agent, mock_model_response, get_model_response
3295+
sentry_init,
3296+
capture_events,
3297+
test_agent,
3298+
nonstreaming_responses_model_response,
3299+
get_model_response,
33173300
):
33183301
"""
33193302
Test that gen_ai.conversation.id is set on all AI-related spans when passed to Runner.run().
@@ -3323,7 +3306,9 @@ async def test_conversation_id_on_all_spans(
33233306
model = OpenAIResponsesModel(model="gpt-4", openai_client=client)
33243307
agent = test_agent.clone(model=model)
33253308

3326-
response = get_model_response(mock_model_response, serialize_pydantic=True)
3309+
response = get_model_response(
3310+
nonstreaming_responses_model_response, serialize_pydantic=True
3311+
)
33273312

33283313
with patch.object(
33293314
agent.model._client._client,
@@ -3508,7 +3493,7 @@ async def test_no_conversation_id_when_not_provided(
35083493
sentry_init,
35093494
capture_events,
35103495
test_agent,
3511-
mock_model_response,
3496+
nonstreaming_responses_model_response,
35123497
get_model_response,
35133498
):
35143499
"""
@@ -3519,7 +3504,9 @@ async def test_no_conversation_id_when_not_provided(
35193504
model = OpenAIResponsesModel(model="gpt-4", openai_client=client)
35203505
agent = test_agent.clone(model=model)
35213506

3522-
response = get_model_response(mock_model_response, serialize_pydantic=True)
3507+
response = get_model_response(
3508+
nonstreaming_responses_model_response, serialize_pydantic=True
3509+
)
35233510

35243511
with patch.object(
35253512
agent.model._client._client,

0 commit comments

Comments
 (0)