sigoden/aichat · error

Invalid response data

Error message

Invalid response data: {text} (status: {status})

What it means

The SSE stream returned a non-2xx status, but the response body could not be parsed as JSON (empty body, HTML error page, or plain text from a gateway), so structured error extraction via catch_error is impossible. The raw text and HTTP status are dumped verbatim; this is the unparseable-error fallback of sse_stream.

Solutions

  1. Use the HTTP status to decide: 401/403 re-authenticate, 429 retry with backoff honoring Retry-After, 5xx retry with backoff
  2. Verify the API base URL — HTML bodies usually indicate a wrong endpoint or reverse-proxy error page
  3. Log the raw body for diagnosis and present only the status code to the user
  4. If the provider returns non-JSON errors consistently, add a text-sniffing branch for known error signatures
Defensive patterns

Strategy: retry

When it happens

Trigger: Thrown at src/client/stream.rs:118 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at src/client/stream.rs:118

            Ok(Event::Open) => {}
            Ok(Event::Message(message)) => {
                let message = SseMmessage {
                    event: message.event,
                    data: message.data,
                };
                if handle(message)? {
                    break;
                }
            }
            Err(err) => {
                match err {
                    EventSourceError::StreamEnded => {}
                    EventSourceError::InvalidStatusCode(status, res) => {
                        let text = res.text().await?;
                        let data: Value = match text.parse() {
                            Ok(data) => data,
                            Err(_) => {
                                bail!(
                                    "Invalid response data: {text} (status: {})",
                                    status.as_u16()
                                );
                            }
                        };
                        catch_error(&data, status.as_u16())?;
                    }
                    EventSourceError::InvalidContentType(header_value, res) => {
                        let text = res.text().await?;
                        bail!(
                            "Invalid response event-stream. content-type: {}, data: {text}",
                            header_value.to_str().unwrap_or_default()
                        );
                    }
                    _ => {
                        bail!("{}", err);
                    }
                }

View on GitHub (pinned to 82976d349a)