nikivdev/code · error

AI returned an empty response.

Error message

AI returned an empty response.

What it means

parse_ask_response is the single funnel through which the AI's natural-language answer is interpreted. Before any parsing it trims backticks/whitespace, and if nothing remains it bails — the model produced no usable content, so there is no selection to return.

Source

Thrown at src/ask.rs:428

                agent_id
            ));
        }
    }

    prompt.push_str(&format!("\nUser query: {}\n", query));
    prompt.push_str("Answer:");

    prompt
}

fn parse_ask_response(
    response: &str,
    tasks: &[DiscoveredTask],
    valid_subcommands: &HashSet<String>,
) -> Result<AskSelection> {
    let cleaned = response.trim().trim_matches('`').trim();
    if cleaned.is_empty() {
        bail!("AI returned an empty response.");
    }

    if let Some(selection) = parse_structured_line(cleaned, tasks, valid_subcommands)? {
        return Ok(selection);
    }

    // Some models emit reasoning wrappers (e.g. <think>...</think>) before the
    // final machine-readable answer. Scan lines and parse the first valid one.
    for line in cleaned.lines() {
        let candidate = line.trim();
        if candidate.is_empty() {
            continue;
        }
        if let Some(selection) = parse_structured_line(candidate, tasks, valid_subcommands)? {
            return Ok(selection);
        }
    }

View on GitHub (pinned to a747e741ae)

Solutions

  1. Retry the AI request (empty responses are usually transient)
  2. Check the raw response body/logs before parsing to confirm what the provider actually returned
  3. Strengthen the prompt to force a non-empty structured answer ('task: <name>' or 'cmd: <command>')
  4. If retrying fails, surface a 'try again' path to the user rather than crashing

Example fix

// before
let cleaned = response.trim().trim_matches('`').trim();
if cleaned.is_empty() {
    bail!("AI returned an empty response.");
}
// after
let cleaned = response.trim().trim_matches('`').trim();
if cleaned.is_empty() {
    eprintln!("AI returned an empty response, retrying...");
    return retry_ask(...);
}
Defensive patterns

Strategy: retry

Validate before calling

fn ai_response_usable(response: &str) -> bool {
    !response.trim().trim_matches('`').trim().is_empty()
}

Try / catch

match parse_ask_response(&response, &tasks, &subcommands) {
    Err(e) if e.to_string().contains("empty response") => {
        // retry up to N times with backoff
    }
    other => other,
}

Prevention

When it happens

Trigger: The AI HTTP call succeeds but returns an empty string or only whitespace/backticks; a response containing just '```' or '``'; a proxy or provider returning an empty body with 200 OK.

Common situations: Model overloaded or refusing and yielding an empty completion; prompt too restrictive; misconfigured API gateway stripping the body.

Related errors


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