{"record":{"id":"25119eec88c6ec2f","repo":"BigPizzaV3/CodexPlusPlus","slug":"chat-response-missing-choices","errorCode":null,"errorMessage":"chat response missing choices","messagePattern":"chat response missing choices","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codex-plus-core/src/protocol_proxy.rs","lineNumber":269,"sourceCode":"}\n\npub fn chat_completion_to_response_with_request(\n    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,","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/BigPizzaV3/CodexPlusPlus/blob/b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6/crates/codex-plus-core/src/protocol_proxy.rs#L251-L287","documentation":"chat_completion_to_response_with_context converts an OpenAI-style chat completion body into the Codex response format. The body must contain a top-level choices array; if choices is missing or is not an array this error is thrown. It guards the protocol translation from malformed upstream payloads.","triggerScenarios":"The proxy receives an HTTP 200 chat-completions-style body whose JSON lacks a `choices` array — typically an error body served with 200, a streaming chunk fed to a non-streaming converter, or a wrong-endpoint response (e.g. /responses payload posted to chat conversion).","commonSituations":"Misconfigured relay upstream returning its own error JSON with 200; model provider changed response schema; client hit /chat/completions but upstream answered a responses-API body; rate-limit/billing error body passed through.","solutions":["Log the raw upstream body at this point to see what actually arrived (often an error object with a `message` field).","Check upstream status/health: the relay may be returning an error body with HTTP 200.","Verify the request went to the chat-completions endpoint, not the responses API, and that streaming mode matches the converter used.","Fix relay/model routing config so the expected provider is called."],"exampleFix":"// before: blind conversion\nlet value = chat_completion_to_response(body)?;\n// after: pre-check shape\nif body.get(\"choices\").and_then(Value::as_array).is_none() {\n    if let Some(msg) = body.get(\"error\").and_then(|e| e.get(\"message\")) {\n        anyhow::bail!(\"upstream error: {}\", msg);\n    }\n    anyhow::bail!(\"unexpected chat completion body\");\n}\nlet value = chat_completion_to_response(body)?;","handlingStrategy":"type-guard","validationCode":"let body: serde_json::Value = serde_json::from_str(&raw)?;\nif body.get(\"choices\").and_then(Value::as_array).is_none() {\n    anyhow::bail!(\"upstream did not return a chat completion body: {raw}\");\n}","typeGuard":"fn is_chat_completion(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(\"missing choices\") => {\n        log::error!(\"upstream body: {}\", serde_json::to_string(&body).unwrap_or_default());\n        return Err(e);\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Log raw upstream bodies when protocol conversion fails","Verify endpoint type (chat completions vs responses API) matches the converter","Check relay upstream health when 200 responses contain error objects"],"tags":["protocol-proxy","chat-completions","response-conversion"],"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"}