Repository navigation
fix(gemini): read data URLs in linear time, send https images for Gemini to fetch, and read every block as a content filter - #342
Conversation
…ini to fetch, and read every block as a content filter The data-URL pattern /^data:([^;,]+)[^,]*?(;base64)?,(.*)$/is backtracked quadratically on a string with no comma: a 1 MB `data:AAAA...` image URL held the event loop for minutes before any request. Data URLs are now read as WHATWG Fetch reads them, each step linear: tabs and line breaks dropped, the media type before the first comma, `;base64` with spaces or a tab around it, the payload percent-decoded before it is read as base64, and the base64 checked against its alphabet and length. - An https image URL is sent as fileData, which Gemini fetches itself (up to 15 MB a file); its type is named from the path's extension when it has one. An http URL is still refused, and the provider fetches nothing. - The inline limit is Gemini's current 100 MB per request, counted in base64 characters as Gemini counts it, in place of the 20 MB rule it replaced. - The errors raised before a request (a refused URL, a malformed data URL, too much inline data) carry HTTP status 400, so the provider health registry reads them as the caller's error and they no longer open Gemini's breaker. - A prompt Gemini blocks (promptFeedback.blockReason, with no candidates) and an answer stopped with no content by BLOCKLIST, PROHIBITED_CONTENT, SPII or the IMAGE_ filter reasons throw content_filter naming the reason, as SAFETY and RECITATION did. They returned an empty answer, which the vision tier read as an image with no text.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Reviewer's GuideGemini image handling now uses linear-time, WHATWG-compatible data URL parsing, delegates HTTPS image fetching to Gemini, measures inline payloads against the 100 MiB base64 limit, and reports malformed inputs and all content-policy blocks as structured caller or content-filter errors. Tests cover provider behavior, streaming, vision integration, breaker health, and documentation reflects the new routes and limits. Sequence diagram for Gemini image submission routessequenceDiagram
participant Caller
participant GeminiProvider
participant GeminiAPI
participant ImageHost
Caller->>GeminiProvider: generateText(image_url)
alt https image URL
GeminiProvider->>GeminiProvider: imagePartOf(url)
GeminiProvider->>GeminiAPI: generateContent(fileData.fileUri)
GeminiAPI->>ImageHost: Fetch image
ImageHost-->>GeminiAPI: Image bytes
else data URL
GeminiProvider->>GeminiProvider: imagePartOf(url)
GeminiProvider->>GeminiAPI: generateContent(inlineData)
else http or malformed URL
GeminiProvider-->>Caller: GeminiProviderError(status 400)
end
Sequence diagram for Gemini content-filter error handlingsequenceDiagram
participant Caller
participant GeminiProvider
participant GeminiAPI
participant Fallback
Caller->>GeminiProvider: generateText()
GeminiProvider->>GeminiAPI: generateContent()
alt prompt blocked
GeminiAPI-->>GeminiProvider: promptFeedback.blockReason
else response filtered
GeminiAPI-->>GeminiProvider: finishReason with no content
end
GeminiProvider->>GeminiProvider: contentFilterBlock(blockReason, finishReason)
GeminiProvider-->>Fallback: GeminiProviderError(content_filter)
Fallback-->>Caller: Policy-aware fallback or error
Flow diagram for linear-time Gemini data URL parsingflowchart TD
A[Data URL] --> B[Remove tabs and line breaks]
B --> C[Find first comma]
C --> D{Base64 marker?}
D -- Yes --> E[Percent-decode payload]
E --> F[Validate forgiving base64 alphabet and length]
D -- No --> G[Percent-decode payload]
G --> H[Encode bytes as base64]
F --> I[Check non-empty payload and 100 MiB base64 limit]
H --> I
I --> J[Build inlineData]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
…or the request The fallback chain and the completion gateway read a failed attempt's tokens from error.details.usage. The content_filter errors carried none, so a blocked Gemini attempt's billed prompt tokens were left out of fallback results and session ledgers. Both the completion and the streaming path now attach the usage they mapped.
## [0.13.69](v0.13.68...v0.13.69) (2026-10-12) ### fix * **gateway:** onFallback reports the most recent error, as the walkers do (#348) ([c1b4847](c1b4847)) * **gemini:** read data URLs in linear time, send https images for Gemini to fetch, and read every block as a content filter (#342) ([ad4df66](ad4df66))
Summary
GeminiProviderread an image's data URL with/^data:([^;,]+)[^,]*?(;base64)?,(.*)$/is. On a string with no comma, the greedy and lazy loops cover the same characters, so the match backtracks about n²/2 steps: a 1 MBdata:AAAA...URL held the Node event loop for minutes, synchronously, before any request. A host that forwards a caller's image URL to a Gemini-backedgenerateText, a vision tier or a GMI turn could be stalled by one request.Data URLs are now read as WHATWG Fetch reads them, each step linear in the URL's length:
;base64may have spaces or a tab around it.The same change settles the other findings of the cold read on Gemini's image parts:
fileData, which Gemini fetches itself, up to 15 MB a file (Google, 2026-01-12; file input methods).fileData.mimeTypeis optional in the v1beta API, and it is named when the path ends in an image extension. An http URL is still refused, and the provider fetches nothing.promptFeedback.blockReason, which comes with no candidates) and an answer stopped with no content byBLOCKLIST,PROHIBITED_CONTENT,SPIIor theIMAGE_filter reasons now throwcontent_filternaming the reason, asSAFETYandRECITATIONdid. They returned an empty answer, which the vision tier read as an image with no text.Linked issues
None: the findings come from a cold read of the 2026-10-08 changes to Gemini's image parts.
Type of change
How this was verified
CI runs the tests; nothing was run locally.
GeminiProvider.test.ts: the WHATWG spellings, each malformed URL refused with status 400 before any request, a 100,000-character URL with no comma refused in under a second, 30 MiB of base64 sent and 120 MiB refused, https URLs sent asfileData, and http refused.GeminiProvider.safety.test.ts: a blocked prompt, the new filter finish reasons,OTHERleft as a finish reason, and a blocked prompt while streaming.createVisionPipeline.gemini.e2e.test.ts, through the realgenerateTextandGeminiProviderwith onlyfetchstubbed: an https image reaches Gemini asfileDatawith no other request, a blocked prompt fails the scan naming the reason, and five malformed data URLs leave Gemini's breaker closed.The docs (
LLM_PROVIDERS.md,VISION_PIPELINE.md) describe the two image routes, the limits and the block handling.A second commit (bd75a97) keeps the usage Gemini reported on the
content_filtererror, asdetails.usage, where the fallback chain and the completion gateway read a failed attempt's tokens.Migration notes
None: no option or type is removed. An https image URL that the provider refused now reaches Gemini as
fileData, and inline images up to 100 MB of base64 per request are sent.Checklist
!before the colon for a change that breaks users (feat!:orfeat(api)!:).