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
5 changes: 4 additions & 1 deletion src/agents/function_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ def generate_func_documentation(
)

param_descriptions: dict[str, str] = {
param.name: param.description
# Google and NumPy style docstrings write variadic parameters with their
# stars ("*args:", "**kwargs:") and griffe returns those names verbatim.
# Strip the stars so lookups by the signature parameter name succeed.
param.name.lstrip("*"): param.description
for section in parsed
if section.kind == DocstringSectionKind.parameters
for param in section.value
Expand Down
56 changes: 56 additions & 0 deletions tests/test_function_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,3 +1008,59 @@ def test_google_docstring_after_section_body_matches_blank_line_form():

assert fixed.description == control.description
assert fixed.params_json_schema["properties"] == control.params_json_schema["properties"]


def starred_args_google_function(x: int, *numbers: float, **kwargs: str) -> str:
"""Add numbers to a base.

Args:
x: The base value.
*numbers: The numbers to add.
**kwargs: Extra options.
"""
return f"{x} {numbers} {kwargs}"


def starred_args_numpy_function(x: int, *numbers: float, **kwargs: str) -> str:
"""Add numbers to a base.

Parameters
----------
x : int
The base value.
*numbers : float
The numbers to add.
**kwargs : str
Extra options.
"""
return f"{x} {numbers} {kwargs}"


def starred_args_sphinx_function(x: int, *numbers: float, **kwargs: str) -> str:
"""Add numbers to a base.

:param x: The base value.
:param numbers: The numbers to add.
:param kwargs: Extra options.
"""
return f"{x} {numbers} {kwargs}"


@pytest.mark.parametrize(
"func,style",
[
(starred_args_google_function, "google"),
(starred_args_numpy_function, "numpy"),
(starred_args_sphinx_function, "sphinx"),
],
)
def test_variadic_param_descriptions_preserved(func, style):
"""Google and NumPy style docstrings write variadic parameters with their stars
("*numbers:", "**kwargs:"). The parsed descriptions must still attach to the bare
signature names in the JSON schema, matching the sphinx form."""
fs = function_schema(func, docstring_style=style, strict_json_schema=False)

properties = fs.params_json_schema.get("properties", {})
assert properties["x"]["description"] == "The base value."
assert properties["numbers"]["description"] == "The numbers to add."
assert properties["kwargs"]["description"] == "Extra options."