Skip to content
Open
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
18 changes: 13 additions & 5 deletions google/genai/_automatic_function_calling_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,21 @@ def _parse_schema_from_parameter( # type: ignore[return]
schema.type = _py_builtin_type_to_schema_type[dict]
schema.properties = {}
for field_name, field_info in param.annotation.model_fields.items():
field_param = inspect.Parameter(
field_name,
inspect.Parameter.POSITIONAL_OR_KEYWORD,
annotation=field_info.annotation,
)
# Forward the field's default so fields with a default are not marked
# required. Without this the sub-schema default stays unset and every
# non-Optional field is treated as required.
if not field_info.is_required():
field_param = field_param.replace(
default=field_info.get_default(call_default_factory=True)
)
schema.properties[field_name] = _parse_schema_from_parameter(
api_option,
inspect.Parameter(
field_name,
inspect.Parameter.POSITIONAL_OR_KEYWORD,
annotation=field_info.annotation,
),
field_param,
func_name,
)
schema.required = _get_required_fields(schema)
Expand Down
49 changes: 49 additions & 0 deletions google/genai/tests/types/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,55 @@ def func_under_test(
assert actual_schema_vertex == expected_schema


def test_pydantic_model_with_default_fields():
class MyPydanticModel(pydantic.BaseModel):
a: int
b: int = 5
c: str = 'hello'
d: list[int] = pydantic.Field(default_factory=list)

def func_under_test(
a: MyPydanticModel,
):
"""test pydantic model with default fields."""
pass

expected_schema = types.FunctionDeclaration(
name='func_under_test',
parameters=types.Schema(
type='OBJECT',
properties={
'a': types.Schema(
type='OBJECT',
properties={
'a': types.Schema(type='INTEGER'),
'b': types.Schema(type='INTEGER', default=5),
'c': types.Schema(type='STRING', default='hello'),
'd': types.Schema(
type='ARRAY',
default=[],
items=types.Schema(type='INTEGER'),
),
},
required=['a'],
),
},
required=['a'],
),
description='test pydantic model with default fields.',
)

actual_schema_mldev = types.FunctionDeclaration.from_callable(
client=mldev_client, callable=func_under_test
)
actual_schema_vertex = types.FunctionDeclaration.from_callable(
client=vertex_client, callable=func_under_test
)

assert actual_schema_mldev == expected_schema
assert actual_schema_vertex == expected_schema


@pytest.mark.skip(
reason=(
'AFC is in progress of refactoring, this test is failing python 3.14'
Expand Down