Developer, hello! I found in the structured output documentation that we can use responseSchema
to structure the output as JSON. However, when I tried to modify the following code:
class Recipe(BaseModel):
recipe_name: str
ingredients: list[str]
to this:
class Recipe(BaseModel):
recipe_name: str
ingredients: Dict[str, str]
I encountered an error. Is there a way to output a list where each element is a dictionary?
Hi One_people,
Defining such a structure in the responseSchema
can be tricky due to limitations in the Gemini SDK’s support for certain data types. For instance, using dict
directly in Pydantic models may lead to validation errors like ValueError: Unknown field for Schema: additionalProperties
. This occurs because Gemini’s schema validation doesn’t fully support dynamic keys in dictionaries.
To work around this, you can define a list of objects with explicit properties. Here’s how you can structure your schema:
from pydantic import BaseModel
from typing import List
class Ingredient(BaseModel):
name: str
amount: str
class Recipe(BaseModel):
recipe_name: str
ingredients: List[Ingredient]
Hope this helps 