The Research Report
Idempotent Uploads to Google Drive for Agent Pipelines
date: 2026-11-08
title: "Idempotent Uploads to Google Drive for Agent Pipelines"
author: Brian Taylor
Idempotent Uploads to Google Drive for Agent Pipelines
Brian’s article sync to Google Drive (and any nightly uploader in his trend-scout pipeline) runs on cron, retries on failure, and spans multiple processes. Those three properties are exactly what produces duplicate files: a stalled request, a retried create, or two workers grabbing the same work. The Drive API gives several primitives that make retries safe, but they are not automatic — you have to choose the right pattern.
Three upload modes; pick by size and failure cost
- Simple upload (
uploadType=media): one POST of raw bytes, 5 MB or less, no metadata. Good for tiny artifacts; bad for structured output because metadata is inferred or absent. - Multipart upload (
uploadType=multipart): metadata and bytes in one request, 5 MB or less. Use when the file is small and re-running the whole request is cheap. - Resumable upload (
uploadType=resumable): starts with a session request, then uploads bytes with PUT in one or more chunks. Required for files over 5 MB and strongly recommended for anything where a network drop would force a full re-send. One extra round trip per upload in exchange for crash-safety.
The trap is mixing the simple/multipart modes with retry logic designed for resumable flows. If a simple upload times out ambiguously, you cannot know whether Drive committed it, so a blind retry creates a second file.
The primitive that actually fixes duplicate creation: pre-generated IDs
Drive can hand you a list of pre-generated file IDs (generateIds). You then create with bodies like {"id": "<pregenerated-id>", ...}. Because the ID is fixed before any bytes move:
- A first attempt that succeeds commits that ID.
- A retry after an indeterminate server error or timeout either returns the same existing file (success) or a
409 Conflict; it does not create a duplicate.
Constraints to remember:
- Pre-generated IDs are not valid for creating Google Workspace files (Docs, Sheets, Slides) except folders and
drive-sdkMIME types. If your sync converts Markdown to Google Docs, you cannot rely on this trick and need a client-side lookup instead (files.listfiltered by parent + name, then update or create). - Keep the mapping from your pipeline’s event key to the Drive ID in your own ledger; the ID is just a retry token, not a discovery mechanism.
Full replacement vs. partial update
Resumable handling aside, be deliberate about PATCH vs PUT:
- HTTP
PATCHdoes a partial update;PUTdoes full replacement. Drive documents that using PUT when adding a new field to an existing resource can introduce breaking changes. - For retries, pattern: on unknown status, query by ID before deciding. If it exists,
update(media/PUT) to replace bytes; otherwise create. Treating an ambiguous success as “maybe created” and re-running full creates is what most duplicate bugs come from.
Operational checklist for the sync job
- Chunk work by a stable eventkey. Persist
{eventkey -> drivefileid} before the upload starts, not after. - Use pre-generated IDs whenever the target is a binary/text file, not a Workspace export.
- Choose upload mode by size: under 5 MB and cheap to re-run → multipart; larger or network-risky → resumable. Resumable’s initial POST includes metadata; the file ID only becomes stable once the session is created — so keep the event_key ledger as the source of truth, not the returned metadata.
- Make the finalize step idempotent: writing the same content twice to the same file ID is harmless; creating twice is not.
- Handle 308 (Resume Incomplete) for resumable sessions rather than re-initializing the session.
- On
409 Conflictfrom a pre-generated ID, look up by your event_key mapping, do not escalate — that is the expected outcome. - Rate-limit and backoff on 429; Drive quotas are per-user-per-300-seconds, and agent bursts hit them easily.
Why this matters for the pipeline
Brian’s pipeline already treats structured Output validity as a floor, not a ceiling (a prior article covered constrained decoding). The Drive sync is the analogous problem at the storage layer: writes appear successful locally while a duplicate lands remotely, because the definition of success was “upload returned 2xx” rather than “one file exists for this event_key.” Moving idempotency into the file identity (pre-generated IDs, or a prior lookup) is the equivalent fix to moving validation into a schema.
Summary: resumable uploads recover bytes; pre-generated IDs recover identity. Neither alone is enough — you need the event_key ledger tying them together.