Skip to content

PydanticAI doesn't return None but raw VertexAI code does #603

@DataMonsterBoy

Description

@DataMonsterBoy

So just using VertexAI libraries I can have the option to get back null values. However, when writing the equivalent code using pydanticAI I never get back null values:

Using vertex libraries:

from vertexai.generative_models import GenerativeModel, GenerationConfig

response_schema = {
    "type": "object",
    "properties": {
        "age": {
            "type": "INTEGER", "nullable": True
        }
    }
}

generation_config = GenerationConfig(
    response_mime_type="application/json",
    response_schema=response_schema
)

model = GenerativeModel("gemini-1.5-flash")
result = model.generate_content(
    "The man was very old. what is the age of the old man?",
        generation_config=generation_config
    )
print(result.candidates[0].content.parts[0].text)
# "{\"age\": null}"

Using pydanticAI:

from pydantic_ai import Agent
from pydantic import BaseModel
from pydantic_ai.models.vertexai import VertexAIModel

class AgeModel(BaseModel):
    age: int | None = None

gemini_model = VertexAIModel('gemini-1.5-flash')

prompt = "The man was very old. what is the age of the old man?"
agent = Agent(gemini_model, result_type=AgeModel)
age = agent.run_sync(prompt)
print(age.data)
# age=100

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions