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

  1. Inspect and log the full upstream body; if it includes an error/filter reason, surface that to the user instead of the generic error.
  2. Adjust sampling/request parameters (n, max_tokens, safety settings) that may cause zero choices.
  3. Check relay stream aggregation — an empty choices array often comes from chunk-merging bugs.
  4. 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

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


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)