googleapis / googleapis/python-genai

Type hint and mypy on `ContentListUnion` and `ContentListUnionDict`

Open
#1,874 1 comment 0 reactions 1 assignee Assigned to @Venkaiahbabuneelam View on GitHub
priority: p2 type: bug
Dominant language
Python
Stars
4k
Forks
1k
Avg merge
2d 11h
Merged PRs (30d)
40

Description

Mypy complains about the type hint of `types.ContentListUnion` and `types.ContentListUnionDict`. I've managed to get around it with the example in #1873.

#### Environment details

- Programming language: python
- OS: macOS Sequoia 15.5
- Language runtime version: 3.12.11
- Package version: 1.55.0

#### Steps to reproduce

1. pip install mypy
2. mypy script.py

```python
import io
import os
from pathlib import Path

from dotenv import load_dotenv
from google import genai
from google.genai import types
from PIL import Image

load_dotenv()

client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
model = "gemini-2.5-flash-image"

output_path = Path("local_dev") / "image.png"
uploaded_file = client.files.upload(file=output_path, config=types.UploadFileConfig(mime_type="image/png"))
print(f"Uploaded file: {uploaded_file.name}")

prompt = "Add people to the scene"
response = client.models.generate_content(model=model, contents=[uploaded_file, prompt])
# ^^^^^^^^^^^^^^^^^^^^^^^^

Argument "contents" to "generate_content" of "Models" has incompatible type "list[object]"; expected "Content | ContentDict | str | Image | File | FileDict | Part | PartDict | list[str | Image | File | FileDict | Part | PartDict] | list[Content | ContentDict | str | Image | File | FileDict | Part | PartDict | list[str | Image | File | FileDict | Part | PartDict]]"Mypy[arg-type](https://mypy.readthedocs.io/en/latest/_refs.html#code-arg-type)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.