BigPizzaV3/CodexPlusPlus · error
chat response missing choices
Error message
chat response missing choices
What it means
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.
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.
Example fix
// before: blind conversion
let value = chat_completion_to_response(body)?;
// after: pre-check shape
if body.get("choices").and_then(Value::as_array).is_none() {
if let Some(msg) = body.get("error").and_then(|e| e.get("message")) {
anyhow::bail!("upstream error: {}", msg);
}
anyhow::bail!("unexpected chat completion body");
}
let value = chat_completion_to_response(body)?; Defensive patterns
Strategy: type-guard
Validate before calling
let body: serde_json::Value = serde_json::from_str(&raw)?;
if body.get("choices").and_then(Value::as_array).is_none() {
anyhow::bail!("upstream did not return a chat completion body: {raw}");
} Type guard
fn is_chat_completion(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("missing choices") => {
log::error!("upstream body: {}", serde_json::to_string(&body).unwrap_or_default());
return Err(e);
}
Err(e) => return Err(e),
} Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- chat response choice missing message
- chat response choices is empty
- 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/25119eec88c6ec2f.
Report an issue: GitHub.
Appendix: source
Thrown at crates/codex-plus-core/src/protocol_proxy.rs:269
}
pub fn chat_completion_to_response_with_request(
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,View on GitHub (pinned to b1ed92e5e4)