I have a schema (generated via Pydantic) where there are many fields which are not required.
Gemini always returns them with {“field”: null} though, instead of saving output tokens and just returning required fields as well as fields that were present in the query.
For example, we might use this as the response_schema:
class Test(BaseModel):
a: int | None = None
b: int | None = None
The output will be {“a”: null, “b”: null} instead of nothing/{}.
Is there a way to have this save output tokens and not provide fields which aren’t required instead?
Thank you for reaching out!
Optional fields show up as null because the current schema permits them to be included even when they are empty. If you enable the option to omit fields with a value of None, it may work like the model simply skipping those fields, so it will no longer return field: null, which also helps reduce token usage.
I found the solution:
class Test(BaseModel):
a: int = Field(None)
b: int = Field(None)
This will work - I don’t really like it because the instantiation contradicts the type, but with the resulting schema (required isn’t set for fields with default, but null type isn’t added due to | None / Optional) the model can omit fields.
Edit: Actually this doesn’t necessarily work either, although it did work with the simple test logic. Right now I do have to add the following to the system prompt in my more complex case:
”OMIT fields which aren’t provided!” then it will leave them out.
I’d be happy to hear about a less hacky way to achieve this.