Gemini-2.5-flash api cannot process video input

image

On the website, I can get the response from the Gemini-2.5-flash model. But when I use the API service, this error appears.

My request code is as follows:

def call_gemini_api(video_file_name, prompt: str, api_key: str):
    client = genai.Client(api_key=api_key)

    video_bytes = open(video_file_name, 'rb').read()

    response = client.models.generate_content(
        # model='models/gemini-2.0-flash',
        model="gemini-2.5-flash-preview-04-17",
        contents=types.Content(
            parts=[
                types.Part(
                    inline_data=types.Blob(data=video_bytes, mime_type='video/mp4')
                ),
                types.Part(text=prompt)
            ]
        )
    )

    return response

Same issue with gemini-2.5-flash-preview.
Works if using gemini-2.0-flash.

Hi, there seems to be an issue when we try to pass video data inline in 2.5-flash-preview. Have you tried using the file API? It works as expected.
Below is the sample code.

myfile = client.files.upload(file="sample.mp4")
response = client.models.generate_content(
    model="gemini-2.5-flash-preview-04-17", contents=[myfile, "Summarize this video."]
)

print(response.text)

I have tried your method and the error is :
google.genai.errors.ClientError: 400 FAILED_PRECONDITION. {‘error’: {‘code’: 400, ‘message’: ‘The File q91mn19efz1u is not in an ACTIVE state and usage is not allowed.’, ‘status’: ‘FAILED_PRECONDITION’}}

Seems like you are trying to process the video file while it is in processing state, please ensure the video file is active before starting the processing

video_file = client.files.upload(file="video.mp4")
video_file.state.name

This is my code:

client = genai.Client(api_key=api_key)

myfile = client.files.upload(file="ideos_resampled-00115180-Scene-071.mp4")
response = client.models.generate_content(
        model="gemini-2.5-flash-preview-04-17", contents=[myfile, "Summarize this video. Then create a quiz with an answer key based on the information in this video."]
    )

Thank you so much for your reply!

You can run below code:

client = genai.Client(api_key=api_key)
myfile = client.files.upload(file="ideos_resampled-00115180-Scene-071.mp4")

while myfile.state == "PROCESSING":
      print('Waiting for video to be processed.')
      time.sleep(5)
      video_file = client.files.get(name=myfile.name)

response = client.models.generate_content(
        model="gemini-2.5-flash-preview-04-17", contents=[myfile, "Summarize this video. Then create a quiz with an answer key based on the information in this video."]
    )

It works!! Thank you so much !!

Hi, using client.files.upload is much slower than using inline api when I try to process a large amount of videos. Could you please tell me how to fix the 500 problem when using inline api?

Hey @Ming_Zhang , what’s the size of your video file? It is recommended to include video data directly only if the file is under 20 mb.

My video file is much smaller than 20mb, it is about 300KB. I try many different videos but still can’t use inline api to send my video. All I know is that the response code from genai is 503.

Could you share the sample code??

Hey please check code here:

import os
from google import genai
from google.genai import types

model = 'gemini-2.5-flash-preview-05-20'
api_key = 'xxx'
system_prompt = 'You are a good video analysis.'
prompt = 'tell me the content of the video.'
video_path = 'xxxxxxx.mp4'

client = genai.Client(api_key=api_key)
chat = client.chats.create(model=model)
chat_config = types.GenerateContentConfig(
    response_mime_type="text/plain",
    system_instruction=[
        types.Part.from_text(text=system_prompt),
    ],
)
video_bytes = open(video_path, 'rb').read()
message = [
    types.Part(
        inline_data=types.Blob(data=video_bytes, mime_type='video/mp4')
    ),
    prompt
]
response = chat.send_message(message=message, config=chat_config)

print(response.text)

I always get the errors as following:

Traceback (most recent call last):
  File "/home/ubuntu/codes/test_gemini.py", line 26, in <module>
    response = chat.send_message(message=message, config=chat_config)
  File "/home/ubuntu/miniconda3/envs/test/lib/python3.10/site-packages/google/genai/chats.py", line 259, in send_message
    response = self._modules.generate_content(
  File "/home/ubuntu/miniconda3/envs/test/lib/python3.10/site-packages/google/genai/models.py", line 5630, in generate_content
    response = self._generate_content(
  File "/home/ubuntu/miniconda3/envs/test/lib/python3.10/site-packages/google/genai/models.py", line 4593, in _generate_content
    response_dict = self._api_client.request(
  File "/home/ubuntu/miniconda3/envs/test/lib/python3.10/site-packages/google/genai/_api_client.py", line 755, in request
    response = self._request(http_request, stream=False)
  File "/home/ubuntu/miniconda3/envs/test/lib/python3.10/site-packages/google/genai/_api_client.py", line 684, in _request
    errors.APIError.raise_for_response(response)
  File "/home/ubuntu/miniconda3/envs/test/lib/python3.10/site-packages/google/genai/errors.py", line 103, in raise_for_response
    raise ServerError(status_code, response_json, response)
google.genai.errors.ServerError: 500 INTERNAL. {'error': {'code': 500, 'message': 'An internal error has occurred. Please retry or report in https://developers.generativeai.google/guide/troubleshooting', 'status': 'INTERNAL'}}

But when I change my model to gemini-2.0-flash, everything will be OK. I need to use gemini-2.5-flash and inline api together and please help me. Thanks!

I try to upgrade google-genai to 1.18.0 and get the following error:

Traceback (most recent call last):
  File "/home/ubuntu/codes/test_gemini.py", line 27, in <module>
    response = chat.send_message(message=message, config=chat_config)
  File "/home/ubuntu/miniconda3/envs/test/lib/python3.10/site-packages/google/genai/chats.py", line 259, in send_message
    response = self._modules.generate_content(
  File "/home/ubuntu/miniconda3/envs/test/lib/python3.10/site-packages/google/genai/models.py", line 5958, in generate_content
    response = self._generate_content(
  File "/home/ubuntu/miniconda3/envs/test/lib/python3.10/site-packages/google/genai/models.py", line 4921, in _generate_content
    response_dict = self._api_client.request(
  File "/home/ubuntu/miniconda3/envs/test/lib/python3.10/site-packages/google/genai/_api_client.py", line 765, in request
    response = self._request(http_request, stream=False)
  File "/home/ubuntu/miniconda3/envs/test/lib/python3.10/site-packages/google/genai/_api_client.py", line 694, in _request
    errors.APIError.raise_for_response(response)
  File "/home/ubuntu/miniconda3/envs/test/lib/python3.10/site-packages/google/genai/errors.py", line 103, in raise_for_response
    raise ServerError(status_code, response_json, response)
google.genai.errors.ServerError: 500 INTERNAL. {'error': {'code': 500, 'message': 'An internal error has occurred. Please retry or report in https://developers.generativeai.google/guide/troubleshooting', 'status': 'INTERNAL'}}

Hey , thanks for flagging. There seems to be an issue with the 2.5 models inline video processing. The issue has been reported.

Hi, Thanks for reporting the issue! Do you have any estimate when the inline api would be fixed?