GenerateContentConfig cannot create response schemas from nested Pydantic BaseModel classes #60
Labels
priority: p0
Highest priority. Critical issue. P0 implies highest priority.
type: feature request
‘Nice-to-have’ improvement, new feature or different behavior or design.
Environment details
Steps to reproduce
Set your project id in the example below, and run.
Actual vs expected behavior:
Description
Passing a Pydantic BaseModel class with other classes nested inside, in the response_schema argument to GenerateContentConfig(), fails. However, it can easily be made to work.
I tested this with genai v0.3.0
The root cause is that the function t_schema() in google.genai._transformers.py does not properly handle the output of BaseModel.model_json_schema() when there are nested classes. See here:
python-genai/google/genai/_transformers.py
Line 285 in 5586f3d
In this case, BaseModel.model_json_schema() places definitions of inner classes in a "$defs" section of the schema by default. They are then pointed to with "$ref" as needed elsewhere. (This behavior can be customized. See pydantic.json_schema)
A working solution is to replace these references with the actual definitions:
Example
This example demonstrates the problem and the workaround. Set the project id before trying it.
The text was updated successfully, but these errors were encountered: