Skip to content

Personal AI, On Personal Devices

Python

fix(cloud): surface provider errors without choices (#974)

Haotian Zhengf714ce03c4b4
diff --git a/src/openjarvis/engine/cloud.py b/src/openjarvis/engine/cloud.py
index 89e9102d..85a55387 100644
--- a/src/openjarvis/engine/cloud.py
+++ b/src/openjarvis/engine/cloud.py
@@ -224,6 +224,26 @@ def estimate_cost(model: str, prompt_tokens: int, completion_tokens: int) -> flo
return input_cost + output_cost
+def _first_choice_or_raise(resp: Any, *, provider: str, model: str) -> Any:
+ """Return the first completion choice or surface the provider error."""
+ choices = getattr(resp, "choices", None)
+ if choices:
+ return choices[0]
+
+ error = getattr(resp, "error", None)
+ if isinstance(error, dict):
+ detail = str(error.get("message") or error)
+ elif error is not None:
+ detail = str(getattr(error, "message", None) or error)
+ else:
+ detail = ""
+
+ message = f"{provider} returned no choices for model {model!r}"
+ raise EngineConnectionError(
+ message + (f": {detail}" if detail else " and no error message")
+ )
+
+
def _serialize_anthropic_block(block: Any) -> Dict[str, Any]:
"""Turn an Anthropic content block into a JSON-safe dict for tracing.
@@ -625,7 +645,7 @@ class CloudEngine(InferenceEngine):
else:
raise
elapsed = time.monotonic() - t0
- choice = resp.choices[0]
+ choice = _first_choice_or_raise(resp, provider="OpenAI", model=model)
usage = resp.usage
prompt_tokens = usage.prompt_tokens if usage else 0
completion_tokens = usage.completion_tokens if usage else 0
@@ -968,7 +988,7 @@ class CloudEngine(InferenceEngine):
t0 = time.monotonic()
resp = self._openrouter_client.chat.completions.create(**create_kwargs)
elapsed = time.monotonic() - t0
- choice = resp.choices[0]
+ choice = _first_choice_or_raise(resp, provider="OpenRouter", model=actual_model)
usage = resp.usage
prompt_tokens = usage.prompt_tokens if usage else 0
completion_tokens = usage.completion_tokens if usage else 0
@@ -1023,7 +1043,7 @@ class CloudEngine(InferenceEngine):
t0 = time.monotonic()
resp = self._minimax_client.chat.completions.create(**create_kwargs)
elapsed = time.monotonic() - t0
- choice = resp.choices[0]
+ choice = _first_choice_or_raise(resp, provider="MiniMax", model=model)
usage = resp.usage
prompt_tokens = usage.prompt_tokens if usage else 0
completion_tokens = usage.completion_tokens if usage else 0
@@ -1073,7 +1093,7 @@ class CloudEngine(InferenceEngine):
t0 = time.monotonic()
resp = self._deepseek_client.chat.completions.create(**create_kwargs)
elapsed = time.monotonic() - t0
- choice = resp.choices[0]
+ choice = _first_choice_or_raise(resp, provider="DeepSeek", model=model)
usage = resp.usage
prompt_tokens = usage.prompt_tokens if usage else 0
completion_tokens = usage.completion_tokens if usage else 0
diff --git a/tests/engine/test_cloud.py b/tests/engine/test_cloud.py
index 6077e558..01fb280a 100644
--- a/tests/engine/test_cloud.py
+++ b/tests/engine/test_cloud.py
@@ -118,6 +118,58 @@ class TestCloudEngineGenerate:
assert result["usage"]["completion_tokens"] == 8
+class TestCloudEngineEmptyChoices:
+ @pytest.mark.parametrize(
+ ("model", "client_attr", "provider"),
+ [
+ ("gpt-4o", "_openai_client", "OpenAI"),
+ ("openrouter/test-model", "_openrouter_client", "OpenRouter"),
+ ("MiniMax-M3", "_minimax_client", "MiniMax"),
+ ("deepseek-v4-flash", "_deepseek_client", "DeepSeek"),
+ ],
+ )
+ def test_empty_choices_surfaces_provider_error(
+ self,
+ monkeypatch: pytest.MonkeyPatch,
+ model: str,
+ client_attr: str,
+ provider: str,
+ ) -> None:
+ for env_var in (
+ "OPENAI_API_KEY",
+ "ANTHROPIC_API_KEY",
+ "GOOGLE_API_KEY",
+ "OPENROUTER_API_KEY",
+ "MINIMAX_API_KEY",
+ "DEEPSEEK_API_KEY",
+ "OPENAI_CODEX_API_KEY",
+ "OPENAI_CODEX_BASE_URL",
+ ):
+ monkeypatch.delenv(env_var, raising=False)
+
+ fake_resp = SimpleNamespace(
+ choices=None,
+ error={"message": f"{provider} request failed"},
+ usage=None,
+ model=model,
+ )
+ fake_client = mock.MagicMock()
+ fake_client.chat.completions.create.return_value = fake_resp
+
+ EngineRegistry.register_value("cloud", CloudEngine)
+ engine = CloudEngine()
+ setattr(engine, client_attr, fake_client)
+
+ with pytest.raises(EngineConnectionError) as exc_info:
+ engine.generate(
+ [Message(role=Role.USER, content="Hi")],
+ model=model,
+ )
+
+ assert provider in str(exc_info.value)
+ assert f"{provider} request failed" in str(exc_info.value)
+
+
class TestOpenAIUnsupportedTemperatureRetry:
"""Regression for #426.