adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

support PDF file in Lite_llm

Open trung02 opened this issue 1 year ago • 17 comments

The current system does not support processing PDF files for interaction with Large Language Models (LLMs) through the LiteLLM framework. Many users rely on PDF documents—such as reports, research papers, or legal files—as their primary input. Without this support, they must manually extract text from PDFs before using the system, which is inconvenient and inefficient.

Image

trung02 avatar Apr 23 '25 08:04 trung02

There is an easy, albeit slightly annoying, workaround I found. Adding the following lines to the src/google/adk/models/lite_llm.py seemed to work for multiple configurations (including adk web).

from litellm import ChatCompletionFileObject

and in _get_content

elif part.inline_data.mime_type == "application/pdf":
        content_objects.append(
          ChatCompletionFileObject(
            type="file",
            file={
              "file_data": data_uri,
              "format": "pdf"
            }
          )
        )

It seems this functionality is not quite built out yet on the adk side, but LiteLLM has this integration and others which are not yet supported. Hopefully soon, as I am too busy to submit a full PR.

mwhite7112 avatar Apr 23 '25 18:04 mwhite7112

@hangfei and @boyangsvl shall I work on this ?

kavinkumar807 avatar Apr 24 '25 02:04 kavinkumar807

@hangfei i could see it is self assigned, if this is still valid for contribution, let me know I’m happy to help

kavinkumar807 avatar Apr 24 '25 03:04 kavinkumar807

@kavinkumar807 thanks for your interest. I will let @selcukgun (expert on LiteLLM) to evaluate if this can be contributed by community.

hangfei avatar May 31 '25 16:05 hangfei

Instead of going full support all the way to LLM

at the minimum, it should allow user to attach pdf, pass artifacts to MCP agent and then MCP server without error.

Any thought?

kimyu-ng-ds avatar Jun 05 '25 05:06 kimyu-ng-ds

This feature should have been implemented earlier. Please resolve it promptly.

wltnrhkd123 avatar Jun 09 '25 03:06 wltnrhkd123

@hangfei and @selcukgun please let us know if there is any way we can help. I really believe this is an important feature, so if you need any help implementing it, please let me know.

marcellomaugeri avatar Jul 04 '25 19:07 marcellomaugeri

I have just tried the @mwhite7112 's workaround and it works like a charm. Thank you

marcellomaugeri avatar Jul 04 '25 19:07 marcellomaugeri

It has clearly been well over two months since this issue was first mentioned. This is a very important issue, and it's something that can be resolved simply by adding a few lines of code. So why has it still not been addressed? It inevitably raises the suspicion that it was either omitted due to the developer's oversight, or that there is an intentional decision not to support file attachment for external LLM models. This is not a difficult task. I kindly request a prompt resolution.

wltnrhkd123 avatar Jul 08 '25 23:07 wltnrhkd123

@wltnrhkd123 today I proposed a pull request, let's hope for the best, I'm waiting code review

marcellomaugeri avatar Jul 09 '25 14:07 marcellomaugeri

fixed in https://github.com/google/adk-python/pull/1855, thanks for the fix :)

seanzhou1023 avatar Jul 11 '25 22:07 seanzhou1023

Why this is limited to pdf only? We should make the changes taking considerations of other type of attachment as well like docs, pptx, excel and so on

miyannishar avatar Jul 12 '25 04:07 miyannishar

@miyannishar I added support for pdf only because I had the chance to test it thoroughly. Please, feel free to add a feature request in the issue and someone (including me) will probably handle it.

marcellomaugeri avatar Jul 12 '25 07:07 marcellomaugeri

After attaching pdf to web UI, have y'all run into the following issue even after pointing to v1.8.0?


Give Feedback / Get Help: https://github.com/BerriAI/litellm/issues/new
LiteLLM.Info: If you need to debug this error, use `litellm._turn_on_debug()'.

2025-07-24 16:33:24,052 - ERROR - fast_api.py:928 - Error in event_generator: litellm.BadRequestError: VertexAIException BadRequestError - {
  "error": {
    "code": 400,
    "message": "Unsupported MIME type: pdf",
    "status": "INVALID_ARGUMENT"
  }
}
Traceback (most recent call last):
  File "../litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py", line 1686, in async_completion
    response = await client.post(
               ^^^^^^^^^^^^^^^^^^
        api_base, headers=headers, json=cast(dict, request_body)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )  # type: ignore
    ^
  File "../litellm/litellm_core_utils/logging_utils.py", line 135, in async_wrapper
    result = await func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "../litellm/llms/custom_httpx/http_handler.py", line 324, in post
    raise e
  File "../litellm/llms/custom_httpx/http_handler.py", line 280, in post
    response.raise_for_status()
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "../httpx/_models.py", line 829, in raise_for_status
    raise HTTPStatusError(message, request=request, response=self)
httpx.HTTPStatusError: Client error '400 Bad Request' for url 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash'
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "../litellm/main.py", line 540, in acompletion
    response = await init_response
               ^^^^^^^^^^^^^^^^^^^
  File "../litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py", line 1692, in async_completion
    raise VertexAIError(
    ...<3 lines>...
    )
litellm.llms.vertex_ai.common_utils.VertexAIError: {
  "error": {
    "code": 400,
    "message": "Unsupported MIME type: pdf",
    "status": "INVALID_ARGUMENT"
  }
}

kimyu-ng avatar Jul 24 '25 21:07 kimyu-ng

@kimyu-ng it never happened to me. Can you plase share a mre? Which model are you using?

What I can tell is that the MIME type was supposed to be "application/pdf"

marcellomaugeri avatar Jul 25 '25 06:07 marcellomaugeri

@kimyu-ng it never happened to me. Can you plase share a mre? Which model are you using?

What I can tell is that the MIME type was supposed to be "application/pdf"

gemini 2.5 flash

kimyu-ng avatar Jul 26 '25 23:07 kimyu-ng

@kimyu-ng just to confirm, are you using the Gemini 2.5 flash via the LiteLLM interface? If so, may I ask why you aren't using the direct interface to access a Gemini model? Instead of model = LiteLLM('gemini-2.5-flash') you can use the following: model='gemini-2.5-flash',

If you experience a bug with the latter, please open a new issue, as this one is about the LiteLLM interface. Feel free to tag me there, when I find a minute I'll try too

marcellomaugeri avatar Jul 27 '25 07:07 marcellomaugeri