Peer closed connection without complete message body

Hello, I’m having the problem of “peer closed connection without sending complete message body (incomplete chunked read)” intermittently. I haven’t been able to identify the cause, but the application is already using the streaming method recommended by the OpenAI documentation (https://platform.openai.com/docs/guides/streaming-responses).

Example code:
<>
stream = self.client.responses.create(
model="gpt-4o",
instructions=prompt,
input=[
{
"role": "user",
"content": [
{"type": "input_text", "text": ocr_text},
{
"type": "input_image",
"image_url": f"data:image/png;base64,{base64_image}",
},
],
},
],
stream=True,
temperature=0,
)

result = ""
usage = None

for event in stream:
# Text streaming chunks
if event.type == "response.output_text.delta":
result += event.delta

# Final event with usage
elif event.type == "response.completed":
usage = event.response.usage

raw_output = rsult
</>

SDK version: 2.7.1

I’ve checked several forums here and it always comes back to this forum, so the problem is still unresolved?

1 Like

Hi!

Welcome to our forum!

Thanks for sharing.

With just this snippet, it’s hard to tell what’s happening (and where the connection is being closed).

Could you share a little more?

  • The full error traceback
  • Your runtime environment (local / Docker / cloud, and whether there’s any proxy or load balancer in between)
  • The smallest example that still reproduces the issue (including how you build base64_image, the image size, and a short ocr_text)

Also, note there’s a small typo in your snippet: raw_output = rsult should be result.

These details will help us understand the situation and narrow down what’s causing the “peer closed connection without sending complete message body (incomplete chunked read)” error.

This information helps us understand what’s going on :blush:

2 Likes

I’m raising this with the team so they can take a closer look. To help us escalate this properly, could you please confirm that you haven’t set unusually low timeout values on your side, and whether this is a new issue for you?

Thanks for your understanding. This information will make it easier to get the right attention on the problem, as these are among the first questions that will need to be answered anyway!

1 Like