← Blog

Gemini Resumable Uploads Need an Offset Check

Parminder Singh
Parminder Singh··6 min read
Summarize with AI

Gemini Files API REST uploads start a resumable session, receive a session URL, and send file bytes with an X-Goog-Upload-Offset header. A production gateway should bind that URL to the authenticated principal and declared file metadata, require the offset expected by local session state, and verify the final File size and hash before releasing its URI for model use.

Platform & Architectureai-securityllm-securitypolicy-enforcementzero-trustarchitectureaudit
Gemini Resumable Uploads Need an Offset Check

Gemini's Files API uses a two-stage REST exchange. The client starts a resumable session with declared length and MIME type, receives an upload URL in a response header, then sends bytes to that URL with X-Goog-Upload-Offset. Google's current example uses offset 0 and combines upload, finalize in one command. A gateway that forwards those calls needs session state between the two HTTP requests.

I want the gateway to compare the submitted offset with the next byte position it has accepted for that session. A mismatch should stop before another body reaches Google. The completed File resource then supplies sizeBytes and sha256Hash for a separate completion check.

TL;DR

  • Bind each Gemini resumable upload URL to one authenticated principal and route, with declared byte count and MIME type plus an expiry.
  • Require X-Goog-Upload-Offset to equal the gateway's next expected byte position before forwarding a body.
  • Advance local offset state only after the provider exchange reaches the route's accepted response state.
  • Release the resulting file URI only after sizeBytes and sha256Hash match the approved upload record.

The upload spans two HTTP requests

Google's Files API guide shows the REST sequence. The start request goes to the upload endpoint with X-Goog-Upload-Protocol: resumable and X-Goog-Upload-Command: start. It also sends the total byte count in X-Goog-Upload-Header-Content-Length and the media type in X-Goog-Upload-Header-Content-Type. Google returns the upload URL in X-Goog-Upload-URL.

The second request carries the actual bytes. Google's example sets Content-Length to the file length, sends X-Goog-Upload-Offset: 0, and uses X-Goog-Upload-Command: upload, finalize. Those fields create a protocol relationship across two calls, even though the upload URL itself can look like a standalone destination.

The gateway needs a local record keyed by a protected session identifier or digest of the returned URL. Store principal and tenant; route and creation time; expiry and declared media type; declared bytes and next expected offset; and policy version. Avoid writing the full session URL into ordinary logs because it acts as a capability for the active upload session.

The AI gateway file upload scanning guide covers content inspection around the same upload boundary.

Offset validation belongs before body forwarding

For the documented one-body flow, the expected offset begins at 0. The gateway should parse X-Goog-Upload-Offset as a non-negative integer under a bounded syntax, reject duplicate header instances, and compare it with session state before streaming the body upstream. A missing, malformed, negative, or unexpected value gets a named local outcome.

If a route supports multiple upload calls, its ledger advances by the exact body bytes accepted under that route's tested provider contract. The next request must present that resulting position. This prevents a retry from silently appending the same segment twice and stops a later segment arriving before an earlier one. It also catches a client that reuses one upload URL after switching files.

My blunt opinion is that trusting the client-supplied offset defeats the point of calling the upload resumable. The server-side ledger is the continuity control. A header copied from a stale retry is only a claim.

Keep protocol claims narrow. Google's public Files example covers a complete body at offset 0. Routes that permit nonzero offsets need fixtures against the exact API version and response behavior they deploy.

Session URLs need identity and route binding

The start call already has authenticated application context before it reaches the Google endpoint. Bind the returned upload session to that principal and tenant. The later request to the session URL must arrive through the same governed route with matching identity context.

Compare the second call with the approved start record. Check the command family, declared total length, current offset, content length, media type where the protocol carries it, expiry, and maximum route size. A route can also cap the number of attempts and concurrent open sessions per principal. These are local policy decisions, separate from Google's file-processing state.

Use a digest for the upload URL in broad audit views. Put any recoverable URL in a restricted store with a short lifetime. On a review screen, show a thin progress bar labeled with expected and received bytes. A red marker at byte 4,194,304 makes an offset mismatch visible without exposing the session URL or file content.

Per-route AI policies provide the owner and version for size, MIME, offset, attempt, and expiry decisions.

State advances after an accepted exchange

