Skip to content

Commit 1d4adf0

Browse files
test(openai-agents): Replace mocks with httpx in API error test (#5601)
Replace mocks with `httpx` types to avoid test failures when library internals change.
1 parent 6e849ca commit 1d4adf0

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

tests/integrations/openai_agents/test_openai_agents.py

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from sentry_sdk.integrations.openai_agents.utils import _set_input_data, safe_serialize
1616
from sentry_sdk.utils import parse_version, package_version
1717

18-
from openai import AsyncOpenAI
18+
from openai import AsyncOpenAI, InternalServerError
1919
from agents.models.openai_responses import OpenAIResponsesModel
2020

2121
from unittest import mock
@@ -1706,35 +1706,46 @@ async def test_error_captures_input_data(sentry_init, capture_events, test_agent
17061706
Test that input data is captured even when the API call raises an exception.
17071707
This verifies that _set_input_data is called before the API call.
17081708
"""
1709-
with patch.dict(os.environ, {"OPENAI_API_KEY": "test-key"}):
1710-
with patch(
1711-
"agents.models.openai_responses.OpenAIResponsesModel.get_response"
1712-
) as mock_get_response:
1713-
mock_get_response.side_effect = Exception("API Error")
1709+
client = AsyncOpenAI(api_key="test-key")
1710+
model = OpenAIResponsesModel(model="gpt-4", openai_client=client)
1711+
agent = test_agent.clone(model=model)
17141712

1715-
sentry_init(
1716-
integrations=[
1717-
OpenAIAgentsIntegration(),
1718-
LoggingIntegration(event_level=logging.CRITICAL),
1719-
],
1720-
traces_sample_rate=1.0,
1721-
send_default_pii=True,
1722-
)
1713+
model_request = httpx.Request(
1714+
"POST",
1715+
"/responses",
1716+
)
17231717

1724-
events = capture_events()
1718+
response = httpx.Response(
1719+
500,
1720+
request=model_request,
1721+
)
17251722

1726-
with pytest.raises(Exception, match="API Error"):
1727-
await agents.Runner.run(
1728-
test_agent, "Test input", run_config=test_run_config
1729-
)
1723+
with patch.object(
1724+
agent.model._client._client,
1725+
"send",
1726+
return_value=response,
1727+
) as _:
1728+
sentry_init(
1729+
integrations=[
1730+
OpenAIAgentsIntegration(),
1731+
LoggingIntegration(event_level=logging.CRITICAL),
1732+
],
1733+
traces_sample_rate=1.0,
1734+
send_default_pii=True,
1735+
)
1736+
1737+
events = capture_events()
1738+
1739+
with pytest.raises(InternalServerError, match="Error code: 500"):
1740+
await agents.Runner.run(agent, "Test input", run_config=test_run_config)
17301741

17311742
(
17321743
error_event,
17331744
transaction,
17341745
) = events
17351746

1736-
assert error_event["exception"]["values"][0]["type"] == "Exception"
1737-
assert error_event["exception"]["values"][0]["value"] == "API Error"
1747+
assert error_event["exception"]["values"][0]["type"] == "InternalServerError"
1748+
assert error_event["exception"]["values"][0]["value"] == "Error code: 500"
17381749

17391750
spans = transaction["spans"]
17401751
ai_client_span = [s for s in spans if s["op"] == "gen_ai.chat"][0]

0 commit comments

Comments
 (0)