Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ai/openai: retry a Responses failure that reports a rate limit
Once a streamed reply's 200 and headers have gone out, the endpoint can
only report a rate limit inside the stream, as a failed response or an
error event with code rate_limit_exceeded. response_failure treated
every code but server_error as fatal, so a limit that a plain request
would get as a retried 429 ended the review.

Classify rate_limit_exceeded as a rate limit that waits as long as a
429 without a Retry-After does. A plain reply carrying the same failure
is classified the same way; insufficient_quota, which no wait fixes,
stays fatal.

Link: #947
Fixes: 19171a1 ("ai: Add a /v1/responses transport for the openai provider")
Signed-off-by: Shang En Sim <sim@shangen.org>
  • Loading branch information
12458 committed Nov 26, 2026
commit fbaf79ada0d392ceb90278f8156fef06bc5c6fd8
2 changes: 1 addition & 1 deletion designs/DESIGN_STREAMED_RESPONSES.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ existing error classification.
carry the full response object. Intermediate events can be skipped. An
`error` event is converted to a failed response so it goes through the
same classification. `server_error` can retry with the caller's
backoff.
backoff, and `rate_limit_exceeded` uses the default rate-limit delay.

### OpenAI Chat Completions

Expand Down
46 changes: 43 additions & 3 deletions src/ai/openai_responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,13 +663,18 @@ fn response_failure(status: Option<&str>, error: Option<ResponsesError>) -> Opti
);

// server_error is this reply's 5xx, so it retries on the same terms:
// no delay of its own, leaving the pace to the caller's backoff. Any
// other code names something the request did, which another attempt
// repeats.
// no delay of its own, leaving the pace to the caller's backoff.
// rate_limit_exceeded is its 429, which a stream can only report this
// way once its 200 has gone out; it waits as long as a 429 without a
// Retry-After does. Any other code names something the request did,
// which another attempt repeats.
Some(match code.as_str() {
"server_error" => {
OpenAiCompatError::TransientError(Duration::from_secs(0), reported).into()
}
"rate_limit_exceeded" => {
OpenAiCompatError::RateLimitExceeded(Duration::from_secs(60)).into()
}
_ => OpenAiCompatError::ResponseFailed(reported).into(),
})
}
Expand Down Expand Up @@ -1401,6 +1406,15 @@ mod tests {
crate::ai::classify_ai_error(&fatal),
crate::ai::AiErrorClass::Fatal
));

assert!(matches!(
crate::ai::classify_ai_error(&failed("rate_limit_exceeded")),
crate::ai::AiErrorClass::RateLimit { .. }
));
assert!(matches!(
crate::ai::classify_ai_error(&failed("insufficient_quota")),
crate::ai::AiErrorClass::Fatal
));
}

/// The endpoint returns item types this build does not model, and a new
Expand Down Expand Up @@ -1599,6 +1613,32 @@ mod tests {
}
}

/// A stream can only report a rate limit inside its 200, as a failed
/// response or an error event, and either waits like a 429.
#[tokio::test]
async fn a_rate_limit_inside_a_stream_is_a_rate_limit() {
let mut reply: Value = serde_json::from_str(MESSAGE).unwrap();
reply["status"] = json!("failed");
reply["output"] = json!([]);
reply["error"] = json!({"code": "rate_limit_exceeded", "message": "try again in 2s"});
for body in [
stream_of("response.failed", &reply.to_string()),
event_stream(&[json!({"type": "error", "code": "rate_limit_exceeded",
"message": "try again in 2s", "param": null})]),
] {
let err = generate_streamed(body)
.await
.expect_err("a rate limit fails");
assert!(
matches!(
crate::ai::classify_ai_error(&err),
crate::ai::AiErrorClass::RateLimit { .. }
),
"{err:#}"
);
}
}

/// A nested error with a null code is classified by its type, which
/// is where a server failure names itself.
#[tokio::test]
Expand Down