A gateway can observe ambiguous transport outcomes. The upstream may receive bytes while the gateway loses the response, or the connection may fail before Google accepts the body. Advancing the local offset before the upstream result creates a gap. An automatic replay can create a duplicate when acceptance is uncertain.

Use an explicit attempt record. It should contain the preflight offset, body length, request digest, dispatch time, response status, and local outcome. Move the session's committed offset only under the response condition validated for the deployed protocol. An ambiguous result enters reconciliation or expires under policy instead of being guessed into success.

The one-call upload, finalize example has a simpler expected path: offset 0, body length equal to the approved total, then a returned File resource. Even there, preserve the attempt separately from final acceptance. A client timeout leaves acceptance unresolved. The returned File fields establish the byte and digest match after an HTTP response.

The safe idempotent LLM retries guide explains the same distinction between a retryable transport event and an application decision. Upload attempts need their own operation identifiers.

Completion needs byte and digest checks

Google's Files API reference defines sizeBytes and sha256Hash as output-only fields on a File resource. It also exposes the file URI and processing state. Those fields support a completion gate after the upload command returns.

Compute the hash over the exact approved byte stream and count the bytes at the gateway boundary. Compare both values with the final File resource before releasing its URI to a generation route. Preserve the declared length, observed length, provider size, local digest, provider digest, file name, URI, and completion disposition. A mismatch should quarantine the file reference and retain evidence under the route's storage policy.

Processing state is a separate check. The API reference identifies PROCESSING, ACTIVE, and FAILED. A successful byte transfer can still leave the file unavailable for inference while processing continues. URI release policy should therefore distinguish upload integrity from processing readiness.

The AI audit log schema supplies the principal, route, policy version, timestamps, and decision envelope around these file-specific fields.

Release fixtures should break continuity

Start with the current documented path: create a session for a known file, send the full body at offset 0, finalize it, and verify size plus SHA-256 before admitting the URI. Then repeat the same second request and confirm that the gateway rejects the stale offset before another body is forwarded.

Add an offset one byte ahead, one byte behind, a negative value, leading signs, excessive digits, duplicate headers, and a non-integer string. Send a body whose Content-Length exceeds the remaining declared bytes. Reuse the session under another authenticated principal and another tenant. Both should fail at the gateway boundary.

Test transport ambiguity with a controllable upstream stub. Accept the body and drop the response, then verify that local state enters the named uncertain path. Add final File resources with wrong sizeBytes, wrong sha256Hash, missing fields, PROCESSING, and FAILED. Unknown response fields should survive in protected raw evidence while policy holds the URI.

These fixtures should assert that blocked bodies never reach the provider stub. They should also confirm that audit records exclude raw file content and the recoverable upload URL.

DeepInspect

DeepInspect can govern Gemini Files API calls carried over authenticated user or agent HTTP traffic deliberately routed through its proxy. It can bind a resumable session to identity and route policy, validate X-Goog-Upload-Offset before forwarding the body, enforce byte ceilings, and record the final size, digest, and URI disposition.

The application owns safe file decoding, malware scanning, content classification, storage, retry UX, and the decision to reference an accepted file in a prompt. Direct Gemini uploads, local file handling, STDIO traffic, and any session request that bypasses the proxy remain outside DeepInspect's enforcement and evidence.

Book a technical deep dive at deepinspect.ai.

Frequently asked questions

What does X-Goog-Upload-Offset mean in the Gemini example?

It identifies the byte position for the upload body. Google's current REST example sends the complete file at offset 0 and combines the upload and finalize commands. A gateway should compare that header with its own session ledger before forwarding bytes.

Should the gateway accept a repeated offset after a timeout?

A timeout leaves acceptance uncertain. Record the attempt and use the route's tested reconciliation behavior. Blindly forwarding the same bytes can duplicate a segment, while blindly advancing the offset can skip data.

Is HTTP success enough to release the Gemini file URI?

The completion gate should compare observed bytes and the local SHA-256 digest with the output-only sizeBytes and sha256Hash fields on the returned File resource. Processing readiness remains a separate state check before inference.

Can one authenticated user continue another user's upload session?

Route policy should bind the session URL to the principal and tenant that opened it. A later request with different identity context should fail before its body is dispatched, even if the caller possesses the URL.

Does offset validation scan the file for malicious content?

Offset validation proves sequence continuity under the gateway's session record. File type validation, decompression controls, malware scanning, content classification, and authorization remain separate application or security services around the upload route.