Skip to content

Add ProviderError exception and handle missing LLM response #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/agents/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ def __init__(self, guardrail_result: "OutputGuardrailResult"):
super().__init__(
f"Guardrail {guardrail_result.guardrail.__class__.__name__} triggered tripwire"
)


class ProviderError(AgentsException):
"""Exception raised when the provider fails."""
6 changes: 5 additions & 1 deletion src/agents/models/openai_chatcompletions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from .. import _debug
from ..agent_output import AgentOutputSchemaBase
from ..exceptions import ProviderError
from ..handoffs import Handoff
from ..items import ModelResponse, TResponseInputItem, TResponseStreamEvent
from ..logger import logger
Expand Down Expand Up @@ -70,6 +71,9 @@ async def get_response(
stream=False,
)

if not getattr(response, "choices", None):
raise ProviderError(f"LLM provider error: {getattr(response, 'error', 'unknown')}")

if _debug.DONT_LOG_MODEL_DATA:
logger.debug("Received model response")
else:
Expand Down Expand Up @@ -252,7 +256,7 @@ async def _fetch_response(
stream_options=self._non_null_or_not_given(stream_options),
store=self._non_null_or_not_given(store),
reasoning_effort=self._non_null_or_not_given(reasoning_effort),
extra_headers={ **HEADERS, **(model_settings.extra_headers or {}) },
extra_headers={**HEADERS, **(model_settings.extra_headers or {})},
extra_query=model_settings.extra_query,
extra_body=model_settings.extra_body,
metadata=self._non_null_or_not_given(model_settings.metadata),
Expand Down
40 changes: 40 additions & 0 deletions tests/test_openai_chatcompletions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import importlib
from collections.abc import AsyncIterator
from typing import Any

Expand Down Expand Up @@ -30,6 +31,7 @@
OpenAIProvider,
generation_span,
)
from agents.exceptions import ProviderError
from agents.models.chatcmpl_helpers import ChatCmplHelpers
from agents.models.fake_id import FAKE_RESPONSES_ID

Expand Down Expand Up @@ -330,3 +332,41 @@ def test_store_param():
assert ChatCmplHelpers.get_store_param(client, model_settings) is True, (
"Should respect explicitly set store=True"
)


@pytest.mark.asyncio
async def test_get_response_raises_provider_error_if_no_choices(monkeypatch):
# Import the class under test _inside_ the function so
# pytest’s conftest autouse fixtures don’t stomp it out.
import agents.models.openai_chatcompletions as chatmod

chatmod = importlib.reload(chatmod)

ModelClass = chatmod.OpenAIChatCompletionsModel

dummy_client = AsyncOpenAI(api_key="fake", base_url="http://localhost")
model = ModelClass(model="test-model", openai_client=dummy_client)

class FakeResponse:
choices = []
error = "service unavailable"

async def fake_fetch_response(*args, **kwargs):
return FakeResponse()

monkeypatch.setattr(ModelClass, "_fetch_response", fake_fetch_response)

settings = ModelSettings(temperature=0.0, max_tokens=1)
with pytest.raises(ProviderError) as exc:
await model.get_response(
system_instructions="",
input="Hello?",
model_settings=settings,
tools=[],
output_schema=None,
handoffs=[],
tracing=ModelTracing.DISABLED,
previous_response_id=None,
)

assert "service unavailable" in str(exc.value)