BigPizzaV3/CodexPlusPlus · error
chat response choices is empty
Error message
chat response choices is empty
What it means
After confirming choices is an array, the chat-completion-to-response converter requires at least one choice. An empty choices array yields this error. OpenAI semantics guarantee non-empty choices for successful completions, so an empty array means the upstream returned an unusable body.
Solutions
- Inspect and log the full upstream body; if it includes an error/filter reason, surface that to the user instead of the generic error.
- Adjust sampling/request parameters (n, max_tokens, safety settings) that may cause zero choices.
- Check relay stream aggregation — an empty choices array often comes from chunk-merging bugs.
- Retry the request; if transient, upstream hiccups usually resolve.
Example fix
// before
let choice = choices.first().ok_or_else(|| anyhow::anyhow!("chat response choices is empty"))?;
// after: fall back to a graceful empty response
let choice = match choices.first() {
Some(c) => c,
None => {
log::warn!("upstream returned zero choices: {}", body);
return Ok(empty_response_value(body));
}
}; Defensive patterns
Strategy: validation
Validate before calling
let choices = body.get("choices").and_then(Value::as_array);
if choices.map_or(true, |c| c.is_empty()) {
anyhow::bail!("upstream returned zero choices; check filters/sampling params");
} Type guard
fn has_choices(v: &serde_json::Value) -> bool {
v.get("choices").and_then(Value::as_array).map_or(false, |a| !a.is_empty())
} Try / catch
match chat_completion_to_response(&body) {
Ok(v) => v,
Err(e) if e.to_string().contains("choices is empty") => {
log::warn!("zero choices from upstream, retrying once");
retry_request().await
}
Err(e) => return Err(e),
} Prevention
- Avoid request parameters that can yield zero choices (n=0, extreme sampling limits)
- Check provider safety-filter responses and surface their reasons
- Verify relay stream-chunk aggregation does not produce empty choice lists
When it happens
Trigger: Upstream (or relay) returned a 200 chat completion JSON with "choices": [] — e.g. content filter producing zero choices, an aborted generation, or a provider quirk streaming an empty final frame converted as non-streaming.
Common situations: Provider safety filters suppressed all choices; relay aggregated stream chunks incorrectly into an empty list; provider beta behavior change; request with n=0 or malformed sampling params.
Related errors
- chat response choice missing message
- chat response missing choices
- API 未返回有效的 choices[0].message.content,请确认兼容 Chat Completions
- 当前中转未启用 Chat Completions 协议代理
- Chat Completions 协议暂不支持 Responses compact 请求
AI-assisted analysis of BigPizzaV3/CodexPlusPlus@b1ed92e5e4 (2026-09-19).
Data as JSON: /api/errors/ab4d95aba09870fb.
Report an issue: GitHub.
Appendix: source
Thrown at crates/codex-plus-core/src/protocol_proxy.rs:272
body: Value,
original_request: &Value,
) -> anyhow::Result<Value> {
let context = build_codex_tool_context(original_request.get("tools"));
chat_completion_to_response_with_context(body, &context, Some(original_request))
}
fn chat_completion_to_response_with_context(
body: Value,
tool_context: &CodexToolContext,
original_request: Option<&Value>,
) -> anyhow::Result<Value> {
let choices = body
.get("choices")
.and_then(Value::as_array)
.ok_or_else(|| anyhow::anyhow!("chat response missing choices"))?;
let choice = choices
.first()
.ok_or_else(|| anyhow::anyhow!("chat response choices is empty"))?;
let message = choice
.get("message")
.ok_or_else(|| anyhow::anyhow!("chat response choice missing message"))?;
let response_id = response_id_from_chat_id(body.get("id").and_then(Value::as_str));
let mut output = Vec::new();
if let Some(reasoning) = chat_reasoning_to_response_output_item(message, &response_id) {
output.push(reasoning);
}
if let Some(message) = chat_message_to_response_output_item(message, &response_id) {
output.push(message);
}
output.extend(chat_tool_calls_to_response_output_items(
message,
tool_context,
));
let mut response = json!({View on GitHub (pinned to b1ed92e5e4)