fix(models): pass (filename, bytes, mime_type) tuple to acreate_file for OpenAI/Azure providers#4603
Conversation
Summary of ChangesHello @alejandro-sotoca-bts, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical bug preventing the successful upload of non-image files to OpenAI and Azure models when using LiteLLM. By correctly formatting the file upload request to include essential metadata like filename and MIME type, it ensures that these models can properly process attached documents, enhancing the robustness of file handling within the system. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
Response from ADK Triaging Agent Hello @alejandro-sotoca-bts, thank you for your contribution! Before we can merge this PR, you'll need to sign the Contributor License Agreement (CLA). You can do so by following the instructions in the "Details" link of the Thanks! |
There was a problem hiding this comment.
Code Review
This pull request correctly fixes a bug where file uploads to OpenAI/Azure providers were failing due to missing MIME types. The solution of passing a (filename, bytes, mime_type) tuple to litellm.acreate_file is appropriate. The logic to determine the filename, by preferring display_name and falling back to a generated name based on the MIME type, is robust. My main feedback is regarding testing. The change introduces new logic and modifies an existing call, which should be covered by unit tests to prevent future regressions. I've left a specific comment with suggestions for updating existing tests and adding a new one.
…for OpenAI/Azure providers When uploading non-image file attachments (e.g. PDFs) via the LiteLLM integration, the `acreate_file` call for OpenAI and Azure providers was passing only raw bytes as the `file` argument. LiteLLM and the OpenAI Files API expect a multipart upload with a `(filename, bytes, content_type)` tuple so the Content-Type header is set correctly. Passing raw bytes caused the stored file to have MIME type `None`, which the chat completions API then rejected with: Invalid file data: 'file_id'. Expected a file with an application/pdf MIME type, but got unsupported MIME type 'None'. Fix: pass `(display_name, data, mime_type)` to `acreate_file`, using the part's `display_name` when available, or a sensible default filename derived from the MIME type via the new `_filename_for_mime` helper. Fixes google#4174
ecec4f0 to
d072250
Compare
…roviders Update existing tests that assert acreate_file is called with raw bytes to instead expect the (filename, bytes, mime_type) tuple required by the LiteLLM/OpenAI multipart upload API. Add test_get_content_pdf_openai_uses_display_name_as_filename to verify that part.inline_data.display_name is used as the filename when available, falling back to _filename_for_mime otherwise.
Summary
Fixes #4174
When a user attaches a non-image file (e.g. a PDF) to a chat using
adk webor theload_artifactstool with an OpenAI or Azure model via LiteLLM, the request fails with:Root cause
In
src/google/adk/models/lite_llm.py, theacreate_filecall for_FILE_ID_REQUIRED_PROVIDERS(openai,azure) was passing only raw bytes as thefileargument:LiteLLM and the OpenAI Files API expect a multipart upload in the form
(filename, bytes, content_type). Without a filename, theContent-Typeheader is not set, so the stored file ends up with MIME typeNoneand the chat completions API rejects it.This only affects OpenAI and Azure because other providers (Gemini, Anthropic) send files inline as data URIs and do not use
acreate_file.Fix
Pass the full
(filename, bytes, mime_type)tuple toacreate_file, using the part'sdisplay_namewhen available, or a sensible default filename derived from the MIME type via the new_filename_for_mimehelper:Testing
Validated locally with
adk webusingopenai/gpt-4oandazure/gpt-4o: PDF attachments are now processed correctly and the MIME type error no longer occurs.