Skip to content
Closed
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
8 changes: 8 additions & 0 deletions src/agents/extensions/models/any_llm_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,14 @@ async def _get_response_via_chat(
prompt=prompt,
)

if not response.choices:
provider_error = getattr(response, "error", None)
error_details = f": {provider_error}" if provider_error is not None else ""
raise ModelBehaviorError(
f"ChatCompletion response has no choices (possible provider error payload)"
f"{error_details}"
)

message: ChatCompletionMessage | None = None
first_choice: Choice | None = None
if response.choices:
Expand Down
31 changes: 31 additions & 0 deletions tests/models/test_any_llm_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,37 @@ async def test_any_llm_chat_path_content_filter_keeps_real_content(monkeypatch)
assert response.output[0].content[0].text == "here is the answer"


@pytest.mark.allow_call_model_methods
@pytest.mark.asyncio
async def test_any_llm_chat_path_rejects_empty_choices(monkeypatch) -> None:
provider = FakeAnyLLMProvider(
supports_responses=False,
chat_response=ChatCompletion(
id="chatcmpl_empty",
created=0,
model="fake-model",
object="chat.completion",
choices=[],
),
)
module, _create_calls = _import_any_llm_module(monkeypatch, provider)

model = module.AnyLLMModel(model="openrouter/openai/gpt-5.4-mini")
with pytest.raises(ModelBehaviorError, match="ChatCompletion response has no choices"):
await model.get_response(
system_instructions=None,
input="hi",
model_settings=ModelSettings(),
tools=[],
output_schema=None,
handoffs=[],
tracing=ModelTracing.DISABLED,
previous_response_id=None,
conversation_id=None,
prompt=None,
)


@pytest.mark.allow_call_model_methods
@pytest.mark.asyncio
@pytest.mark.parametrize(
Expand Down