I am streaming responses with background mode ON. Because, sometimes my connection to the OpenAI server would drop with an httpx.ReadError, httpx.ReadTimeout or httpx.RemoteProtocolError exception.
This is working but sometimes I am now getting an openai.BadRequestError with the text that is on the title.
Here’s my code:
state = {
"cursor": None,
"response_id": None,
}
max_retries = 3
async with RealtimeDB() as realtime_db:
for attempt in range(max_retries):
try:
if state["cursor"] is None or state["response_id"] is None:
# First attempt, or failed before getting response_id - start fresh
stream = await aopenai_client.responses.create(
model=model,
input=input,
instructions=instructions,
previous_response_id=previous_response_id,
tools=tools,
reasoning=reasoning,
stream=True,
background=True,
)
async for event in stream:
await _handle_event(event, state, realtime_db, thought_path, realtime_path)
else:
# Resume from cursor
async with aopenai_client.responses.stream(
response_id=state["response_id"],
starting_after=state["cursor"],
) as stream:
async for event in stream:
await _handle_event(event, state, realtime_db, thought_path, realtime_path)
return state
except (httpx.ReadError, httpx.ReadTimeout, httpx.RemoteProtocolError):
if attempt == max_retries - 1:
raise
await asyncio.sleep(2**attempt)
Note the error happens a few times a day out of ~1000 messages a day.
OpenAI version: ‘2.15.0’