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
10 changes: 7 additions & 3 deletions pydantic_ai_slim/pydantic_ai/_function_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,21 @@ def function_schema( # noqa: C901
if p.kind == Parameter.VAR_POSITIONAL:
annotation = list[annotation]

# FieldInfo.from_annotation expects a type, `annotation` is Any
required = p.default is Parameter.empty
# FieldInfo.from_annotated_attribute expects a type, `annotation` is Any
annotation = cast(type[Any], annotation)
field_info = FieldInfo.from_annotation(annotation)
if required:
field_info = FieldInfo.from_annotation(annotation)
else:
field_info = FieldInfo.from_annotated_attribute(annotation, p.default)
if field_info.description is None:
field_info.description = field_descriptions.get(field_name)

fields[field_name] = td_schema = gen_schema._generate_td_field_schema( # pyright: ignore[reportPrivateUsage]
field_name,
field_info,
decorators,
required=p.default is Parameter.empty,
required=required,
)
# noinspection PyTypeChecker
td_schema.setdefault('metadata', {})['is_model_like'] = is_model_like(annotation)
Expand Down
8 changes: 4 additions & 4 deletions tests/models/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -1144,17 +1144,16 @@ def tool_with_tuples(x: tuple[int], y: tuple[str] = ('abc',)) -> str:
snapshot(None),
),
(
strict_compatible_tool,
tool_with_default,
None,
snapshot(
{
'additionalProperties': False,
'properties': {'x': {'type': 'integer'}},
'required': ['x'],
'properties': {'x': {'default': 1, 'type': 'integer'}},
'type': 'object',
}
),
snapshot(True),
snapshot(None),
),
(
tool_with_recursion,
Expand Down Expand Up @@ -1413,6 +1412,7 @@ def tool_with_tuples(x: tuple[int], y: tuple[str] = ('abc',)) -> str:
'properties': {
'x': {'maxItems': 1, 'minItems': 1, 'prefixItems': [{'type': 'integer'}], 'type': 'array'},
'y': {
'default': ['abc'],
'maxItems': 1,
'minItems': 1,
'prefixItems': [{'type': 'string'}],
Expand Down
8 changes: 4 additions & 4 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ def my_tool_plain(*, a: int = 1, b: int) -> int:
'outer_typed_dict_key': None,
'parameters_json_schema': {
'additionalProperties': False,
'properties': {'a': {'type': 'integer'}, 'b': {'type': 'integer'}},
'properties': {'a': {'type': 'integer'}, 'b': {'default': 1, 'type': 'integer'}},
'required': ['a'],
'type': 'object',
},
Expand All @@ -945,7 +945,7 @@ def my_tool_plain(*, a: int = 1, b: int) -> int:
'outer_typed_dict_key': None,
'parameters_json_schema': {
'additionalProperties': False,
'properties': {'a': {'type': 'integer'}, 'b': {'type': 'integer'}},
'properties': {'a': {'default': 1, 'type': 'integer'}, 'b': {'type': 'integer'}},
'required': ['b'],
'type': 'object',
},
Expand Down Expand Up @@ -1031,7 +1031,7 @@ def my_tool(x: Annotated[Union[str, None], WithJsonSchema({'type': 'string'})] =
'name': 'my_tool_1',
'outer_typed_dict_key': None,
'parameters_json_schema': {
'properties': {'x': {'type': 'string'}},
'properties': {'x': {'default': None, 'type': 'string'}},
'type': 'object',
},
'strict': None,
Expand All @@ -1042,7 +1042,7 @@ def my_tool(x: Annotated[Union[str, None], WithJsonSchema({'type': 'string'})] =
'name': 'my_tool_2',
'outer_typed_dict_key': None,
'parameters_json_schema': {
'properties': {'x': {'type': 'string', 'title': 'X title'}},
'properties': {'x': {'default': None, 'type': 'string', 'title': 'X title'}},
'type': 'object',
},
'strict': None,
Expand Down