fix: pass max_line_length to readline() to prevent LineTooLong on large SSE lines with MTLS.#2437
Merged
Merged
Conversation
c2e602a to
c69e75d
Compare
…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
c69e75d to
0e8f7bb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.