sigoden/aichat · error

Invalid response data

Error message

Invalid response data: {data}

What it means

In claude_extract_chat_completions, a successful Anthropic Messages response parsed to neither text content nor tool calls (reasoning blocks alone don't count). The library considers this an unusable empty completion and bails with the raw response JSON embedded in the message.

Solutions

  1. Inspect {data} for the actual content array and stop_reason
  2. Increase max_tokens — thinking tokens consume the budget and can leave zero text tokens
  3. Check stop_reason (e.g. refusal, max_tokens) in the payload
  4. If using extended-thinking models, ensure the library version extracts text blocks correctly / update the library
  5. Capture thinking blocks separately if reasoning-only output is expected

Example fix

// before
.with_max_tokens(512)  // consumed entirely by thinking -> empty text
// after
.with_max_tokens(4096)  // headroom for thinking + text
Defensive patterns

Strategy: try-catch

Validate before calling

// Give thinking models headroom
if max_tokens <= thinking_budget { return Err("max_tokens must exceed thinking budget".into()); }

Try / catch

match client.chat_completions(req).await {
    Err(e) if e.to_string().starts_with("Invalid response data") => {
        // inspect {data} for content blocks / stop_reason
        Err(anyhow!("claude empty completion: {e}"))
    }
    r => r,
}

Prevention

When it happens

Trigger: claude_chat_completions gets a 2xx response where the content array contains no text/tool_use blocks — e.g. only thinking blocks, an empty content array, or refusal/stop_reason-only responses the extractor doesn't convert.

Common situations: max_tokens exhausted by thinking tokens (extended thinking models) leaving no text output, content filtered by safety, or Anthropic response schema changes.

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/40510f7672e6c302. Report an issue: GitHub.

Appendix: source

Thrown at src/client/claude.rs:342

                        item["id"].as_str(),
                    ) {
                        tool_calls.push(ToolCall::new(
                            name.to_string(),
                            input.clone(),
                            Some(id.to_string()),
                        ));
                    }
                }
                _ => {}
            }
        }
    }
    if let Some(reasoning) = reasoning {
        text = format!("<think>\n{reasoning}\n</think>\n\n{text}")
    }

    if text.is_empty() && tool_calls.is_empty() {
        bail!("Invalid response data: {data}");
    }

    let output = ChatCompletionsOutput {
        text: text.to_string(),
        tool_calls,
        id: data["id"].as_str().map(|v| v.to_string()),
        input_tokens: data["usage"]["input_tokens"].as_u64(),
        output_tokens: data["usage"]["output_tokens"].as_u64(),
    };
    Ok(output)
}

View on GitHub (pinned to 82976d349a)