{"record":{"id":"ab4d95aba09870fb","repo":"BigPizzaV3/CodexPlusPlus","slug":"chat-response-choices-is-empty","errorCode":null,"errorMessage":"chat response choices is empty","messagePattern":"chat response choices is empty","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codex-plus-core/src/protocol_proxy.rs","lineNumber":272,"sourceCode":"    body: Value,\n    original_request: &Value,\n) -> anyhow::Result<Value> {\n    let context = build_codex_tool_context(original_request.get(\"tools\"));\n    chat_completion_to_response_with_context(body, &context, Some(original_request))\n}\n\nfn chat_completion_to_response_with_context(\n    body: Value,\n    tool_context: &CodexToolContext,\n    original_request: Option<&Value>,\n) -> anyhow::Result<Value> {\n    let choices = body\n        .get(\"choices\")\n        .and_then(Value::as_array)\n        .ok_or_else(|| anyhow::anyhow!(\"chat response missing choices\"))?;\n    let choice = choices\n        .first()\n        .ok_or_else(|| anyhow::anyhow!(\"chat response choices is empty\"))?;\n    let message = choice\n        .get(\"message\")\n        .ok_or_else(|| anyhow::anyhow!(\"chat response choice missing message\"))?;\n\n    let response_id = response_id_from_chat_id(body.get(\"id\").and_then(Value::as_str));\n    let mut output = Vec::new();\n    if let Some(reasoning) = chat_reasoning_to_response_output_item(message, &response_id) {\n        output.push(reasoning);\n    }\n    if let Some(message) = chat_message_to_response_output_item(message, &response_id) {\n        output.push(message);\n    }\n    output.extend(chat_tool_calls_to_response_output_items(\n        message,\n        tool_context,\n    ));\n\n    let mut response = json!({","sourceCodeStart":254,"sourceCodeEnd":290,"githubUrl":"https://github.com/BigPizzaV3/CodexPlusPlus/blob/b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6/crates/codex-plus-core/src/protocol_proxy.rs#L254-L290","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nlet choice = choices.first().ok_or_else(|| anyhow::anyhow!(\"chat response choices is empty\"))?;\n// after: fall back to a graceful empty response\nlet choice = match choices.first() {\n    Some(c) => c,\n    None => {\n        log::warn!(\"upstream returned zero choices: {}\", body);\n        return Ok(empty_response_value(body));\n    }\n};","handlingStrategy":"validation","validationCode":"let choices = body.get(\"choices\").and_then(Value::as_array);\nif choices.map_or(true, |c| c.is_empty()) {\n    anyhow::bail!(\"upstream returned zero choices; check filters/sampling params\");\n}","typeGuard":"fn has_choices(v: &serde_json::Value) -> bool {\n    v.get(\"choices\").and_then(Value::as_array).map_or(false, |a| !a.is_empty())\n}","tryCatchPattern":"match chat_completion_to_response(&body) {\n    Ok(v) => v,\n    Err(e) if e.to_string().contains(\"choices is empty\") => {\n        log::warn!(\"zero choices from upstream, retrying once\");\n        retry_request().await\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["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"],"tags":["protocol-proxy","chat-completions","empty-response"],"backgroundTag":"unexpected-response-shape","analyzedSha":"b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6","analyzedAt":"2026-09-19T23:35:21.129Z","contentChangedAt":"2026-09-19T23:35:21.129Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}