Repository navigation
Gemini: cannot bound or diagnose silent reads — HttpOptions.timeout only sets aiohttp total #5802
Description
Problem
In production we periodically see LlmAgent → Vertex calls go silent for minutes, then surface as a bare asyncio.TimeoutError with no actionable message. A real incident: one LLM call completed cleanly at 08:16:03, the next went silent until 08:19:29 (~3.5 minutes) before raising. Nothing in the logs in the silent window. From the bare exception, we cannot distinguish any of these:
- Never connected (DNS / TCP / TLS failure)
- Connected, request accepted, no response body ever returned
- Mid-stream stall (some bytes arrived, then connection went silent)
- Vertex honored its server-side deadline and returned 5xx
- TCP reset / FIN / NAT eviction
- Our client-side deadline fired against a still-processing server
Without that distinction we cannot decide whether to retry, fail over, escalate, or fix a real bug.
Why the current surface isn't enough
-
HttpOptions.timeoutis a single integer (ms) that becomesaiohttp.ClientTimeout(total=...)ingoogle/genai/_api_client.py(the per-request call tosession.request(..., timeout=aiohttp.ClientTimeout(total=http_request.timeout))). Onlytotalis set — nosock_read, nosock_connect, noconnect. -
aiohttp.ClientTimeout.totalalone does not guarantee the deadline fires on a truly silent read — see aio-libs/aiohttp#11740 (maintainer-confirmed Oct 2025).sock_readis required for a hard ceiling. -
There is no
TraceConfigseam. The SDK builds its own internalaiohttp.ClientSessionlazily and never exposes it. We cannot attach hooks foron_connection_create_start,on_request_end,on_response_chunk_received,on_request_exception. Without those, "silent for 3.5 minutes" gives zero signal about what state the transport was in. -
Per-request
HttpOptions(aiohttp_client=session)doesn't work under ADK. ADK's tracing serializesGenerateContentConfigviamodel_dump, and a liveaiohttp.ClientSessionis not pydantic-serializable. Result:PydanticSerializationError. -
HttpOptions(async_client_args={'timeout': ...})raisesTypeError— see googleapis/python-genai#1899.
Current workaround
Subclass Gemini and override the api_client @cached_property (per the docstring at google_llm.py:95-112), returning a Client(http_options=HttpOptions(aiohttp_client=<custom session with TraceConfig + per-phase timeouts>)).
This works, but it has real sharp edges:
- We must manually re-derive
_tracking_headers(),retry_options,base_url,api_version, and thevertexai=Truebranch. Any defaultapi_clientadds upstream that we don't mirror, we silently lose. (Tracking headers in particular are easy to drop and hard to notice.) - Session lifecycle is on the caller. The session is event-loop-affine, so it must be built inside FastAPI
lifespanand closed on shutdown. ADK provides no documented helper orlifespanhook for this — every adopter rediscovers it. - The injected session is not visible to ADK telemetry, so per-call diagnostic fields land in our own logs rather than ADK spans.
What we'd like
Any of the following, in roughly preferred order:
-
Land #4345 (already open, adds
custom_api_client/custom_live_api_clientconstructor params). This removes the need to subclass for the "I built my own Client" case. Related: #2560, #5027. -
Expose per-phase timeouts on
HttpOptionsfor callers who don't want to own a full session. Even justHttpOptions(sock_read_timeout=..., sock_connect_timeout=...)would fix the silent-read ceiling issue (point 2 above) without anyone needing to inject a session. -
Optional in-flight transport hooks (an ADK-level callback similar to
before_model_callback, but firing on transport transitions:on_connect_start,on_request_end,on_first_byte,on_chunk_received,on_request_exception). This is the diagnostic surface the OpenTelemetry GenAI spec marks as TODO and nothing in the ecosystem provides yet. -
At minimum, document the session-injection contract more loudly — loop-affinity, lifecycle ownership (
_api_client.py:2168-2174already skips closing user sessions, but this isn't called out anywhere user-facing), and which default kwargs anapi_clientoverride must preserve.
Related upstream issues
- googleapis/python-genai#1893 — genai request hangs in "zombie state" with open socket and no data (gemini-2.5-flash)
- googleapis/python-genai#911 —
HttpOptions(timeout=...)partially ignored - googleapis/python-genai#1899 —
async_client_args["timeout"]raisesTypeError - aio-libs/aiohttp#11740 —
ClientTimeout.totalalone can hang on silent reads
Environment
google-adk: 1.32.0google-genai: 1.75.0aiohttp: 3.12.13- Python: 3.12.13
- Runtime: FastAPI on AWS EKS, Vertex AI (Gemini-3 family)
Activity
sanketpatil06 commentedon May 25, 2026
Hi @KrishnaVemu , Thanks for the update. The request is currently being reviewed by our team, and we will let you know if anything further is needed. Thank you for your understanding.
GWeale commentedon Sep 28, 2026
you can pass your own client now instead of subclassing, e.g. Gemini(client=genai.Client(http_options=HttpOptions(aiohttp_client=your_session))) with your own timeouts and trace config. added in a01d516 and shipped in 2.8.0; per-phase timeouts on HttpOptions itself would need to come from googleapis/python-genai, so closing