sigoden/aichat · error

Invalid response data

Error message

Invalid response data: {data}

What it means

In the Cohere client's extract_chat_completions, the parsed response produced no text and no tool calls, so the library bails with 'Invalid response data' embedding the raw JSON. As with the other providers, an empty completion is treated as an invalid response rather than returned silently.

Solutions

  1. Inspect {data} in the error for the actual Cohere response body and finish reasons
  2. Check is_finished/finish_reason and safety fields in the payload
  3. Increase max_tokens or adjust the prompt to avoid safety filtering
  4. Confirm you are hitting the API version the library expects (Cohere v1 vs v2 schemas differ)
  5. Update the library if Cohere changed its response schema

Example fix

// before
client.chat_completions(req).await?  // v2 endpoint, v1-shaped parser -> empty extract
// after
.with_provider("cohere/chat-v2")  // or upgrade library so schema matches endpoint
Defensive patterns

Strategy: try-catch

Try / catch

match client.chat_completions(req).await {
    Err(e) if e.to_string().starts_with("Invalid response data") => {
        // inspect embedded Cohere JSON: finish_reason, safety flags
        Err(anyhow!("cohere empty completion: {e}"))
    }
    r => r,
}

Prevention

When it happens

Trigger: chat_completions on Cohere receives a 2xx response whose generations/message fields yield empty text and no tool_calls — e.g. a safety-filtered generation, an empty generation, or an unexpected response schema (chat vs chat/v2 shapes).

Common situations: Cohere safety mode stripping output, wrong endpoint/version producing a schema the extractor doesn't understand, max_tokens too small, or model returning only finish reasons.

Understand the failure class

Background: "empty response", "returned no data", "empty embeddings": what HTTP 200-with-empty-body errors mean across libraries — this error's family across 36 libraries.

Related errors


AI-assisted analysis of sigoden/aichat@82976d349a (2026-09-09). Data as JSON: /api/errors/7f8d62defb1ec7f6. Report an issue: GitHub.

Appendix: source

Thrown at src/client/cohere.rs:245

            if let (Some(name), Some(arguments), Some(id)) = (
                call["function"]["name"].as_str(),
                call["function"]["arguments"].as_str(),
                call["id"].as_str(),
            ) {
                let arguments: Value = arguments.parse().with_context(|| {
                    format!("Tool call '{name}' have non-JSON arguments '{arguments}'")
                })?;
                tool_calls.push(ToolCall::new(
                    name.to_string(),
                    arguments,
                    Some(id.to_string()),
                ));
            }
        }
    }

    if text.is_empty() && tool_calls.is_empty() {
        bail!("Invalid response data: {data}");
    }
    let output = ChatCompletionsOutput {
        text,
        tool_calls,
        id: data["id"].as_str().map(|v| v.to_string()),
        input_tokens: data["usage"]["billed_units"]["input_tokens"].as_u64(),
        output_tokens: data["usage"]["billed_units"]["output_tokens"].as_u64(),
    };
    Ok(output)
}

View on GitHub (pinned to 82976d349a)