-
Notifications
You must be signed in to change notification settings - Fork 0
Structured Output
You can ask Gemini for structured output, meaning that you can make it reliably produce JSON output. To do this you specify a schema. The easiest way to do this is using a Pydantic BaseModel, as demonstrated in the demo.
Below are some notes about structured output which were worth writing down.
When we make a pydantic BaseModel we might have something like this:
class Animal:
n_legs: int
n_eyes: int
danger_level: int...which we could give to Gemini.
In the prompt we could describe each of those fields to make sure you generate the right thing.
We can also do this in the schema like this:
class Animal:
n_legs: int = Field(description="The number of legs the animal typically has")
n_eyes: int = Field(description="The number of eyes the animal typically has")
danger_level: int = Field(description="A rating between 1 and 10 of how dangerous this animal is to humans.")This couples the fields and definitions of those fields, which is nice.
Now comes the interesting question. Is this actually something we can/should do?
Google's documentation says that you can give JSON schemas to Gemini, and "description" is one of the supported fields in these schemas. It also says that in python it's easier to use pydantic data models rather than using JSON schemas.
So far so good. But then you read things like:
Passing JSON Schema directly is not yet supported when using the SDK
and
It only works with Gemini 2.5.
And you start questioning everything. Because I am pretty sure I have had this working...
- with the SDK
- with Gemini 2.0.
So I'm pretty sure it works and is fine, but Google gives no examples to my knowledge of this feature being used like we're using it, and the documentation implies (although I think it might be talking about something slightly different) that it isn't supported.