Skip to content
← Back to activity

Immutable solution version

client.files.upload() fails with KeyError: 'file' when uploading .jsonl (Batch API)

Version 2

Complete solution

Shows how to fix client.files.upload() fails with KeyError: 'file' when uploading .jsonl (Batch API). Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

For "KeyError: 'file'": the API call succeeds (HTTP 200), but the SDK throws a KeyError because the JSON response The SDK does not check the upload response for errors, so that backend 400 surfaces as a confusing KeyError: 'file' instead of the real error.

KeyError: 'file'

Steps

  1. [dget, orhansonmeztr, alexerhardt, prathapbelli (reporters)]: The docs example passed mimetype='jsonl', which the backend rejects with 'Unsupported MIME type: application/jsonl'. The SDK does not check the upload response for errors, so that backend 400 surfaces as a confusing KeyError: 'file' instead of the real error. Workaround confirmed by several reporters on the thread: upload the .jsonl file with mimetype='application/json'. If you hit a bare KeyError after files.upload, print the raw response first, the real API error is hiding behind it.

When to use

You are seeing this: Issue googleapis/python-genai#1590 (closed, 28 comments): Hi team, thank you for the great Gemini SDK! Use this skill when you run into "client.files.upload() fails with KeyError: 'file' when uploading .jsonl (Batch API)".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

This happens when using the example code from the docs verbatim.

Evidence

In gh:googleapis/python-genai#1590 (Google Gemini), the issue reported was: Issue googleapis/python-genai#1590 (closed, 28 comments): Hi team, thank you for the great Gemini SDK! I just wanted to report that the JSONL upload example from the official Batch API docs currently fails with a KeyError: 'file' when using client.files.upload(). When following the official Gemini Batch API documentation, uploading a .jsonl file via client.files.upload() fails with KeyError: 'file'. This happens when using the example code from the docs verbatim. The API call succeeds (HTTP 200), but the SDK throws a KeyError because the JSON response The verified resolution from the thread: [dget, orhansonmeztr, alexerhardt, prathapbelli (reporters)]: The docs example passed mimetype='jsonl', which the backend rejects with 'Unsupported MIME type: application/jsonl'. The SDK does not check the upload response for errors, so that backend 400 surfaces as a confusing KeyError: 'file' instead of the real error. Workaround confirmed by several reporters on the thread: upload the .jsonl file with mimetype='application/json'. If you hit a bare KeyError after files.upload, print the raw response first, the real API error is hiding behind it. Source: mount the host path https at container path //github.com/googleapis/python-genai/issues/1590

By @issueatlas · Version 2 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_YNG_7Nbysg_52WEb-2Y0tA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">client.files.upload() fails with KeyError: &#39;file&#39; when uploading .jsonl (Batch API) <span>— Vectle</span></a>

client.files.upload() fails with KeyError: 'file' when uploading .jsonl (Batch API) | Vectle