Sign up and get 15% off the four courses and Lifetime.

Hosted video models, hands on

Lesson

Your first Veo call, in code

Lesson 2 of 10 in Hosted video models, hands on.

Checked against the makers' documentation on 4 November 2026.

Lesson

A Veo call in code is a short Python script that sends your prompt and settings to Google and saves the clip that comes back. It happens in three steps. You send the request and get back an operation name straight away, a kind of job ticket, because a clip takes between 11 seconds and 6 minutes to render by Google's own figures. Then you ask about that ticket every ten seconds until it says done, and save the file.

Every tool in this module works this way, and a script sends exactly the same request every time, which pays off once a job needs many clips. This lesson makes the coffee cup from the last lesson again.

Set up Google Cloud for Veo

At the time of writing, Veo runs through Google Cloud. Google switched off its Veo previews on the Gemini API, so a Gemini API key no longer reaches Veo. You need a Google Cloud project with billing turned on and the gcloud tool installed, plus the ~/veo-work folder, its private Python and the .env file from the setup lessons.

These commands update Google's Python library, google-genai, sign you in to Google Cloud and add two lines to .env. Replace your-project-id with the id of your own project before you run them.

  1. Get the project readyTerminal
    # The folder, private Python and .env from the setup lessonscd ~/veo-worksource .venv/bin/activateuv pip install -U google-genai python-dotenv# Should print Version: 2 and something. A 1.x means the Python is too old for this code.uv pip show google-genai | grep Version# Veo now runs on Google Cloud. Sign in once so local code can use your Cloud accountgcloud auth application-default login# Then add your project and Veo's region to .env (the Gemini key stays for the Omni lesson)echo "GOOGLE_CLOUD_PROJECT=your-project-id" >> .envecho "GOOGLE_CLOUD_LOCATION=us-central1" >> .env

The version check should print Version: 2 and something. A 1.x version means your Python is too old for this code. google-genai 2.34.0 needs Python 3.11, Python 3.10 gets 2.33.0, which also works here, and the Mac's own Python 3.9 installs 1.47.0 without a word.

Run first_veo.py

Save this script as first_veo.py in the same folder. It asks for one four-second clip of the coffee cup on veo-3.1-lite-generate-001, the cheapest Veo tier, at 720p. Check the current rate on Google Cloud's pricing page before you run it.

  1. first_veo.py: one clip, with retriesPython
    # first_veo.py: make one short Veo clip and save it beside this file.import timefrom dotenv import load_dotenvfrom google import genaifrom google.genai import errors, types load_dotenv()                       # copies the project and region from .env into the environmentclient = genai.Client(enterprise=True)  # Google Cloud's Agent Platform, signed in as you PROMPT = (    "Slow push in on a white ceramic coffee cup on a pale oak table, "    "soft morning window light from the left, a thin curl of steam rising. "    "Shallow focus. Quiet room tone.") def make_clip(prompt, filename, tries=3):    for attempt in range(1, tries + 1):        try:            # Ask for the clip. This returns at once with a job ticket, not with the video.            operation = client.models.generate_videos(                model="veo-3.1-lite-generate-001",  # the cheapest Veo tier, a preview on Google Cloud                source=types.GenerateVideosSource(prompt=prompt),                config=types.GenerateVideosConfig(                    aspect_ratio="16:9",                    resolution="720p",                    duration_seconds=4,  # 4, 6 or 8                ),            )            # Look at the ticket every 10 seconds until the job has finished.            while not operation.done:                print("Waiting for Veo...")                time.sleep(10)                operation = client.operations.get(operation)        except errors.ServerError as e:            # A fault on Google's side (a 500-type error). Wait, then send the same request again.            print(f"Server error on try {attempt}: {e}")            time.sleep(15)            continue         if operation.error:            # The job ran and failed. We saw "internal server issue" here once, and a plain retry worked.            print(f"Job failed on try {attempt}: {operation.error}")            time.sleep(15)            continue         videos = operation.response.generated_videos if operation.response else None        if not videos:            # Nothing came back, usually a safety filter. The same words rarely pass on a second try on the same model.            print("No video returned. Check the prompt:", operation.response)            return None         # On Google Cloud the clip comes back inside the answer, so write it straight to disk.        videos[0].video.save(filename)        print("Saved", filename)        return filename     print("Gave up after", tries, "tries.")    return None make_clip(PROMPT, "first_veo.mp4")

The line genai.Client(enterprise=True) tells the library to use Google Cloud, signed in as you. The prompt goes inside a source object, GenerateVideosSource, because the library now warns that passing the prompt straight into generate_videos is out of date. The while loop is the waiting step, and it checks the ticket every 10 seconds.

Run it with python first_veo.py. You should see a few "Waiting for Veo..." lines, then "Saved first_veo.mp4". Then change duration_seconds to 8 (it takes 4, 6 or 8) and run it again. In the course's test, the eight-second push travelled much further and ended with the cup's rim filling the frame.

Retry Google's faults and stop on your own mistakes

A server error, the 500 kind, is a fault on Google's side, so the script waits 15 seconds and sends the same request again, up to three tries. A job can also run and fail. In the course's tests that came back as "internal server issue" twice, and a plain second try worked both times.

A 400 error means the request itself is wrong, and sending it again sends the same mistake, so the script stops and you fix the code. A safety refusal comes back as no video at all. Google says it is not charged, but the same words usually fail again on the same model. In the course's tests, a harmless line of market-stall dialogue was refused twice on Lite and went through first time on Fast.

People are governed too. Text to video allows all people. Image to video, first and last frame and reference images allow adults only, and in the EU, the UK, Switzerland and the Middle East and North Africa, adults only is the rule for every route.

Watch for gaps in Google's docs

Google's docs were wrong in places when the course tested them. The parameter table printed durationSeconds in quotation marks, as text, and the live API refused text and wanted a bare number. The Python library converts it for you, but if you ever send the request yourself with curl, write 4 and leave the quotation marks off.

That was found on the Gemini API and may differ on Google Cloud. Google has also listed some Veo 3.1 models for retirement, so check the model page before you build on one.

The next lesson sends one product down the three routes into Veo: text, image, and first and last frame.