Hmbown/CodeWhale · error

Responses API error (HTTP )

Error message

Responses API error (HTTP {status}): {raw}

What it means

When opening a Responses API stream, the client first checks the HTTP status before consuming the byte stream. A non-success status (4xx/5xx) means the request failed at the API layer; the client reads a bounded amount of the error body and bails with the status code and raw body so the caller sees the upstream failure detail.

Solutions

  1. Read the {raw} body in the message — it contains the provider's error JSON explaining the cause
  2. For 401/403, fix the API key/credentials and account permissions
  3. For 429, back off and retry respecting rate limits
  4. For 400, correct the request parameters/model name; for 5xx, retry later or check provider status

Example fix

// before: single attempt
let response = self.send(request).await?;
// after: retry transient upstream failures with backoff
let response = retry_with_backoff(3, || async {
    let resp = self.send(request.clone()).await?;
    if resp.status().is_server_error() { bail!("transient {status}") }
    Ok(resp)
}).await?;
Defensive patterns

Strategy: retry

Validate before calling

// preflight: validate credentials and endpoint reachability
assert!(!api_key.is_empty(), "missing API key for Responses endpoint");

Try / catch

match stream_request().await {
    Err(e) if e.to_string().contains("Responses API error") => {
        if e.to_string().contains("HTTP 429") || e.to_string().contains("HTTP 5") {
            backoff_retry(|| stream_request()).await
        } else {
            Err(e) // 4xx client errors are not retryable
        }
    }
    other => other,
}

Prevention

When it happens

Trigger: handle_responses_stream (via create_message_stream or handle_responses_message) receives an HTTP response whose status.is_success() is false — e.g. 401 invalid key, 429 rate limit, 400 bad request, 5xx upstream outage — when calling the provider's Responses endpoint.

Common situations: Expired or wrong API key (401/403); malformed request payload or unsupported parameters (400); quota exhaustion (429); provider outage (5xx); hitting a wrong base URL path.

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 Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/cef81aab07305fbd. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/client/responses.rs:222

                            .header("OpenAI-Beta", "responses=experimental")
                            .header("originator", "codex_cli_rs");
                        if let Some(account_id) = &account_id {
                            builder = builder.header("chatgpt-account-id", account_id);
                        }
                    }
                    builder.body(request_body.clone())
                })
                .await
                .context("Responses API request failed")
            }
        })
        .await?;

        let status = response.status();
        crate::client::record_provider_response(self.api_provider, status.as_u16());
        if !status.is_success() {
            let raw = bounded_error_text(response, ERROR_BODY_MAX_BYTES).await;
            anyhow::bail!("Responses API error (HTTP {status}): {raw}");
        }

        let stream_idle_timeout = self.stream_idle_timeout;
        let byte_stream = response.bytes_stream();

        let stream = async_stream::stream! {
            use futures_util::StreamExt;

            // Emit synthetic MessageStart.
            yield Ok(StreamEvent::MessageStart {
                message: MessageResponse {
                    id: String::new(),
                    r#type: "message".to_string(),
                    role: "assistant".to_string(),
                    content: vec![],
                    model: wire_model.clone(),
                    stop_reason: None,
                    stop_sequence: None,

View on GitHub (pinned to 73e0f67d83)