Veo 3.1 Video Generation

Source notebook

Repo path: vision/getting-started/veo3_video_generation.ipynb · Open on GitHub · intermediate

Generates Veo 3.1 videos from text and images with optional Gemini prompt optimization.

Summary

This notebook teaches how to use the Google Gen AI SDK for Python with Veo 3.1 on Agent Platform. It demonstrates authentication, project and region setup, Gemini-assisted prompt construction, text-to-video generation, Cloud Storage output, and image-to-video generation from a starting frame. It also shows polling long-running video generation operations and displaying returned video bytes or Cloud Storage URIs.

Key code patterns

Initialize GenAI clients

from google import genai
 
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
LOCATION = os.environ.get("GOOGLE_CLOUD_REGION", "us-central1")
client = genai.Client(enterprise=True, project=PROJECT_ID, location=LOCATION)
gemini_client = genai.Client(enterprise=True, project=PROJECT_ID, location="global")

Uses an enterprise GenAI client for Veo in a regional location and Gemini in the global location.

Optimize Veo prompt with Gemini

response = gemini_client.models.generate_content(
    model="gemini-3.5-flash",
    contents=gemini_prompt,
)
prompt = response.text

Turns selected cinematic keywords into a single detailed prompt before video generation.

Generate text-to-video

operation = client.models.generate_videos(
    model="veo-3.1-generate-001",
    prompt=prompt,
    config=types.GenerateVideosConfig(
        aspect_ratio="16:9",
        number_of_videos=1,
        duration_seconds=6,
        resolution="1080p",
        generate_audio=True,
    ),
)

Shows the core Veo request shape for text prompts, including audio, duration, resolution, and aspect ratio.

Poll video operation

while not operation.done:
    time.sleep(15)
    operation = client.operations.get(operation)
 
if operation.response:
    show_video(operation.result.generated_videos[0].video.video_bytes)

Video generation is asynchronous, so the notebook polls until completion before reading the generated video.

Generate image-to-video

operation = client.models.generate_videos(
    model="veo-3.1-generate-001",
    prompt=prompt,
    image=types.Image.from_file(location=starting_image),
    config=types.GenerateVideosConfig(generate_audio=True),
)

Adds motion to a local starting image and optionally improves quality with a detailed prompt.

Models & APIs used

When to use this

Use this pattern to prototype Veo 3.1 text-to-video or image-to-video workflows with optional Gemini prompt refinement.

Gotchas & caveats

  • A Google Cloud project is required and the Agent Platform API must be enabled.
  • Colab users must authenticate with google.colab.auth.authenticate_user().
  • PROJECT_ID falls back to GOOGLE_CLOUD_PROJECT and LOCATION defaults to us-central1.
  • The notebook uses a global Gemini client but a regional Veo client.
  • 4k video generation can add latency up to several minutes.
  • Cloud Storage output requires setting output_gcs_uri to a valid bucket path.
  • Generated video operations must be polled before results are available.
  • Person generation must be configured with values such as allow_adult or dont_allow.

Best practices

  • Use detailed prompts for better video quality.
  • Include subject, action, scene, camera, style, temporal, audio, and dialogue details when relevant.
  • Use Gemini to synthesize keyword choices into a cohesive Veo prompt.
  • Use Veo 3.1 Fast when latency is a priority.
  • Use Veo 3.1 Lite for rapid prototyping and iteration.
  • Store generated videos in Cloud Storage when a persistent output location is needed.