Skip to content

fix: pass max_line_length to readline() to prevent LineTooLong on large SSE lines with MTLS.#2437

Merged
copybara-service[bot] merged 1 commit into
mainfrom
copybara/915454389
May 15, 2026
Merged

fix: pass max_line_length to readline() to prevent LineTooLong on large SSE lines with MTLS.#2437
copybara-service[bot] merged 1 commit into
mainfrom
copybara/915454389

Conversation

@copybara-service
Copy link
Copy Markdown

fix: pass max_line_length to readline() to prevent LineTooLong on large SSE lines with MTLS.

When using AsyncAuthorizedSession (Vertex AI + ADC), the google-auth library
creates its own internal aiohttp.ClientSession with the default
read_bufsize=2**16. This gives the underlying StreamReader a _high_water of
131072 bytes, which is the effective limit for readline().

Streaming responses from thinking models can include large thoughtSignature
fields, or generated images, that push a single SSE data: line beyond 131072 bytes, causing aiohttp to raise LineTooLong.

The fix passes max_line_length=READ_BUFFER_SIZE (4MB) explicitly to every
readline() call in _aiter_response_stream(), overriding the limit regardless
of which code path created the underlying session. This covers both the direct
AiohttpClientSession path (API key auth) and the AsyncAuthorizedSession path
(Vertex AI + ADC).

Test: added test_aiohttp_large_sse_line_with_thought_signature which uses a
mock that enforces the real aiohttp LineTooLong limit, and streams a 150KB SSE
line that would previously fail. Manually verified, this test fails before the change.

@copybara-service copybara-service Bot force-pushed the copybara/915454389 branch 2 times, most recently from c2e602a to c69e75d Compare May 15, 2026 15:18
…ge SSE lines with MTLS.

When using AsyncAuthorizedSession (Vertex AI + ADC), the google-auth library
creates its own internal aiohttp.ClientSession with the default
read_bufsize=2**16. This gives the underlying StreamReader a _high_water of
131072 bytes, which is the effective limit for readline().

Streaming responses from thinking models can include large thoughtSignature
fields, or generated images, that push a single SSE data: line beyond 131072 bytes, causing aiohttp to raise LineTooLong.

The fix passes max_line_length=READ_BUFFER_SIZE (4MB) explicitly to every
readline() call in _aiter_response_stream(), overriding the limit regardless
of which code path created the underlying session. This covers both the direct
AiohttpClientSession path (API key auth) and the AsyncAuthorizedSession path
(Vertex AI + ADC).

Test: added test_aiohttp_large_sse_line_with_thought_signature which uses a
mock that enforces the real aiohttp LineTooLong limit, and streams a 150KB SSE
line that would previously fail. Manually verified, this test fails before the change.
PiperOrigin-RevId: 916020090
@copybara-service copybara-service Bot force-pushed the copybara/915454389 branch from c69e75d to 0e8f7bb Compare May 15, 2026 15:34
@copybara-service copybara-service Bot merged commit 0e8f7bb into main May 15, 2026
@copybara-service copybara-service Bot deleted the copybara/915454389 branch May 15, 2026 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant