sigoden/aichat · error

Invalid response data

Error message

Invalid response data: {data}

What it means

In the Bedrock client's streaming chat-completions path, a non-success HTTP response is returned by the EventStream request. The library parses the JSON error body, runs catch_error() for known error shapes, and if the body is not a recognized error shape it bails with 'Invalid response data' embedding the raw response data. This means Bedrock returned an HTTP failure whose body did not match any known error schema.

Solutions

  1. Print the {data} in the message — it contains Bedrock's actual error payload explaining the root cause
  2. Verify the model ID is correct and enabled for your account in the target region
  3. Check IAM permissions for bedrock:InvokeModelWithResponseStream
  4. Retry with backoff if the payload indicates throttling/TooManyRequests
  5. Check AWS region configuration matches a region where the model is available

Example fix

// before
let client = Client::default().with_model("claude-3-sonnet");
// after (use full inference profile / correct model id)
let client = Client::default().with_model("anthropic.claude-3-sonnet-20240229-v1:0");
Defensive patterns

Strategy: try-catch

Validate before calling

// Before the call: ensure model id and creds are set
if client.model().is_empty() { return Err("model id required".into()); }

Type guard

fn is_success_status(res: &reqwest::Response) -> bool { res.status().is_success() }

Try / catch

match client.chat_completions_streaming(req).await {
    Err(e) if e.to_string().starts_with("Invalid response data") => {
        // surface the embedded payload; check model access/IAM before retrying
        log::error!("bedrock error: {e}");
    }
    r => r?,
}

Prevention

When it happens

Trigger: chat_completions_streaming on Bedrock receives status.is_success() == false AND the parsed JSON body is not recognized by catch_error() (e.g. throttling/quota errors, model access errors, or HTML/agent error pages with unexpected JSON shapes).

Common situations: Model not enabled in the AWS account/region, missing IAM permissions (bedrock:InvokeModelWithResponseStream), throttling due to rate limits, invalid model ID, or a proxy returning a non-standard error body.

Understand the failure class

Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.

Related errors


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

Appendix: source

Thrown at src/client/bedrock.rs:201

    if !status.is_success() {
        catch_error(&data, status.as_u16())?;
    }

    debug!("non-stream-data: {data}");
    extract_chat_completions(&data)
}

async fn chat_completions_streaming(
    builder: RequestBuilder,
    handler: &mut SseHandler,
) -> Result<()> {
    let res = builder.send().await?;
    let status = res.status();
    if !status.is_success() {
        let data: Value = res.json().await?;
        catch_error(&data, status.as_u16())?;
        bail!("Invalid response data: {data}");
    }

    let mut function_name = String::new();
    let mut function_arguments = String::new();
    let mut function_id = String::new();
    let mut reasoning_state = 0;

    let mut stream = res.bytes_stream();
    let mut buffer = BytesMut::new();
    let mut decoder = MessageFrameDecoder::new();
    while let Some(chunk) = stream.next().await {
        let chunk = chunk?;
        buffer.extend_from_slice(&chunk);
        while let DecodedFrame::Complete(message) = decoder.decode_frame(&mut buffer)? {
            let response_headers = parse_response_headers(&message)?;
            let message_type = response_headers.message_type.as_str();
            let smithy_type = response_headers.smithy_type.as_str();
            match (message_type, smithy_type) {

View on GitHub (pinned to 82976d349a)