Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pydantic_ai_slim/pydantic_ai/models/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,12 @@ def _simplify(self, schema: dict[str, Any], refs_stack: tuple[str, ...]) -> None
self._object(schema, refs_stack)
elif type_ == 'array':
return self._array(schema, refs_stack)
elif type_ == 'string' and (fmt := schema.pop('format', None)):
description = schema.get('description')
if description:
schema['description'] = f'{description} (format: {fmt})'
else:
schema['description'] = f'Format: {fmt}'

def _object(self, schema: dict[str, Any], refs_stack: tuple[str, ...]) -> None:
ad_props = schema.pop('additionalProperties', None)
Expand Down
54 changes: 53 additions & 1 deletion tests/models/test_gemini.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pyright: reportPrivateUsage=false
from __future__ import annotations as _annotations

import datetime
import json
from collections.abc import AsyncIterator, Callable, Sequence
from dataclasses import dataclass
Expand All @@ -9,7 +10,7 @@
import httpx
import pytest
from inline_snapshot import snapshot
from pydantic import BaseModel
from pydantic import BaseModel, Field
from typing_extensions import Literal, TypeAlias

from pydantic_ai import Agent, ModelRetry, UnexpectedModelBehavior, UserError
Expand Down Expand Up @@ -317,6 +318,57 @@ class Location(BaseModel):
await m.agent_model(function_tools=[], allow_text_result=True, result_tools=[result_tool])


async def test_json_def_date(allow_model_requests: None):
class FormattedStringFields(BaseModel):
d: datetime.date
dt: datetime.datetime
t: datetime.time = Field(description='')
td: datetime.timedelta = Field(description='my timedelta')

json_schema = FormattedStringFields.model_json_schema()
assert json_schema == snapshot(
{
'properties': {
'd': {'format': 'date', 'title': 'D', 'type': 'string'},
'dt': {'format': 'date-time', 'title': 'Dt', 'type': 'string'},
't': {'format': 'time', 'title': 'T', 'type': 'string', 'description': ''},
'td': {'format': 'duration', 'title': 'Td', 'type': 'string', 'description': 'my timedelta'},
},
'required': ['d', 'dt', 't', 'td'],
'title': 'FormattedStringFields',
'type': 'object',
}
)

m = GeminiModel('gemini-1.5-flash', api_key='via-arg')
result_tool = ToolDefinition(
'result',
'This is the tool for the final Result',
json_schema,
)
agent_model = await m.agent_model(function_tools=[], allow_text_result=True, result_tools=[result_tool])
assert agent_model.tools == snapshot(
_GeminiTools(
function_declarations=[
_GeminiFunction(
description='This is the tool for the final ' 'Result',
name='result',
parameters={
'properties': {
'd': {'description': 'Format: date', 'type': 'string'},
'dt': {'description': 'Format: date-time', 'type': 'string'},
't': {'description': 'Format: time', 'type': 'string'},
'td': {'description': 'my timedelta (format: duration)', 'type': 'string'},
},
'required': ['d', 'dt', 't', 'td'],
'type': 'object',
},
)
]
)
)


@dataclass
class AsyncByteStreamList(httpx.AsyncByteStream):
data: list[bytes]
Expand Down
Loading