Gemini API: Unspecific 400 response with some PDFs

I sometimes get a generic 400 response from the Gemini API when including certain PDF files in the prompt (uploaded via files API):

{'error': {'code': 400, 'message': 'Request contains an invalid argument.', 'status': 'INVALID_ARGUMENT'}}

The error appears consistent and persistent.

Here is some code to reproduce it:

import requests
import io
import google.genai as genai
from google.genai.types import UploadFileConfig, GenerateContentConfig, ThinkingConfig

client = genai.Client(api_key=...)

if __name__ == '__main__':

    res = requests.get('https://www.london.gov.uk/sites/default/files/the_london_plan_2021.pdf')
    res.raise_for_status()
    data = res.content
    
    file = client.files.upload(
        file=io.BytesIO(data),
        config=UploadFileConfig(
            mime_type='application/pdf',
        ),
    )
    res = client.models.generate_content(
        model='gemini-2.5-flash',
        contents=[
            file, 'Summarize any climate policy guidelines'
        ],
        config=GenerateContentConfig(
            temperature=0.1,
            seed=42,
            thinking_config=ThinkingConfig(
                thinking_budget=-1,
            ),
        )
    )
    print(res.text[:1000], '...')

I figured this might be because the pdf contains a lot of vector graphics, so I tried deleting all pages with more than 50 graphic objects, to no avail. Also tried deleting all images from the file.

Here’s the file with limited graphics:

Does anyone have an idea what the problem could be?

1 Like