python-genai: Midsize PDF file yields Error 400: "The request's total referenced files bytes are too large to be read"

Environment details

  • Programming language: Python
  • OS: Mac OS 15.3.
  • Language runtime version: 3.10
  • Package version: v.1.1.0

Steps to reproduce

I have a midsize PDF file with 160MB and 127 pages. I can successfully upload the PDF with client.files.upload(file=pdf_path).

However, when I try to use the uploaded file in client.models.generate_content() I get this error:

ClientError: 400 INVALID_ARGUMENT. {'error': {'code': 400, 'message': "The request's total referenced files bytes are too large to be read", 'status': 'INVALID_ARGUMENT'}}

My code works on the same PDF, when I shorten it to say 10 pages.

How can I fix this error and use the full PDF?

Thanks in advance for any help in this matter.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 1
  • Comments: 17

Most upvoted comments

I have the same issue with document > 50MB. Hello @pamorgan , if you would share us how long it will take to resolve this issue, that would be great. Thank you

The service currently only supports pdf file of size 50MB or less and 300 pages or less. We will treat this issue as a missing documentation and will open an internal feature request to increase the supported pdf file size. Thank you for raising this issue.

Reopening - Thank you for reporting. The service team is investigating the root cause of the issue.

I can confirm that this works. I have created a PDF with 51231211 bytes. I now can count the tokens and OCR the file.

This finding seems to contradict your documentation here where you state that files can be up to 2GB.

You can use the File API to upload a document of any size. Always use the File API when the total request size (including the files, text prompt, system instructions, etc.) is larger than 20 MB.

Note: The File API lets you store up to 20 GB of files per project, with a per-file maximum size of 2 GB.

Is ~52MB really the size limit that I can use? How can I process PDFs (or other documents) that are larger than this?

EDIT: I tried to submit a larger PDF in parts in one prompt, each part having a file size below the limit. This doesn’t work neither. The OCR stops somewhere on the second PDF part.

The error message mentioned that the maximum file size is 52428800, can you try a pdf file under that limit and see if it works?

Thanks for the quick response, @rnckp . I just double checked my test script. My previous test is for a PDF of size 20MB instead of 200MB. Sorry about that!

I just produced a 160MB file and got a similar error message:

google.genai.errors.ClientError: 400 INVALID_ARGUMENT. {‘error’: {‘code’: 400, ‘message’: ‘Request contains an invalid argument.’, ‘status’: ‘INVALID_ARGUMENT’, ‘details’: [{‘@type’: ‘type.googleapis.com/google.rpc.DebugInfo’, ‘detail’: ‘[ORIGINAL ERROR] generic::invalid_argument: Document size exceeds supported limit: 166877608 v.s 52428800’}]}}

Hi, @rnckp

I think the problem might be caused by the type of the sample_file object. I was using the return value from the files.upload() API, as shown in below code:

sample_file = client.files.upload(file=doc_data, config={“mime_type”:‘application/pdf’})

Hi, @rnckp

I succeeded with a PDF of size around 20MB and it worked for me. Think it might be related to input token limitation? Can you try to get the token count of your prompt?

I used below script to get the token count:

response = client.models.count_tokens( model=‘gemini-2.0-flash-001’, contents=[ “summary the document”, document ], ) print(response)

And which model are you using in your test? Each model has different limits on input_token_limit, which you can get by calling client.models.list()