Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use structured output with lists in the new SDK #52

Open
osanseviero opened this issue Dec 27, 2024 · 2 comments
Open

Use structured output with lists in the new SDK #52

osanseviero opened this issue Dec 27, 2024 · 2 comments
Assignees
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue.

Comments

@osanseviero
Copy link

osanseviero commented Dec 27, 2024

In the old SDK, we could do

import google.generativeai as genai
import typing_extensions as typing

class Recipe(typing.TypedDict):
    recipe_name: str
    ingredients: list[str]

model = genai.GenerativeModel("gemini-1.5-pro-latest")
result = model.generate_content(
    "List a few popular cookie recipes.",
    generation_config=genai.GenerationConfig(
        response_mime_type="application/json", response_schema=list[Recipe]
    ),
)
print(result)

With the new API, doing something similar

from google import genai
from pydantic import BaseModel

class Recipe(BaseModel):
    recipe_name: str
    ingredients: list[str]

result = client.models.generate_content(
    model="gemini-2.0-flash-exp",
    contents="List a few popular cookie recipes.",
    config=genai.types.GenerateContentConfig(
        response_mime_type="application/json", response_schema=list[Recipe]
    ),
)
print(result)

I would get

AttributeError: type object 'list' has no attribute 'model_json_schema'

If I instead try to wrap with another model

class RecipeList(BaseModel):
    recipes: list[Recipe]

...

result = client.models.generate_content(
    model=MODEL_ID,
    contents="List a few popular cookie recipes.",
    config=genai.types.GenerateContentConfig(
        response_mime_type="application/json", response_schema=RecipeList
    ),
)

I get an error as well, but this time in validation stage, which is unfortunately not very clear (but it does hint the model did return something)

ValidationError: 2 validation errors for Schema
properties.recipes.items.$ref
  Extra inputs are not permitted [type=extra_forbidden, input_value='#/$defs/Recipe', input_type=str]
    For further information visit https://errors.pydantic.dev/2.10/v/extra_forbidden
$defs
  Extra inputs are not permitted [type=extra_forbidden, input_value={'Recipe': {'properties':...ts'], 'type': 'OBJECT'}}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.10/v/extra_forbidden
@osanseviero osanseviero added priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue. labels Dec 27, 2024
@osanseviero osanseviero changed the title Use the SDK with Use structured output with lists in the new SDK Dec 27, 2024
@tomy2502
Copy link

from typing import List

class Output_from_AIStudio(BaseModel):
keywords: List[str]

@osanseviero
Copy link
Author

Related: #60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

3 participants