Skip to content

Commit 0102259

Browse files
patch further down call stack
1 parent 0de90f0 commit 0102259

1 file changed

Lines changed: 27 additions & 36 deletions

File tree

tests/integrations/openai_agents/test_openai_agents.py

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,6 @@ async def test_hosted_mcp_tool_propagation_header_streamed(
12301230
)
12311231

12321232
client = AsyncOpenAI(api_key="z")
1233-
client.responses._post = AsyncMock(return_value=EXAMPLE_RESPONSE)
12341233

12351234
model = OpenAIResponsesModel(model="gpt-4", openai_client=client)
12361235

@@ -1245,8 +1244,14 @@ async def test_hosted_mcp_tool_propagation_header_streamed(
12451244
release="d08ebdb9309e1b004c6f52202de58a09c2268e42",
12461245
)
12471246

1247+
request = httpx.Request(
1248+
"POST",
1249+
"/responses",
1250+
)
1251+
12481252
response = httpx.Response(
12491253
200,
1254+
request=request,
12501255
content=async_iterator(
12511256
sse_chunks(
12521257
[
@@ -1308,22 +1313,10 @@ async def test_hosted_mcp_tool_propagation_header_streamed(
13081313
),
13091314
)
13101315

1311-
# Emulate https://github.com/openai/openai-python/blob/656e3cab4a18262a49b961d41293367e45ee71b9/src/openai/_base_client.py#L1751
1312-
api_response = AsyncAPIResponse(
1313-
raw=response,
1314-
cast_to=Response,
1315-
client=client,
1316-
stream=True,
1317-
stream_cls=AsyncStream[ResponseStreamEvent],
1318-
options=FinalRequestOptions.construct(method="post", url="/responses"),
1319-
retries_taken=0,
1320-
)
1321-
13221316
with patch.object(
1323-
model._client.responses,
1324-
"create",
1325-
# based on https://github.com/openai/openai-python/blob/656e3cab4a18262a49b961d41293367e45ee71b9/src/openai/_base_client.py#L1763
1326-
return_value=await api_response.parse(),
1317+
agent_with_tool.model._client._client,
1318+
"send",
1319+
return_value=response,
13271320
) as create, mock.patch(
13281321
"sentry_sdk.tracing_utils.Random.randrange", return_value=500000
13291322
):
@@ -1341,13 +1334,17 @@ async def test_hosted_mcp_tool_propagation_header_streamed(
13411334
async for event in result.stream_events():
13421335
pass
13431336

1344-
ai_client_span = transaction._span_recorder.spans[-1]
1337+
ai_client_span = next(
1338+
span
1339+
for span in transaction._span_recorder.spans
1340+
if span.op == "gen_ai.chat"
1341+
)
13451342

13461343
args, kwargs = create.call_args
13471344

1348-
assert "tools" in kwargs
1349-
assert len(kwargs["tools"]) == 1
1350-
hosted_mcp_tool = kwargs["tools"][0]
1345+
request = args[0]
1346+
body = json.loads(request.content.decode("utf-8"))
1347+
hosted_mcp_tool = body["tools"][0]
13511348

13521349
assert hosted_mcp_tool["headers"][
13531350
"sentry-trace"
@@ -2818,7 +2815,6 @@ async def test_streaming_ttft_on_chat_span(sentry_init, test_agent, async_iterat
28182815
should NOT trigger TTFT.
28192816
"""
28202817
client = AsyncOpenAI(api_key="z")
2821-
client.responses._post = AsyncMock(return_value=EXAMPLE_RESPONSE)
28222818

28232819
model = OpenAIResponsesModel(model="gpt-4", openai_client=client)
28242820

@@ -2831,8 +2827,14 @@ async def test_streaming_ttft_on_chat_span(sentry_init, test_agent, async_iterat
28312827
traces_sample_rate=1.0,
28322828
)
28332829

2830+
request = httpx.Request(
2831+
"POST",
2832+
"/responses",
2833+
)
2834+
28342835
response = httpx.Response(
28352836
200,
2837+
request=request,
28362838
content=async_iterator(
28372839
sse_chunks(
28382840
[
@@ -2912,22 +2914,11 @@ async def test_streaming_ttft_on_chat_span(sentry_init, test_agent, async_iterat
29122914
),
29132915
)
29142916

2915-
# Emulate https://github.com/openai/openai-python/blob/656e3cab4a18262a49b961d41293367e45ee71b9/src/openai/_base_client.py#L1751
2916-
api_response = AsyncAPIResponse(
2917-
raw=response,
2918-
cast_to=Response,
2919-
client=client,
2920-
stream=True,
2921-
stream_cls=AsyncStream[ResponseStreamEvent],
2922-
options=FinalRequestOptions.construct(method="post", url="/responses"),
2923-
retries_taken=0,
2924-
)
2925-
2917+
# Patching https://github.com/openai/openai-python/blob/656e3cab4a18262a49b961d41293367e45ee71b9/src/openai/_base_client.py#L1604
29262918
with patch.object(
2927-
model._client.responses,
2928-
"create",
2929-
# based on https://github.com/openai/openai-python/blob/656e3cab4a18262a49b961d41293367e45ee71b9/src/openai/_base_client.py#L1763
2930-
return_value=await api_response.parse(),
2919+
agent_with_tool.model._client._client,
2920+
"send",
2921+
return_value=response,
29312922
) as _:
29322923
with sentry_sdk.start_transaction(
29332924
name="test_ttft", sampled=True

0 commit comments

Comments
 (0)