Skip to content

Conversation

dmontagu
Copy link
Contributor

@dmontagu dmontagu commented Jan 3, 2025

Closes #603

I confirmed it works and can return None by using the following script, slightly modified from the one in the issue above:

import os
from pydantic import BaseModel
from pydantic_ai import Agent
from pydantic_ai.models.gemini import GeminiModel

os.environ['GEMINI_API_KEY'] = '<scrubbed>'

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

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

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

@samuelcolvin let me know if you want any testing beyond the one I updated here.

Copy link

sonarqubecloud bot commented Jan 3, 2025

def _simplify(self, schema: dict[str, Any], refs_stack: tuple[str, ...]) -> None:
schema.pop('title', None)
default = schema.pop('default', _utils.UNSET)
schema.pop('default', None)
Copy link
Contributor Author

@dmontagu dmontagu Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why we are currently requiring the default to be None in order to remove the the {'type': 'null'} from the schema, that condition doesn't seem to me to be necessary, so I removed it here. (And we don't use the value of the default at all, so I removed the variable that was holding its value, and changed the .pop call to fall back to None to be consistent with the previous line which is also unused.)

@samuelcolvin samuelcolvin merged commit c2ef7c5 into main Jan 6, 2025
15 checks passed
@samuelcolvin samuelcolvin deleted the dmontagu/improve-gemini-null-handling branch January 6, 2025 16:48
@samuelcolvin
Copy link
Member

thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PydanticAI doesn't return None but raw VertexAI code does
2 participants