Pydantic actually works with Gemini 2.0 (response_schema). Please check the code below which works for me. The error message is not coming from Gemini 2.0.
import os
from google import genai
from google.genai.types import GenerateContentConfig
from PIL import Image
from pydantic import BaseModel
os.environ["API_KEY"] = <YOUR_API_KEY>
client = genai.Client(api_key=os.environ["API_KEY"])
image = Image.open(SOME_IMAGE_PATH)
prompt = "Detect objects box 2d."
class Object(BaseModel):
label: str
box_2d: list[int]
response = client.models.generate_content(
model="gemini-2.0-flash-exp",
contents=[image, prompt],
config=GenerateContentConfig(
response_mime_type="application/json",
response_schema=Object,
),
)
print(response.text)