> ## Documentation Index
> Fetch the complete documentation index at: https://docs.modular.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Video generation

> Generate videos from text or images with the responses API

Generate videos from text or images with the [responses
API](/api/inference/create-response). The API returns generated
videos as base64-encoded data.

For information about all request and response fields, see the
[responses API reference](/api/inference/create-response). For
available video generation models, see [Supported models](/models).

<Tip>
  The responses API follows the provider-agnostic
  [Open Responses](https://huggingface.co/blog/open-responses) specification.
</Tip>

## Generate a video

To generate a video, set `input` to a text description of the video and set
generation parameters in `provider_options.image`.

The following example generates a video from a text prompt and saves it as
`output-text-to-video.mp4`.

<Tabs>
  <Tab title="Python">
    Send a prompt, decode the returned video data, and save the video:

    ```python title="generate-video.py" theme={null}
    import base64
    from pathlib import Path

    from openai import OpenAI

    client = OpenAI(
        base_url="https://api.modular.com/v1",
        api_key="<your-api-key>",  # Load your key
    )

    response = client.responses.create(
        model="Wan-AI/Wan2.2-T2V-A14B-Diffusers",
        input=(
            "A campfire crackles in a forest clearing at night, with sparks "
            "spiraling into a star-filled sky."
        ),
        extra_body={
            "provider_options": {
                "image": {
                    "height": 512,
                    "width": 512,
                    "steps": 28,
                }
            }
        },
    )

    video_data = response.output[0].content[0].image_data
    Path("output-text-to-video.mp4").write_bytes(base64.b64decode(video_data))
    ```
  </Tab>

  <Tab title="curl">
    This example runs on Linux and requires `jq`. Send a prompt and decode the
    returned video data:

    ```bash theme={null}
    curl -X POST https://api.modular.com/v1/responses \
      -H "Authorization: Bearer <your-api-key>" \
      -H "Content-Type: application/json" \
      -d '{
        "model": "Wan-AI/Wan2.2-T2V-A14B-Diffusers",
        "input": "A campfire crackles in a forest clearing at night, with sparks spiraling into a star-filled sky.",
        "provider_options": {
          "image": {
            "height": 512,
            "width": 512,
            "steps": 28
          }
        }
      }' \
      | jq -r '.output[0].content[0].image_data' \
      | base64 -d > output-text-to-video.mp4
    ```
  </Tab>
</Tabs>

The generated video should look similar to the following:

<video src="https://mintcdn.com/modular/21dnEKVIFHhCZ_2p/inference/images/output-text-to-video.mp4?fit=max&auto=format&n=21dnEKVIFHhCZ_2p&q=85&s=c29428d5a856561521c291c53406b73a" width="512" controls muted loop data-path="inference/images/output-text-to-video.mp4" />

## Generate a video from an image

To generate a video from an image, set `input` to a user message containing:

* An `input_image` block with an image URL or base64-encoded data URI.
* An `input_text` block describing how to animate the image.

The following example reads a local image named `input.png`, encodes it as a
data URI, and saves the generated video as `output-image-to-video.mp4`.

<Tabs>
  <Tab title="Python">
    Encode the input image, send it with a prompt, and save the generated video:

    ```python title="generate-video-from-image.py" theme={null}
    import base64
    from pathlib import Path

    from openai import OpenAI

    client = OpenAI(
        base_url="https://api.modular.com/v1",
        api_key="<your-api-key>",  # Load your key
    )

    input_data = base64.b64encode(Path("input.png").read_bytes()).decode("utf-8")

    response = client.responses.create(
        model="Wan-AI/Wan2.2-I2V-A14B-Diffusers",
        input=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "input_image",
                        "image_url": f"data:image/png;base64,{input_data}",
                    },
                    {
                        "type": "input_text",
                        "text": "Animate this scene with wind moving through the grass.",
                    },
                ],
            }
        ],
        extra_body={
            "provider_options": {
                "image": {
                    "height": 480,
                    "width": 480,
                    "steps": 28,
                }
            }
        },
    )

    video_data = response.output[0].content[0].image_data
    Path("output-image-to-video.mp4").write_bytes(base64.b64decode(video_data))
    ```
  </Tab>

  <Tab title="curl">
    This example runs on Linux and requires `jq`. Encode the input image and write
    the request body to a file:

    ```bash theme={null}
    IMAGE_DATA=$(base64 -w 0 input.png)

    cat > request.json <<EOF
    {
      "model": "Wan-AI/Wan2.2-I2V-A14B-Diffusers",
      "input": [
        {
          "role": "user",
          "content": [
            {
              "type": "input_image",
              "image_url": "data:image/png;base64,$IMAGE_DATA"
            },
            {
              "type": "input_text",
              "text": "Animate this scene with wind moving through the grass."
            }
          ]
        }
      ],
      "provider_options": {
        "image": {
          "height": 480,
          "width": 480,
          "steps": 28
        }
      }
    }
    EOF
    ```

    Send the request and decode the returned video data:

    ```bash theme={null}
    curl -X POST https://api.modular.com/v1/responses \
      -H "Authorization: Bearer <your-api-key>" \
      -H "Content-Type: application/json" \
      --data-binary @request.json \
      | jq -r '.output[0].content[0].image_data' \
      | base64 -d > output-image-to-video.mp4
    ```
  </Tab>
</Tabs>
