Hi everyone,
We are using the Gemini API (model: gemini-2.5-flash) to generate summaries for uploaded files such as PDFs, images, and Excel (XLS/XLSX).
We are seeing an intermittent issue where some files fail with a 400 INVALID_ARGUMENT error when processed via the API, even though:
- The same file uploads and summarizes successfully in the Gemini web app
There doesn’t seem to be a consistent pattern related to file type or size.
Error we receive:
ClientError: got status: 400 Bad Request.
{“error”:{“code”:400,“message”:“Request contains an invalid argument.”,“status”:“INVALID_ARGUMENT”}}
at throwErrorIfNotOK (…/_api_client.ts:629:27)
at async Models.generateContent (…/models.ts:61:12)
Hello @Niranjan_Gowda,
Welcome to AI Forum.
Thank you for reporting this. Since you mentioned this is intermittent and specific to file uploads with gemini-2.5-flash, it sounds like a race condition or a specific validation edge case rather than a fundamental configuration error.
If you are uploading files via the Files API and immediately calling generateContent, the file might not be ready yet. Even small files need a moment to process.
Sometimes a “400 Invalid Argument” occurs if the MIME type specified during the upload does not perfectly match what the model detects during inference. Verify that the mime_type passed to the API matches the actual file signature.
1 Like
By intermittent, I mean that the issue occurs only for specific files. For a few files, the API consistently returns a 400 error. We’ve tried multiple approaches—uploading via the Files API using a file buffer, providing a public file URL directly, and even using the sample request from the API documentation—but all of them result in the same error for those files.
We found this sample pdf on the internet for which it happens always - https://drive.google.com/file/d/1Hn4KxSVnFFTFIyzxJQaOXcMSVSaW3mGJ/view?usp=sharing
I tried to repair the pdf and reproduced it though it did not work, it gave a different message. Here is the code I used to repair.
def repair_pdf(pdf_bytes: bytes) -> bytes:
try:
import pymupdf as fitz # PyMuPDF
with fitz.open(stream=pdf_bytes, filetype="pdf") as doc:
return doc.tobytes(garbage=4, deflate=True)
except Exception as e:
raise ValueError(f"Failed to repair PDF: {e}")
Now the error message is,
400 INVALID_ARGUMENT. {'error': {'code': 400, 'message': "The request's total referenced files bytes are too large to be read", 'status': 'INVALID_ARGUMENT'}}
Not sure why this behavior is happening though, could it be because the model itself has a limit on file size?
I did try to wait and call generate_content() but it still says “400 Invalid Argument”. Here is the below logic I tried to use.
myfile = client.files.upload(file=repaired_path, config={'mime_type': 'application/pdf'})
# 5. Polling for file readiness (Resolves 400 Invalid Argument race condition)
print(f"File state: {myfile.state.name}")
while myfile.state.name == "PROCESSING":
print(". ", end="", flush=True)
time.sleep(2)
myfile = client.files.get(name=myfile.name)
if myfile.state.name != "ACTIVE":
raise ValueError(f"File failed to process: {myfile.state.name}")
print(f"\nFile ready. State: {myfile.state.name}")
# 6. Generate content (using a valid model name)
response = client.models.generate_content(
model="gemini-3-flash-preview",
contents=["Describe this file", myfile]
)
print("\nResponse text:")
print(response.text)
@Niranjan_Gowda @Bharath_Velamala
Thank you for sharing sample pdf and code. The size of the pdf file you shared is above 80 MB. For PDF files specifically, the current hard limit for processing is 50 MB and 1,000 pages per file. Even though the Files API allows uploading larger files (up to 2 GB) for storage, the models (Gemini 1.5 Pro/Flash and Gemini 2.0) often reject PDFs larger than 50 MB during the actual inference/processing stage, returning a 400 Invalid Argument error. You can find the file size limit in the documentation here.
1 Like