Skip to content

Commit e773b15

Browse files
fix(core): preserve tuple annotations for variadic tool arguments (#4655)
1 parent 3603dc9 commit e773b15

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

‎src/agents/function_schema.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,10 @@ def function_schema(
399399
if param.kind == param.VAR_POSITIONAL:
400400
# e.g. *args: extend positional args
401401
if get_origin(ann) is tuple:
402-
# e.g. def foo(*args: tuple[int, ...]) -> treat as List[int]
402+
# Preserve a homogeneous tuple as the type of each positional argument.
403403
args_of_tuple = get_args(ann)
404404
if len(args_of_tuple) == 2 and args_of_tuple[1] is Ellipsis:
405-
ann = list[args_of_tuple[0]] # type: ignore
405+
ann = list[ann] # type: ignore
406406
else:
407407
ann = list[Any]
408408
else:

‎tests/test_function_schema.py‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,7 @@ def func(a: int, context: RunContextWrapper) -> None:
439439

440440

441441
def test_var_positional_tuple_annotation():
442-
# When a function has a var-positional parameter annotated with a tuple type,
443-
# function_schema() should convert it into a field with type List[<tuple-element>].
442+
# A variadic tuple annotation applies to each positional argument.
444443
def func(*args: tuple[int, ...]) -> int:
445444
total = 0
446445
for arg in args:
@@ -450,8 +449,17 @@ def func(*args: tuple[int, ...]) -> int:
450449
fs = function_schema(func, use_docstring_info=False)
451450

452451
properties = fs.params_json_schema.get("properties", {})
453-
assert properties.get("args").get("type") == "array"
454-
assert properties.get("args").get("items").get("type") == "integer"
452+
args_schema = properties.get("args", {})
453+
assert args_schema.get("type") == "array"
454+
assert args_schema.get("items", {}).get("type") == "array"
455+
assert args_schema.get("items", {}).get("items", {}).get("type") == "integer"
456+
457+
parsed = fs.params_pydantic_model.model_validate({"args": [[1, 2], [3]]})
458+
args, kwargs = fs.to_call_args(parsed)
459+
assert func(*args, **kwargs) == 6
460+
461+
with pytest.raises(ValidationError):
462+
fs.params_pydantic_model.model_validate({"args": [1, 2, 3]})
455463

456464

457465
def test_var_keyword_dict_annotation():

0 commit comments

Comments
 (0)