can1357/oh-my-pi · error

Gemini Files API upload finalization failed with HTTP ${fina

Error message

Gemini Files API upload finalization failed with HTTP ${finalizeResponse.status}

What it means

This error is thrown by the Gemini Files API provider when the second phase of an upload (finalizing a file that was uploaded via the resumable upload URL) completes but the HTTP response status is not ok. The library distinguishes between a network/transport failure (a different message) and a completed HTTP exchange with a non-2xx status, surfacing the status code so developers can identify the server-side rejection reason.

Source

Thrown at packages/coding-agent/src/blob-broker/provider-files-gemini.ts:135

				throw new Error("Gemini Files API upload initialization response is missing X-Goog-Upload-URL");

			let finalizeResponse: Response;
			try {
				finalizeResponse = await fetchImpl(uploadUrl, {
					method: "POST",
					headers: {
						"Content-Length": String(byteLength),
						"X-Goog-Upload-Command": "upload, finalize",
						"X-Goog-Upload-Offset": "0",
					},
					body: request.bytes,
					signal: request.signal,
				});
			} catch {
				throw new Error("Gemini Files API upload finalization request failed");
			}
			if (!finalizeResponse.ok) {
				throw new Error(`Gemini Files API upload finalization failed with HTTP ${finalizeResponse.status}`);
			}

			const file = parseFinalizedFile(await responseJson(finalizeResponse, "finalize"));
			return {
				provider: "google",
				id: file.name,
				uri: file.uri,
				mimeType: file.mimeType,
				bytes: byteLength,
				expiresAt: file.expiresAt,
				delete: {
					method: "DELETE",
					url: `${GEMINI_FILES_RESOURCE_URL}/${file.name}`,
					headers: { "x-goog-api-key": credential },
				},
			};
		},
		async delete(handle: ProviderFileHandle): Promise<void> {

View on GitHub (pinned to 9690622007)

Solutions

  1. Verify the Gemini API key (x-goog-api-key credential) is valid, active, and has Files API access; test with a simple models list call.
  2. Check the status code in the message: 401/403 fix the key, 429 back off and retry respecting quota, 4xx do not blindly retry since the finalize session may be invalid.
  3. Start a fresh upload (new resumable session) if the finalize URL was reused or the upload took longer than Gemini's session lifetime.
  4. Retry on 5xx statuses after a delay; these are transient server-side failures.
  5. Confirm network/proxy settings are not rewriting or blocking the request (e.g. corporate proxy returning 403).

Example fix

// before: retrying the same finalize on any failure
await retry(() => client.upload(request), 3);
// after: inspect status, only retry transient errors
try {
  await client.upload(request);
} catch (err) {
  if (!/HTTP 5\d\d/.test(err.message)) throw err; // 4xx: get a new upload session
  await Bun.sleep(1000);
  await client.upload(request);
}
Defensive patterns

Strategy: retry

Validate before calling

if (!credential?.trim()) throw new Error("Gemini API key required before upload");
if (typeof fetchImpl !== "undefined" && fetchImpl !== globalThis.fetch) console.warn("custom fetchImpl in use — check its error mapping");

Try / catch

try {
  const handle = await geminiClient.upload(request);
} catch (err) {
  const m = /HTTP (\d{3})/.exec(err.message ?? "");
  const status = m ? Number(m[1]) : 0;
  if (status >= 500 || status === 429) {
    await Bun.sleep(2 ** attempt * 500);
    return retryUpload();
  }
  throw err; // 4xx: fix key/session, don't retry
}

Prevention

When it happens

Trigger: Calling upload() on the Gemini file client when fetch of the finalize request returns a non-ok status (e.g. 400, 401, 403, 404, 500) from the Gemini Files API finalize endpoint.

Common situations: Expired or wrong GOOGLE/GEMINI API key (401/403); finalize URL expired or already used (400/410); file exceeds size or quota limits (413/429); transient Gemini service errors (5xx); malformed metadata sent during finalize.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/33da51bc6bf64a8e. Report an issue: GitHub.