Skip to content

Commit e68f977

Browse files
authored
Python: Build an array schema for variadic tuple parameters (#14692)
### Motivation and Context `KernelJsonSchemaBuilder` treats `tuple[int, ...]` as a two-item tuple whose second item is the `Ellipsis`, so a function parameter typed that way gets this schema: ```json {"type": "array", "items": [{"type": "integer"}, {"type": "object"}]} ``` The model is told the argument is exactly one integer followed by an object, instead of a list of integers. ### Description When the tuple's arguments are `(X, Ellipsis)`, build `{"type": "array", "items": <schema of X>}`, the same as `list[X]`. Fixed-length tuples like `tuple[int, str]` keep their per-position `items`. Added tests for `tuple[int, ...]` and `Optional[tuple[str, ...]]`; both fail on main. ### Contribution Checklist - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
1 parent 1ae2a39 commit e68f977

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎python/semantic_kernel/schema/kernel_json_schema_builder.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,12 @@ def handle_complex_type(
203203
schema["additionalProperties"] = False
204204
return schema
205205
if origin is tuple:
206-
items = [cls.build(arg, structured_output=structured_output) for arg in args]
207-
schema = {"type": "array", "items": items}
206+
if len(args) == 2 and args[1] is Ellipsis:
207+
# tuple[X, ...] holds any number of X, like list[X]
208+
schema = {"type": "array", "items": cls.build(args[0], structured_output=structured_output)}
209+
else:
210+
items = [cls.build(arg, structured_output=structured_output) for arg in args]
211+
schema = {"type": "array", "items": items}
208212
if description:
209213
schema["description"] = description
210214
if structured_output:

‎python/tests/unit/schema/test_schema_builder.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,16 @@ def test_build_tuple():
241241
}
242242

243243

244+
def test_build_variadic_tuple():
245+
schema = KernelJsonSchemaBuilder.build(tuple[int, ...], "ids")
246+
assert schema == {"type": "array", "items": {"type": "integer"}, "description": "ids"}
247+
248+
249+
def test_build_optional_variadic_tuple():
250+
schema = KernelJsonSchemaBuilder.build(Optional[tuple[str, ...]])
251+
assert schema == {"type": ["array", "null"], "items": {"type": "string"}}
252+
253+
244254
def test_build_union():
245255
schema = KernelJsonSchemaBuilder.build(Union[int, str])
246256
assert schema == {"anyOf": [{"type": "integer"}, {"type": "string"}]}

0 commit comments

Comments
 (0)