{"record":{"id":"954b3449fc8eb526","repo":"BigPizzaV3/CodexPlusPlus","slug":"chat-response-choice-missing-message","errorCode":null,"errorMessage":"chat response choice missing message","messagePattern":"chat response choice missing message","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codex-plus-core/src/protocol_proxy.rs","lineNumber":275,"sourceCode":"    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!({\n        \"id\": response_id,\n        \"object\": \"response\",\n        \"created_at\": body.get(\"created\").and_then(Value::as_u64).unwrap_or(0),","sourceCodeStart":257,"sourceCodeEnd":293,"githubUrl":"https://github.com/BigPizzaV3/CodexPlusPlus/blob/b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6/crates/codex-plus-core/src/protocol_proxy.rs#L257-L293","documentation":"The converter expects each choice to contain a message object carrying the assistant reply. If the selected choice lacks `message`, this error is thrown. This usually indicates a non-standard or truncated chat-completion body (e.g. a delta-style chunk rather than a complete message).","triggerScenarios":"choices[0] exists but has no `message` key — e.g. a streaming delta chunk {\"delta\": {...}} was fed into the non-streaming converter, or the provider emitted function_call/tool_calls without a message wrapper.","commonSituations":"Mixing streaming and non-streaming endpoints in the relay; provider API version change removing/renaming message; intermediary proxy stripping fields; passing tool-call chunks through chat conversion.","solutions":["Confirm the body is a complete (non-streaming) chat completion; route delta chunks to the streaming converter instead.","Log the offending choice to see which fields are actually present (delta? function_call?).","Pin/verify the provider API version so the response schema matches what the converter expects.","Handle tool-call-only responses in the converter before requiring `message`."],"exampleFix":"// before\nlet message = choice.get(\"message\").ok_or_else(|| anyhow::anyhow!(\"chat response choice missing message\"))?;\n// after: accept delta chunks too\nlet message = choice.get(\"message\")\n    .or_else(|| choice.get(\"delta\"))\n    .ok_or_else(|| anyhow::anyhow!(\"choice has neither message nor delta: {}\", choice))?;","handlingStrategy":"type-guard","validationCode":"let ok = body[\"choices\"][0].get(\"message\").is_some()\n    || body[\"choices\"][0].get(\"delta\").is_some();\nif !ok {\n    anyhow::bail!(\"choice lacks message/delta; is this a complete completion?\");\n}","typeGuard":"fn has_assistant_message(choice: &serde_json::Value) -> bool {\n    choice.get(\"message\").map_or(false, |m| m.is_object())\n}","tryCatchPattern":"match chat_completion_to_response(&body) {\n    Ok(v) => v,\n    Err(e) if e.to_string().contains(\"missing message\") => {\n        log::error!(\"non-standard choice shape: {}\", body[\"choices\"][0]);\n        Err(e)\n    }\n    Err(e) => Err(e),\n}","preventionTips":["Never feed streaming delta chunks into the non-streaming converter","Pin provider API versions and re-test converters after upgrades","Handle tool-call-only choices explicitly before requiring `message`"],"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"}