nikivdev/code · error

OpenAI returned empty commit message

Error message

OpenAI returned empty commit message

What it means

Thrown when the OpenAI API returns a successful response but `choices[0].message.content` is empty or absent after trimming, so no commit message can be extracted.

Source

Thrown at src/commit.rs:12964

                    if status.is_client_error() {
                        bail!("OpenAI API error {}: {}", status, text);
                    }
                    last_error = Some(format!("OpenAI API error {}: {}", status, text));
                    continue;
                }

                let parsed: ChatResponse =
                    resp.json().context("failed to parse OpenAI response")?;

                let message = parsed
                    .choices
                    .first()
                    .and_then(|c| c.message.as_ref())
                    .map(|m| m.content.trim().to_string())
                    .unwrap_or_default();

                if message.is_empty() {
                    bail!("OpenAI returned empty commit message");
                }

                return Ok(trim_quotes(&message));
            }
            Err(e) => {
                last_error = Some(format!("failed to call OpenAI API: {}", e));
                if attempt < MAX_RETRIES - 1 {
                    println!("API call failed, will retry...");
                }
            }
        }
    }

    bail!(
        "{}",
        last_error.unwrap_or_else(|| "OpenAI API failed after retries".to_string())
    )
}

View on GitHub (pinned to a747e741ae)

Solutions

  1. Increase max_tokens in the request payload.
  2. Log the raw response JSON and check the `finish_reason` field (e.g. content_filter).
  3. Reduce the diff size sent to the model.
  4. Switch to a different model if content filtering repeatedly triggers.
Defensive patterns

Strategy: fallback

Validate before calling

// Reduce oversized diffs before sending to avoid empty/filtered output
if diff.len() > 50_000 {
    return Err(anyhow!("diff too large for OpenAI request"));
}

Type guard

fn openai_content_present(resp: &ChatResponse) -> bool {
    resp.choices.first()
        .and_then(|c| c.message.as_ref())
        .map(|m| !m.content.trim().is_empty())
        .unwrap_or(false)
}

Try / catch

match result {
    Err(e) if e.to_string().contains("OpenAI returned empty") => {
        eprintln!("Empty OpenAI content (check finish_reason); using fallback");
        fallback_to_manual_message()
    }
    other => other,
}

Prevention

When it happens

Trigger: OpenAI returns 200 with an empty `choices[0].message.content` — content filter triggered (`finish_reason: content_filter`), max_tokens too small, or the model returned no content in the message field.

Common situations: Content filter on the diff content; very small max_tokens setting; model returning empty on huge diffs; response schema drift so `message` deserializes to None.

Related errors


AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01). Data as JSON: /api/errors/15f21be4166380d0. Report an issue: GitHub.