Skip to content

Commit eb30919

Browse files
committed
Fix function_to_jsonschema()
1 parent 20dbb08 commit eb30919

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

coagent/agents/aswarm/util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def greet(
174174
# The default value is already a Field.
175175
typ = annotation
176176
field = default
177-
elif default != inspect.Parameter.empty:
177+
elif default is not inspect.Parameter.empty:
178178
# Normal default value
179179
if typing.get_origin(annotation) == typing.Annotated:
180180
# The parameter is annotated with metadata.
@@ -213,7 +213,7 @@ def greet(
213213

214214
def get_type_and_field_from_annotated(
215215
annotation: typing.Any,
216-
default: typing.Any | None = None,
216+
default: typing.Any = inspect.Parameter.empty,
217217
) -> tuple[typing.Type, Field]:
218218
# Any additional metadata except the first one is ignored.
219219
typ, metadata = typing.get_args(annotation)[:2]
@@ -223,7 +223,7 @@ def get_type_and_field_from_annotated(
223223
case _:
224224
# Always treat the metadata as a string.
225225
description = str(metadata)
226-
if default is None:
226+
if default is inspect.Parameter.empty:
227227
field = Field(..., description=description)
228228
else:
229229
field = Field(default=default, description=description)

tests/agents/test_util.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async def test_chat_stream(mock_model_client):
3232

3333

3434
def test_function_to_jsonschema_no_description():
35-
def func(a: int, b: str = "ok") -> None:
35+
def func(a: int, b: str = "ok", c: float = None) -> None:
3636
"""This is a test function."""
3737
pass
3838

@@ -45,6 +45,7 @@ def func(a: int, b: str = "ok") -> None:
4545
"properties": {
4646
"a": {"title": "A", "type": "integer"},
4747
"b": {"default": "ok", "title": "B", "type": "string"},
48+
"c": {"default": None, "title": "C", "type": "number"},
4849
},
4950
"required": ["a"],
5051
"title": "func",
@@ -59,6 +60,7 @@ def test_function_to_jsonschema_annotated_with_string():
5960
def func(
6061
a: Annotated[int, "The description for parameter a"],
6162
b: Annotated[str, "The description for parameter b"] = "ok",
63+
c: Annotated[float, "The description for parameter c"] = None,
6264
) -> None:
6365
"""This is a test function."""
6466
pass
@@ -81,6 +83,12 @@ def func(
8183
"title": "B",
8284
"type": "string",
8385
},
86+
"c": {
87+
"default": None,
88+
"description": "The description for parameter c",
89+
"title": "C",
90+
"type": "number",
91+
},
8492
},
8593
"required": ["a"],
8694
"title": "func",
@@ -95,6 +103,9 @@ def test_function_to_jsonschema_annotated_with_pydantic_field():
95103
def func(
96104
a: Annotated[int, Field(description="The description for parameter a")],
97105
b: Annotated[str, Field(description="The description for parameter b")] = "ok",
106+
c: Annotated[
107+
float, Field(description="The description for parameter c")
108+
] = None,
98109
) -> None:
99110
"""This is a test function."""
100111
pass
@@ -117,6 +128,12 @@ def func(
117128
"title": "B",
118129
"type": "string",
119130
},
131+
"c": {
132+
"default": None,
133+
"description": "The description for parameter c",
134+
"title": "C",
135+
"type": "number",
136+
},
120137
},
121138
"required": ["a"],
122139
"title": "func",

0 commit comments

Comments
 (0)