Skip to content

Commit ef98e33

Browse files
authored
Include default values in tool arguments JSON schema (#2418)
1 parent 4e75448 commit ef98e33

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

pydantic_ai_slim/pydantic_ai/_function_schema.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,21 @@ def function_schema( # noqa: C901
154154
if p.kind == Parameter.VAR_POSITIONAL:
155155
annotation = list[annotation]
156156

157-
# FieldInfo.from_annotation expects a type, `annotation` is Any
157+
required = p.default is Parameter.empty
158+
# FieldInfo.from_annotated_attribute expects a type, `annotation` is Any
158159
annotation = cast(type[Any], annotation)
159-
field_info = FieldInfo.from_annotation(annotation)
160+
if required:
161+
field_info = FieldInfo.from_annotation(annotation)
162+
else:
163+
field_info = FieldInfo.from_annotated_attribute(annotation, p.default)
160164
if field_info.description is None:
161165
field_info.description = field_descriptions.get(field_name)
162166

163167
fields[field_name] = td_schema = gen_schema._generate_td_field_schema( # pyright: ignore[reportPrivateUsage]
164168
field_name,
165169
field_info,
166170
decorators,
167-
required=p.default is Parameter.empty,
171+
required=required,
168172
)
169173
# noinspection PyTypeChecker
170174
td_schema.setdefault('metadata', {})['is_model_like'] = is_model_like(annotation)

tests/models/test_openai.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,17 +1144,16 @@ def tool_with_tuples(x: tuple[int], y: tuple[str] = ('abc',)) -> str:
11441144
snapshot(None),
11451145
),
11461146
(
1147-
strict_compatible_tool,
1147+
tool_with_default,
11481148
None,
11491149
snapshot(
11501150
{
11511151
'additionalProperties': False,
1152-
'properties': {'x': {'type': 'integer'}},
1153-
'required': ['x'],
1152+
'properties': {'x': {'default': 1, 'type': 'integer'}},
11541153
'type': 'object',
11551154
}
11561155
),
1157-
snapshot(True),
1156+
snapshot(None),
11581157
),
11591158
(
11601159
tool_with_recursion,
@@ -1413,6 +1412,7 @@ def tool_with_tuples(x: tuple[int], y: tuple[str] = ('abc',)) -> str:
14131412
'properties': {
14141413
'x': {'maxItems': 1, 'minItems': 1, 'prefixItems': [{'type': 'integer'}], 'type': 'array'},
14151414
'y': {
1415+
'default': ['abc'],
14161416
'maxItems': 1,
14171417
'minItems': 1,
14181418
'prefixItems': [{'type': 'string'}],

tests/test_tools.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ def my_tool_plain(*, a: int = 1, b: int) -> int:
932932
'outer_typed_dict_key': None,
933933
'parameters_json_schema': {
934934
'additionalProperties': False,
935-
'properties': {'a': {'type': 'integer'}, 'b': {'type': 'integer'}},
935+
'properties': {'a': {'type': 'integer'}, 'b': {'default': 1, 'type': 'integer'}},
936936
'required': ['a'],
937937
'type': 'object',
938938
},
@@ -945,7 +945,7 @@ def my_tool_plain(*, a: int = 1, b: int) -> int:
945945
'outer_typed_dict_key': None,
946946
'parameters_json_schema': {
947947
'additionalProperties': False,
948-
'properties': {'a': {'type': 'integer'}, 'b': {'type': 'integer'}},
948+
'properties': {'a': {'default': 1, 'type': 'integer'}, 'b': {'type': 'integer'}},
949949
'required': ['b'],
950950
'type': 'object',
951951
},
@@ -1031,7 +1031,7 @@ def my_tool(x: Annotated[Union[str, None], WithJsonSchema({'type': 'string'})] =
10311031
'name': 'my_tool_1',
10321032
'outer_typed_dict_key': None,
10331033
'parameters_json_schema': {
1034-
'properties': {'x': {'type': 'string'}},
1034+
'properties': {'x': {'default': None, 'type': 'string'}},
10351035
'type': 'object',
10361036
},
10371037
'strict': None,
@@ -1042,7 +1042,7 @@ def my_tool(x: Annotated[Union[str, None], WithJsonSchema({'type': 'string'})] =
10421042
'name': 'my_tool_2',
10431043
'outer_typed_dict_key': None,
10441044
'parameters_json_schema': {
1045-
'properties': {'x': {'type': 'string', 'title': 'X title'}},
1045+
'properties': {'x': {'default': None, 'type': 'string', 'title': 'X title'}},
10461046
'type': 'object',
10471047
},
10481048
'strict': None,

0 commit comments

Comments
 (0)