Support dictionaries in structured output
chrisheecho opened this issue · comments
client = genai.Client()
async def main():
class Foo(BaseModel):
foo: dict[str, int]
await client.aio.models.generate_content(
model="gemini-2.0-flash",
contents="Please populate all fields of the following model with random values",
config={
"response_mime_type": "application/json",
"response_schema": Foo,
}
)
if __name__ == "__main__":
asyncio.run(main())pydantic_core._pydantic_core.ValidationError: 1 validation error for Schema
properties.foo.additionalProperties
Extra inputs are not permitted [type=extra_forbidden, input_value={'type': 'integer'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.10/v/extra_forbidden
To achieve the above, I would need to refactor my model into something utterly disgusting:
client = genai.Client()
async def main():
class Bar(BaseModel):
key: str
value: int
class Foo(BaseModel):
foo: list[Bar]
return (await client.aio.models.generate_content(
model="gemini-2.0-flash",
contents="Please populate all fields of the following model with random values",
config={
"response_mime_type": "application/json",
"response_schema": Foo,
}
)).parsed
if __name__ == "__main__":
print(asyncio.run(main()))Using a straight up dict also does not work:
client = genai.Client()
async def main():
return (await client.aio.models.generate_content(
model="gemini-2.0-flash",
contents="Please populate all fields of the following model with random values",
config={
"response_mime_type": "application/json",
"response_schema": dict[str, int],
}
)).parsed
if __name__ == "__main__":
print(asyncio.run(main()))additionalProperties
Extra inputs are not permitted [type=extra_forbidden, input_value={'type': 'integer'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.10/v/extra_forbidden
Closing in favor of: #70