|
9 | 9 | from typing import Annotated, Any, cast |
10 | 10 |
|
11 | 11 | import pytest |
| 12 | +from jsonschema import Draft202012Validator |
12 | 13 | from pydantic import BaseModel, Field |
13 | 14 | from typing_extensions import TypedDict |
14 | 15 |
|
@@ -1063,6 +1064,114 @@ async def noop(ctx: ToolContext[Any], input: str) -> str: |
1063 | 1064 | assert tool.params_json_schema["required"] == ["x"] |
1064 | 1065 |
|
1065 | 1066 |
|
| 1067 | +@pytest.mark.parametrize( |
| 1068 | + ("parent", "use_ref"), |
| 1069 | + [ |
| 1070 | + ({"type": "object", "properties": {}}, True), |
| 1071 | + ({"type": "object"}, True), |
| 1072 | + ({}, True), |
| 1073 | + ({"type": "object", "properties": {}}, False), |
| 1074 | + ], |
| 1075 | +) |
| 1076 | +def test_function_tool_rejects_single_all_of_that_broadens_closed_parent( |
| 1077 | + parent: dict[str, Any], use_ref: bool |
| 1078 | +) -> None: |
| 1079 | + async def noop(ctx: ToolContext[Any], input: str) -> str: |
| 1080 | + return "" |
| 1081 | + |
| 1082 | + entry = {"type": "object", "properties": {"value": {"type": "string"}}} |
| 1083 | + schema = { |
| 1084 | + **parent, |
| 1085 | + "additionalProperties": False, |
| 1086 | + "$defs": {"Entry": entry}, |
| 1087 | + "allOf": [{"$ref": "#/$defs/Entry"} if use_ref else copy.deepcopy(entry)], |
| 1088 | + } |
| 1089 | + original = copy.deepcopy(schema) |
| 1090 | + validator = Draft202012Validator(original) |
| 1091 | + assert validator.is_valid({}) |
| 1092 | + assert not validator.is_valid({"value": "example"}) |
| 1093 | + |
| 1094 | + with pytest.raises(UserError, match="singleton `allOf`.*properties"): |
| 1095 | + FunctionTool(name="t", description="d", params_json_schema=schema, on_invoke_tool=noop) |
| 1096 | + |
| 1097 | + assert schema == original |
| 1098 | + non_strict_tool = FunctionTool( |
| 1099 | + name="t", |
| 1100 | + description="d", |
| 1101 | + params_json_schema=schema, |
| 1102 | + on_invoke_tool=noop, |
| 1103 | + strict_json_schema=False, |
| 1104 | + ) |
| 1105 | + assert non_strict_tool.params_json_schema == original |
| 1106 | + assert not Draft202012Validator(non_strict_tool.params_json_schema).is_valid( |
| 1107 | + {"value": "example"} |
| 1108 | + ) |
| 1109 | + |
| 1110 | + |
| 1111 | +@pytest.mark.parametrize("parent", [{"type": "object"}, {"type": "object", "properties": {}}]) |
| 1112 | +def test_function_tool_single_all_of_wrapper_remains_strict(parent: dict[str, Any]) -> None: |
| 1113 | + async def noop(ctx: ToolContext[Any], input: str) -> str: |
| 1114 | + return "" |
| 1115 | + |
| 1116 | + schema = { |
| 1117 | + **parent, |
| 1118 | + "description": "wrapper", |
| 1119 | + "allOf": [{"type": "object", "properties": {"value": {"type": "string"}}}], |
| 1120 | + } |
| 1121 | + original = copy.deepcopy(schema) |
| 1122 | + tool = FunctionTool(name="t", description="d", params_json_schema=schema, on_invoke_tool=noop) |
| 1123 | + |
| 1124 | + assert schema == original |
| 1125 | + assert tool.params_json_schema["description"] == "wrapper" |
| 1126 | + assert tool.params_json_schema["required"] == ["value"] |
| 1127 | + validator = Draft202012Validator(tool.params_json_schema) |
| 1128 | + assert validator.is_valid({"value": "example"}) |
| 1129 | + assert not validator.is_valid({}) |
| 1130 | + assert not validator.is_valid({"value": "example", "extra": True}) |
| 1131 | + |
| 1132 | + |
| 1133 | +def test_function_tool_nested_single_all_of_ref_wrapper_remains_strict() -> None: |
| 1134 | + async def noop(ctx: ToolContext[Any], input: str) -> str: |
| 1135 | + return "" |
| 1136 | + |
| 1137 | + schema = { |
| 1138 | + "type": "object", |
| 1139 | + "components": { |
| 1140 | + "schemas": { |
| 1141 | + "Inner": {"type": "object", "properties": {"value": {"type": "string"}}}, |
| 1142 | + "Outer": {"type": "object", "allOf": [{"$ref": "#/components/schemas/Inner"}]}, |
| 1143 | + } |
| 1144 | + }, |
| 1145 | + "allOf": [{"$ref": "#/components/schemas/Outer"}], |
| 1146 | + } |
| 1147 | + original = copy.deepcopy(schema) |
| 1148 | + tool = FunctionTool(name="t", description="d", params_json_schema=schema, on_invoke_tool=noop) |
| 1149 | + |
| 1150 | + assert schema == original |
| 1151 | + assert tool.params_json_schema["required"] == ["value"] |
| 1152 | + assert "allOf" not in tool.params_json_schema |
| 1153 | + validator = Draft202012Validator(tool.params_json_schema) |
| 1154 | + assert validator.is_valid({"value": "example"}) |
| 1155 | + assert not validator.is_valid({"value": "example", "extra": True}) |
| 1156 | + |
| 1157 | + |
| 1158 | +def test_function_tool_single_all_of_closed_empty_object_remains_strict() -> None: |
| 1159 | + async def noop(ctx: ToolContext[Any], input: str) -> str: |
| 1160 | + return "" |
| 1161 | + |
| 1162 | + schema = { |
| 1163 | + "type": "object", |
| 1164 | + "properties": {}, |
| 1165 | + "additionalProperties": False, |
| 1166 | + "allOf": [{"type": "object", "properties": {}}], |
| 1167 | + } |
| 1168 | + tool = FunctionTool(name="t", description="d", params_json_schema=schema, on_invoke_tool=noop) |
| 1169 | + |
| 1170 | + validator = Draft202012Validator(tool.params_json_schema) |
| 1171 | + assert validator.is_valid({}) |
| 1172 | + assert not validator.is_valid({"value": "example"}) |
| 1173 | + |
| 1174 | + |
1066 | 1175 | def test_function_tool_rejects_deep_schema_before_copying() -> None: |
1067 | 1176 | async def noop(ctx: ToolContext[Any], input: str) -> str: |
1068 | 1177 | return "" |
|
0 commit comments