cookbook icon indicating copy to clipboard operation
cookbook copied to clipboard

Passing uploaded File object directly in contents causes required oneof field 'data' must have one initialized field error in generate_content

Open dumko2001 opened this issue 6 months ago • 1 comments

Description of the bug:

The official Google GenAI Python SDK documentation and examples indicate that after uploading a file using client.files.upload(), you can pass the returned File object directly in the contents list for client.models.generate_content(), like this:

file = client.files.upload(file="path/to/sample.mp3") response = client.models.generate_content( model="gemini-2.0-flash", contents=["Describe this audio clip", file] ) print(response.text) The SDK is supposed to automatically convert the File object into a valid content part referencing the uploaded file. However, in practice, this results in a 400 INVALID_ARGUMENT error with the message:

required oneof field 'data' must have one initialized field This suggests that the conversion of the File object to a valid content part is failing, and the backend is receiving an empty or invalid content part.

Actual vs expected behavior:

Actual: Passing a File object directly in contents results in an API error indicating missing required data. Expected: The SDK should correctly convert the File object to a valid content reference so the backend receives a proper content part, allowing mixed inputs of text and files in the same request.

Any other information you'd like to share?

This issue breaks the documented and recommended usage pattern for uploading and referencing files in content generation. Workarounds involve manually building a content dict like {"file": {"uri": file.uri}} instead of passing the File object directly. This may be a regression or bug in the latest SDK version or the underlying API. Please advise on the correct usage or fix the SDK to properly handle File objects in contents.

dumko2001 avatar May 28 '25 12:05 dumko2001

Hi @dumko2001, I have tried to pass the audio file to the model contents which was uploaded using the client.files.upload() I did not face any issue. please refer to this gist. Thank you.

kiransair avatar May 29 '25 06:05 kiransair

Marking this issue as stale since it has been open for 14 days with no activity. This issue will be closed if no further activity occurs.

github-actions[bot] avatar Jul 04 '25 22:07 github-actions[bot]

This issue was closed because it has been inactive for 27 days. Please post a new issue if you need further assistance. Thanks!

github-actions[bot] avatar Jul 17 '25 22:07 github-actions[bot]