I am using the async client in the following way:
stream = await client.responses.create(
model="gpt-4.1",
input=input_messages,
previous_response_id=previous_response_id,
stream=True,
)
async for event in stream: # <-- Error occurs here, mid-stream
if event.type == 'response.created':
response_id = event.response.id
elif event.type == 'response.content_part.added':
# Push to UI immediately
send_to_client(event.part.text)
elif event.type == 'response.output_text.delta':
send_to_client(event.delta)
elif event.type == 'response.completed':
# Success
pass
Sometimes I get a random error during the for loop. Example of error:
httpx.RemoteProtocolError: peer closed connection without sending complete message body (incomplete chunked read)
My question is what is the best way to handle this?
My stream is already in the middle, I may have already called some tools, sent some tokens to the client, etc.