|
15 | 15 | from sentry_sdk.integrations.openai_agents.utils import _set_input_data, safe_serialize |
16 | 16 | from sentry_sdk.utils import parse_version, package_version |
17 | 17 |
|
18 | | -from openai import AsyncOpenAI |
| 18 | +from openai import AsyncOpenAI, InternalServerError |
19 | 19 | from agents.models.openai_responses import OpenAIResponsesModel |
20 | 20 |
|
21 | 21 | from unittest import mock |
@@ -1706,35 +1706,46 @@ async def test_error_captures_input_data(sentry_init, capture_events, test_agent |
1706 | 1706 | Test that input data is captured even when the API call raises an exception. |
1707 | 1707 | This verifies that _set_input_data is called before the API call. |
1708 | 1708 | """ |
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) |
1714 | 1712 |
|
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 | + ) |
1723 | 1717 |
|
1724 | | - events = capture_events() |
| 1718 | + response = httpx.Response( |
| 1719 | + 500, |
| 1720 | + request=model_request, |
| 1721 | + ) |
1725 | 1722 |
|
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) |
1730 | 1741 |
|
1731 | 1742 | ( |
1732 | 1743 | error_event, |
1733 | 1744 | transaction, |
1734 | 1745 | ) = events |
1735 | 1746 |
|
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" |
1738 | 1749 |
|
1739 | 1750 | spans = transaction["spans"] |
1740 | 1751 | ai_client_span = [s for s in spans if s["op"] == "gen_ai.chat"][0] |
|
0 commit comments