Skip to content

ReadTimeout and ssl.c:999 Handshake errors during Image uploads (while text-only requests succeed) #2377

@hemantkAnvex

Description

@hemantkAnvex

When using the google-genai SDK to upload image payloads (e.g., JPEG frames approx 200KB-400KB), the client consistently throws ReadTimeout or _ssl.c:999: The handshake operation timed out.

This issue occurs even with extremely generous timeout settings (e.g., 60s or 90s) and persists across multiple SDK versions.
Crucially:

Text-only requests using the same genai.Client and API Key succeed instantly.
Raw HTTPX requests (bypassing the SDK completely) succeed 100% of the time with the exact same image payload and API endpoint, provided HTTP/2 is disabled.
This suggests an internal bug in the SDK’s httpx wrapper or its HTTP/2 multiplexing logic when handling multipart/JSON binary data.

Environment details

  • SDK Version: google-genai>=0.1.1 (tested up to 1.75.0)
  • Python Version: 3.11 / 3.12
  • OS: Linux (Ubuntu 22.04 / Debian Bookworm)
  • Deployment: Docker containers (Bridge network, MTU 1400/1500)

Reproduction Snippet (Fails):

from google import genai
from google.genai import types

client = genai.Client(api_key="YOUR_KEY", http_options={'timeout': 60.0})

# This consistently fails with ReadTimeout or SSL handshake timeout
response = client.models.generate_content(
    model='gemini-2.5-flash',
    contents=[
        types.Part.from_bytes(data=image_bytes, mime_type="image/jpeg"),
        "Analyze this image"
    ]
)

Proof of Workaround (Succeeds):

Bypassing the SDK with raw httpx and disabling HTTP/2 works perfectly:

import httpx
import base64

url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=YOUR_KEY"
image_b64 = base64.b64encode(image_bytes).decode('utf-8')

payload = {
    "contents": [{"parts": [
        {"text": "Analyze this image"},
        {"inline_data": {"mime_type": "image/jpeg", "data": image_b64}}
    ]}]
}

# This works perfectly every time
with httpx.Client(http2=False, timeout=60.0) as client:
    response = client.post(url, json=payload)
    print(response.status_code) # Returns 200

Expected Behavior:

The SDK should handle image uploads as reliably as raw HTTP requests without timing out during the write or handshake phase.

Metadata

Metadata

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions