Python Veo 2 API Example

Hello,

New API user here, trying to generate videos with Veo 2 through google.genai - I’ve just been trying to run the tutorial code in python, shown below.

For my own use case I’ve added

client = genai.Client(api_key = os.getenv("GOOGLE_API_KEY"))

Which works with image_gen. But no matter whether I’m doing txt2video or img2video I get the same error with this tutorial code

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[6], line 16
     13     time.sleep(20)
     14     operation = client.operations.get(operation)
---> 16 for n, generated_video in enumerate(operation.response.generated_videos):
     17     client.files.download(file=generated_video.video)
     18     generated_video.video.save(f"video{n}.mp4")  # save the video

Is the tutorial broken? am I missing something?

I am able to generate an image with

prompt="Panning wide shot of a calico kitten sleeping in the sunshine"

imagen = client.models.generate_images(
    model="imagen-3.0-generate-002",
    prompt=prompt,
    config=types.GenerateImagesConfig(
      number_of_images=1
    )
)

imagen.generated_images[0].image.show()

but no videos.. any help would be appreciated .

See tutorial code below

import time
from google import genai
from google.genai import types

client = genai.Client()  # read API key from GOOGLE_API_KEY

operation = client.models.generate_videos(
    model="veo-2.0-generate-001",
    prompt="Panning wide shot of a calico kitten sleeping in the sunshine",
    config=types.GenerateVideosConfig(
        person_generation="dont_allow",  # "dont_allow" or "allow_adult"
        aspect_ratio="16:9",  # "16:9" or "9:16"
    ),
)

while not operation.done:
    time.sleep(20)
    operation = client.operations.get(operation)

for n, generated_video in enumerate(operation.response.generated_videos):
    client.files.download(file=generated_video.video)
    generated_video.video.save(f"video{n}.mp4")  # save the video

so - this is sort of fixed. I tried it for an hour or so, maybe a few times every 10 minutes. And now it’s working.

The error is sort of mentioned in the documentation - where it says if it doesn’t work because it’s resource constrained, try again…

This needs better error messaging to be a usable API.

If anyone else has this issue - I guess the solution is “try again just slamming the api until you’re lucky”?

Please let me know if anyone else has any insight/better ideas