Inconcistency between Responses API and Chat Completions API on rate limit errors
Confirm this is an issue with the Python library and not an underlying OpenAI API
- [x] This is an issue with the Python library
Describe the bug
I noticed that rate limit errors are raised differently in the two APIs during streaming. In chat completions the error is raised and retried by the client itself on the initial call, before reading the stream. On the other hand, the first call to responses.create(...) returns a stream object successfully and then upon reading the stream, an APIError is raised indicating a rate limit. As a result, the whole retrying logic in the client is completely skipped and the user has to implement their own. It is also unclear whether an error could be raised mid-stream which adds to the complexity of the fix.
After inspecting the client's code, I initially thought that this is not a library issue but a misalignment with the model's service. I contacted Microsoft support since they host our models but they insisted I raise an issue here.
To Reproduce
To reproduce this you would need an Azure hosted OpenAI model ( I have personally already tried this with gpt-4o and gpt-5 ). Lower your token rate limit threshold to a low enough value and use the below snippet.
Code snippets
from openai import OpenAI
client = OpenAI(
base_url = "your-endpoint-v1",
api_key="your-api-key"
)
response = client.responses.create(
model="your-model",
store=False,
stream=True,
input='a long enough prompt'*10000 # to hit a rate limit
)
for event in response:
continue # an APIError is raised here
OS
Linux, Windows
Python version
Python >=3.11.9
Library version
openai v1.109.1
Yeah, it seems like we only retry for the initial requests, and not when the response is rate-limiting in the middle of streaming. Could you provide more information about what you receive in the APIError - message, status, etc.?
@karpetrosyan This is the local state after I set a breakpoint at line 193 of _streaming.py.
I don't think it would be simpler for users if we added automatic retries mid-stream due to rate limits. I think this should be the expected behavior.
Does Chat Completions API retry mid-stream? I have never had a rate limit error show up mid-stream using Chat Completions, the error always comes before I even start reading the stream, exactly when I create the stream object and there the client retries it automatically. Meanwhile, for Responses API the stream object is always successfully created and I get a rate limit error right after I start reading the stream ( it has never happened mid-stream yet, always at the very start of the stream). Is this inconsistency in behavior between the two APIs expected? The client never retries a rate limit error when using the Responses API.
They both stream via the same API, so the behavior should be the same. Neither can retry mid-stream because the user has already consumed part of it, and it's unclear what retry would even mean in this context. I don't think there's an inconsistency at the SDK level, though there might be an issue with the OpenAI part, which isn't tracked in this repo.
Audit + proposed safe retry semantics:
The retry gap between non-streaming requests and streamed Responses should not be solved by blindly retrying after arbitrary stream errors. A stream is a partially delivered side-effectful sequence: once the client has yielded event k, replaying the request can duplicate observable output/tool state.
The safe invariant is:
retry_allowed := error_before_first_user_visible_event
and:
events_emitted > 0 => no transparent replay
For Responses streaming, wrap the initial stream acquisition/iteration so transport/API rate-limit failures that occur before the first yielded event can go through the existing _should_retry / retry budget. Once any SSE event has been yielded to the caller, subsequent errors must surface as stream errors rather than silently replaying the request.
This gives parity with ordinary request retries without violating stream prefix semantics:
prefix_before_retry == empty
is the condition that makes replay observationally equivalent. If the prefix is non-empty, replay is not equivalent unless the API exposes a request-level idempotency/replay token with documented streaming semantics.
Regression matrix: (1) rate-limit before first event => retry; (2) rate-limit after first event => no automatic replay; (3) first-event success then midstream error => single surfaced error; (4) sync/async parity; (5) retry budget/max_retries preserved; (6) cancellation never retried.
This is a deliberately stronger contract than simply catching APIError from Stream.__iter__: it makes the retry boundary mathematically explicit and prevents duplicate streamed prefixes or duplicated tool effects.
Refined implementation plan after auditing current main: keep generic BaseClient retry unchanged and add a streaming-only retry boundary around Stream.__stream__ / AsyncStream.__stream__.
State machine:
ACQUIRED -> PRE_FIRST_EVENT -> EMITTING -> TERMINAL
Only errors in PRE_FIRST_EVENT may transparently retry. Once any event has been yielded (EMITTING), the stream error must propagate.
Implementation shape:
Stream/AsyncStreamalready retain_client,_cast_to,_options, andresponse, so a new private helper can recreate the exact original request options with the same idempotency key and incremented retry count.- On
APIErrorproduced while decoding the first SSE event, inspect the underlying response status/headers/body. Only retry if the failure is retryable by the same_should_retrypolicy used byBaseClient(429/5xx/408/409 or explicit x-should-retry), and no item/event has been yielded. - Close the failed response before retrying; invoke the existing retry backoff logic (
_calculate_retry_timeout/_sleep_for_retry) rather than introducing a second policy. - Recreate a fresh streaming request from the saved
FinalRequestOptions, preserving the idempotency key and request parameters. Replace the local iterator/response with the new stream's raw iterator and continue inPRE_FIRST_EVENT. - Once the first event is yielded, transition permanently to
EMITTING; any laterAPIError,RemoteProtocolError, timeout, or cancellation is surfaced and never transparently replayed. - Sync/async parity.
CancelledErrormust never be retried.
Formal safety invariant:
transparent_retry => emitted_event_count == 0.
This avoids replaying a non-empty prefix, which would be observationally different and can duplicate tool-side effects. It also fixes the reported Azure behavior without changing normal stream semantics.
Required tests with mocked SSE/HTTP transport:
- 429 before first event -> exactly one retry and first event delivered once;
- 500 before first event -> retry until budget exhausted;
- 429 after first event -> no retry, one surfaced APIError;
- arbitrary APIError after first event -> no retry;
- cancellation before first event -> propagate cancellation, no retry;
- sync and async parity;
- idempotency key and retry-count headers preserved across the transparent retry.
Thanks for the detailed report! Both APIs use the same SDK retry behavior: HTTP 429 responses are retried, while errors delivered inside an established stream are surfaced to the application, including errors in the first event. We’ll keep stream retries under application control, where callers can decide whether replaying the request is appropriate. Closing this as expected SDK behavior.