fix(moonshot): handle union type arrays in tool schemas

This commit is contained in:
Tranquil-Flow 2026-06-13 05:09:20 -07:00 committed by Teknium
parent 39a35b784f
commit 5acd185f7c
2 changed files with 57 additions and 1 deletions

View file

@ -135,7 +135,14 @@ def _repair_schema(node: Any, is_schema: bool = True) -> Any:
def _fill_missing_type(node: Dict[str, Any]) -> Dict[str, Any]:
"""Infer a reasonable ``type`` if this schema node has none."""
if "type" in node and node["type"] not in {None, ""}:
node_type = node.get("type")
if isinstance(node_type, list):
concrete = next(
(t for t in node_type if isinstance(t, str) and t not in {"", "null"}),
"string",
)
return {**node, "type": concrete}
if "type" in node and node_type not in {None, ""}:
return node
# Heuristic: presence of ``properties`` → object, ``items`` → array, ``enum``

View file

@ -397,3 +397,52 @@ class TestEnumNullStripping:
assert db_type["type"] == "string"
assert db_type["enum"] == ["mysql", "postgresql"], \
"null/empty enum values must be stripped after anyOf collapse"
class TestUnionTypeList:
"""Moonshot sanitizer accepts JSON Schema union type arrays."""
def test_union_type_list_normalizes_to_first_concrete_type(self):
params = {
"type": "object",
"properties": {
"limit": {
"type": ["number", "string"],
"description": "Max results",
},
},
}
out = sanitize_moonshot_tool_parameters(params)
assert out["properties"]["limit"]["type"] == "number"
def test_union_type_list_skips_null_type(self):
params = {
"type": "object",
"properties": {
"name": {"type": ["null", "string"]},
},
}
out = sanitize_moonshot_tool_parameters(params)
assert out["properties"]["name"]["type"] == "string"
def test_union_type_list_with_enum_does_not_crash_or_mutate_input(self):
params = {
"type": "object",
"properties": {
"sort": {
"type": ["string", "null"],
"enum": ["asc", "desc", None, ""],
},
},
}
out = sanitize_moonshot_tool_parameters(params)
sort = out["properties"]["sort"]
assert sort["type"] == "string"
assert sort["enum"] == ["asc", "desc"]
assert params["properties"]["sort"]["type"] == ["string", "null"]