Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
py 990 lines 36.8 KB
Raw
1 """Tests for composio.utils.strict_schema and its use by OpenAIResponsesProvider."""
2
3 import copy
4 import json
5
6 import pytest
7
8 from composio.utils.strict_schema import omit_null_tool_arguments, to_strict_json_schema
9
10
11 def assert_strict_shape(node, path=""):
12 """Structural invariants OpenAI enforces on every node of a strict schema."""
13 if isinstance(node, list):
14 for index, item in enumerate(node):
15 assert_strict_shape(item, f"{path}[{index}]")
16 return
17 if not isinstance(node, dict):
18 return
19 if "anyOf" in node:
20 assert "type" not in node, f"{path}: type beside anyOf"
21 for keyword in ("default", "examples", "oneOf", "patternProperties"):
22 assert keyword not in node, f"{path}: {keyword}"
23 node_type = node.get("type")
24 is_object = node_type == "object" or (
25 isinstance(node_type, list) and "object" in node_type
26 )
27 if is_object or "properties" in node:
28 properties = node.get("properties") or {}
29 assert node.get("required") == list(properties.keys()), f"{path}: required"
30 assert node.get("additionalProperties") is False, (
31 f"{path}: additionalProperties"
32 )
33 for key, child in node.items():
34 if key in ("enum", "const"):
35 continue
36 assert_strict_shape(child, f"{path}.{key}" if path else key)
37
38
39 class TestToStrictJsonSchema:
40 def test_keeps_flat_all_required_object_valid(self):
41 schema = {
42 "type": "object",
43 "properties": {"query": {"type": "string"}},
44 "required": ["query"],
45 "additionalProperties": False,
46 }
47
48 result = to_strict_json_schema(schema)
49
50 assert result.schema == schema
51 assert result.changes == []
52
53 def test_keeps_optional_properties_required_and_nullable(self):
54 result = to_strict_json_schema(
55 {
56 "type": "object",
57 "properties": {
58 "cfg": {
59 "type": "object",
60 "properties": {
61 "url": {"type": "string"},
62 "note": {"type": "string", "description": "optional note"},
63 },
64 "required": ["url"],
65 }
66 },
67 "required": ["cfg"],
68 }
69 )
70
71 assert result.schema == {
72 "type": "object",
73 "properties": {
74 "cfg": {
75 "type": "object",
76 "properties": {
77 "url": {"type": "string"},
78 "note": {
79 "type": ["string", "null"],
80 "description": "optional note",
81 },
82 },
83 "required": ["url", "note"],
84 "additionalProperties": False,
85 }
86 },
87 "required": ["cfg"],
88 "additionalProperties": False,
89 }
90 assert [(c.path, c.reason) for c in result.changes] == [
91 ("properties.cfg.properties.note", "optional-property-nullable")
92 ]
93
94 def test_requires_and_widens_every_property_when_required_is_missing(self):
95 result = to_strict_json_schema(
96 {
97 "type": "object",
98 "properties": {"a": {"type": "string"}, "b": {"type": "number"}},
99 }
100 )
101
102 assert result.schema == {
103 "type": "object",
104 "properties": {
105 "a": {"type": ["string", "null"]},
106 "b": {"type": ["number", "null"]},
107 },
108 "required": ["a", "b"],
109 "additionalProperties": False,
110 }
111
112 def test_keeps_nullable_type_arrays_and_closes_nullable_objects(self):
113 result = to_strict_json_schema(
114 {
115 "type": "object",
116 "properties": {
117 "id": {"type": ["string", "null"], "description": "identifier"},
118 "cfg": {
119 "type": ["object", "null"],
120 "properties": {"a": {"type": "string"}},
121 "required": ["a"],
122 },
123 "xs": {"type": ["array", "null"], "items": {"type": "string"}},
124 },
125 "required": ["id", "cfg", "xs"],
126 }
127 )
128
129 assert result.schema["properties"]["cfg"] == {
130 "type": ["object", "null"],
131 "properties": {"a": {"type": "string"}},
132 "required": ["a"],
133 "additionalProperties": False,
134 }
135 assert result.schema["properties"]["id"] == {
136 "type": ["string", "null"],
137 "description": "identifier",
138 }
139 assert result.changes == []
140 assert_strict_shape(result.schema)
141
142 def test_widens_composition_and_enum_properties_without_type_beside_any_of(self):
143 result = to_strict_json_schema(
144 {
145 "type": "object",
146 "properties": {
147 "value": {"anyOf": [{"type": "string"}, {"type": "number"}]},
148 "already": {"anyOf": [{"type": "string"}, {"type": "null"}]},
149 "choice": {"enum": ["a", "b"], "description": "pick one"},
150 "multi": {"type": ["string", "number"]},
151 },
152 }
153 )
154
155 properties = result.schema["properties"]
156 assert properties["value"] == {
157 "anyOf": [{"type": "string"}, {"type": "number"}, {"type": "null"}]
158 }
159 assert properties["already"] == {
160 "anyOf": [{"type": "string"}, {"type": "null"}]
161 }
162 assert properties["choice"] == {
163 "description": "pick one",
164 "anyOf": [{"enum": ["a", "b"]}, {"type": "null"}],
165 }
166 assert properties["multi"] == {"type": ["string", "number", "null"]}
167 assert_strict_shape(result.schema)
168
169 def test_normalizes_composition_branches_and_array_items_recursively(self):
170 result = to_strict_json_schema(
171 {
172 "type": "object",
173 "properties": {
174 "payload": {
175 "anyOf": [
176 {
177 "type": "object",
178 "properties": {
179 "inner": {"type": "string"},
180 "extra": {"type": "string"},
181 },
182 "required": ["inner"],
183 },
184 {"type": "null"},
185 ]
186 },
187 "rows": {
188 "type": "array",
189 "items": {
190 "type": "object",
191 "properties": {
192 "id": {"type": "string"},
193 "tag": {"type": "string"},
194 },
195 "required": ["id"],
196 },
197 },
198 "either": {"oneOf": [{"type": "string"}, {"type": "number"}]},
199 },
200 "required": ["payload", "rows", "either"],
201 }
202 )
203
204 assert result.schema["properties"]["payload"]["anyOf"][0] == {
205 "type": "object",
206 "properties": {
207 "inner": {"type": "string"},
208 "extra": {"type": ["string", "null"]},
209 },
210 "required": ["inner", "extra"],
211 "additionalProperties": False,
212 }
213 assert result.schema["properties"]["rows"]["items"]["required"] == ["id", "tag"]
214 assert result.schema["properties"]["either"] == {
215 "anyOf": [{"type": "string"}, {"type": "number"}]
216 }
217 assert "one-of-converted" in {c.reason for c in result.changes}
218 assert_strict_shape(result.schema)
219
220 def test_reports_dynamic_key_and_free_form_objects_as_unsupported(self):
221 result = to_strict_json_schema(
222 {
223 "type": "object",
224 "properties": {
225 "headers": {
226 "type": "object",
227 "additionalProperties": {"type": "string"},
228 },
229 "meta": {"type": "object", "description": "any json"},
230 "tagged": {
231 "type": "object",
232 "patternProperties": {"^x-": {"type": "string"}},
233 },
234 "open": {"type": "object", "additionalProperties": True},
235 },
236 "required": ["headers", "meta", "tagged", "open"],
237 }
238 )
239
240 assert [(e.path, e.keyword) for e in result.unsupported] == [
241 ("properties.headers", "additionalProperties"),
242 ("properties.meta", "properties"),
243 ("properties.tagged", "patternProperties"),
244 ("properties.open", "additionalProperties"),
245 ]
246 # The schema-valued additionalProperties is preserved, not overwritten.
247 assert result.schema["properties"]["headers"]["additionalProperties"] == {
248 "type": "string"
249 }
250 assert "patternProperties" in result.schema["properties"]["tagged"]
251
252 def test_strips_annotation_keywords_and_reports_keywords_it_cannot_rewrite(self):
253 result = to_strict_json_schema(
254 {
255 "type": "object",
256 "properties": {
257 "name": {"type": "string", "examples": ["a"], "default": "x"},
258 "pair": {"type": "array", "prefixItems": [{"type": "number"}]},
259 "all": {"allOf": [{"type": "string"}]},
260 },
261 "required": ["name", "pair", "all"],
262 }
263 )
264
265 assert result.schema["properties"]["name"] == {"type": "string"}
266 reasons = [c.reason for c in result.changes]
267 assert reasons.count("unsupported-keyword-stripped") == 2
268 assert [(e.path, e.keyword) for e in result.unsupported] == [
269 ("properties.pair", "prefixItems"),
270 ("properties.all", "allOf"),
271 ]
272
273 def test_keeps_defs_and_refs_and_reports_dangling_refs(self):
274 result = to_strict_json_schema(
275 {
276 "type": "object",
277 "properties": {
278 "cfg": {"$ref": "#/$defs/Config"},
279 "optionalCfg": {
280 "$ref": "#/$defs/Config",
281 "description": "optional",
282 },
283 "missing": {"$ref": "#/$defs/Nope"},
284 "external": {"$ref": "https://example.com/schema.json"},
285 },
286 "required": ["cfg", "cfg", "missing", "external"],
287 "$defs": {
288 "Config": {
289 "type": "object",
290 "properties": {
291 "url": {"type": "string"},
292 "note": {"type": "string"},
293 },
294 "required": ["url"],
295 }
296 },
297 }
298 )
299
300 assert result.schema["properties"]["cfg"] == {"$ref": "#/$defs/Config"}
301 assert result.schema["properties"]["optionalCfg"] == {
302 "description": "optional",
303 "anyOf": [{"$ref": "#/$defs/Config"}, {"type": "null"}],
304 }
305 assert result.schema["$defs"] == {
306 "Config": {
307 "type": "object",
308 "properties": {
309 "url": {"type": "string"},
310 "note": {"type": ["string", "null"]},
311 },
312 "required": ["url", "note"],
313 "additionalProperties": False,
314 }
315 }
316 assert result.schema["required"] == [
317 "cfg",
318 "optionalCfg",
319 "missing",
320 "external",
321 ]
322 assert [(e.path, e.keyword) for e in result.unsupported] == [
323 ("properties.missing", "$ref"),
324 ("properties.external", "$ref"),
325 ]
326 assert ("$defs.Config.properties.note", "optional-property-nullable") in [
327 (c.path, c.reason) for c in result.changes
328 ]
329
330 def test_caps_the_change_log_without_losing_properties(self):
331 properties = {f"p{i}": {"type": "string"} for i in range(60)}
332
333 result = to_strict_json_schema(
334 {"type": "object", "properties": properties, "required": ["p0"]}
335 )
336
337 assert len(result.schema["properties"]) == 60
338 assert len(result.changes) == 50
339 assert result.total_changes == 59
340
341 def test_does_not_mutate_input(self):
342 schema = {
343 "type": "object",
344 "properties": {
345 "cfg": {
346 "type": "object",
347 "properties": {"opt": {"type": "string", "default": 1}},
348 }
349 },
350 "required": ["cfg"],
351 }
352 snapshot = copy.deepcopy(schema)
353
354 to_strict_json_schema(schema)
355
356 assert schema == snapshot
357
358 def test_is_idempotent(self):
359 once = to_strict_json_schema(
360 {
361 "type": "object",
362 "properties": {
363 "cfg": {"$ref": "#/$defs/Config"},
364 "id": {"type": ["string", "null"]},
365 },
366 "$defs": {
367 "Config": {
368 "type": "object",
369 "properties": {
370 "url": {"type": "string"},
371 "note": {"type": "string"},
372 },
373 "required": ["url"],
374 }
375 },
376 }
377 ).schema
378
379 twice = to_strict_json_schema(once)
380
381 assert twice.schema == once
382 assert twice.changes == []
383 assert twice.unsupported == []
384
385 def test_reports_non_object_roots_as_unsupported_and_keeps_recursive_defs(self):
386 assert [
387 (e.path, e.keyword)
388 for e in to_strict_json_schema({"type": "string"}).unsupported
389 ] == [("", "type")]
390 nullable_root = to_strict_json_schema(
391 {"type": ["object", "null"], "properties": {"a": {"type": "string"}}}
392 )
393 assert ("", "type") in [(e.path, e.keyword) for e in nullable_root.unsupported]
394
395 cyclic = to_strict_json_schema(
396 {
397 "type": "object",
398 "properties": {"node": {"$ref": "#/$defs/Node"}},
399 "required": ["node"],
400 "$defs": {
401 "Node": {
402 "type": "object",
403 "properties": {
404 "child": {"$ref": "#/$defs/Node"},
405 "label": {"type": "string"},
406 },
407 "required": ["label"],
408 }
409 },
410 }
411 )
412 # Recursion through $defs is representable in strict mode.
413 assert cyclic.unsupported == []
414 assert cyclic.schema["$defs"]["Node"] == {
415 "type": "object",
416 "properties": {
417 "child": {"anyOf": [{"$ref": "#/$defs/Node"}, {"type": "null"}]},
418 "label": {"type": "string"},
419 },
420 "required": ["child", "label"],
421 "additionalProperties": False,
422 }
423
424 def test_raises_past_maximum_depth(self):
425 deep = {"type": "string"}
426 for _ in range(600):
427 deep = {
428 "type": "object",
429 "properties": {"nest": deep},
430 "required": ["nest"],
431 }
432
433 with pytest.raises(Exception, match="depth"):
434 to_strict_json_schema(deep)
435
436
437 _OMIT_SCHEMA = {
438 "type": "object",
439 "properties": {
440 "cfg": {
441 "type": "object",
442 "properties": {"url": {"type": "string"}, "note": {"type": "string"}},
443 "required": ["url"],
444 },
445 "label": {"type": "string"},
446 "clearable": {"type": ["string", "null"]},
447 "choice": {"anyOf": [{"enum": ["a"]}, {"type": "null"}]},
448 "rows": {
449 "type": "array",
450 "items": {
451 "type": "object",
452 "properties": {"id": {"type": "string"}, "tag": {"type": "string"}},
453 },
454 },
455 "refd": {"$ref": "#/$defs/Str"},
456 "refdNullable": {"$ref": "#/$defs/NullableStr"},
457 },
458 "$defs": {"Str": {"type": "string"}, "NullableStr": {"type": ["string", "null"]}},
459 }
460
461
462 class TestOmitNullToolArguments:
463 def test_drops_nulls_the_schema_rejects_and_keeps_the_ones_it_accepts(self):
464 arguments = {
465 "cfg": {"url": "https://example.com", "note": None},
466 "label": None,
467 "clearable": None,
468 "choice": None,
469 "unknown": None,
470 "rows": [{"id": "1", "tag": None}, None],
471 "refd": None,
472 "refdNullable": None,
473 }
474 snapshot = copy.deepcopy(arguments)
475
476 assert omit_null_tool_arguments(arguments, _OMIT_SCHEMA) == {
477 "cfg": {"url": "https://example.com"},
478 "clearable": None,
479 "choice": None,
480 "unknown": None,
481 "rows": [{"id": "1"}, None],
482 "refdNullable": None,
483 }
484 assert arguments == snapshot
485
486
487 class TestOpenAIResponsesProviderStrict:
488 def _tool(self, input_parameters):
489 from tests.test_provider import create_mock_tool
490
491 tool = create_mock_tool("TEST_TOOL", "composio")
492 tool.input_parameters = input_parameters
493 return tool
494
495 def test_wrap_tool_passes_parameters_through_by_default(self):
496 from composio.core.provider._openai_responses import OpenAIResponsesProvider
497
498 provider = OpenAIResponsesProvider()
499 wrapped = provider.wrap_tool(self._tool({"type": "object", "properties": {}}))
500
501 assert wrapped["parameters"] == {"type": "object", "properties": {}}
502 assert wrapped["strict"] is False
503
504 def test_constructor_keeps_base_provider_config(self):
505 from composio.core.provider._openai_responses import OpenAIResponsesProvider
506
507 provider = OpenAIResponsesProvider(
508 strict=True, schema_config={"skip_defaults": True}
509 )
510
511 assert provider.strict is True
512 assert provider.skip_default is True
513
514 def test_wrap_tool_emits_strict_and_normalizes_complex_schemas(self):
515 from composio.core.provider._openai_responses import OpenAIResponsesProvider
516
517 provider = OpenAIResponsesProvider(strict=True)
518 wrapped = provider.wrap_tool(
519 self._tool(
520 {
521 "type": "object",
522 "properties": {
523 "cfg": {"$ref": "#/$defs/Config"},
524 "id": {"type": ["string", "null"]},
525 "label": {"type": "string"},
526 },
527 "required": ["cfg", "id"],
528 "$defs": {
529 "Config": {
530 "type": "object",
531 "properties": {
532 "url": {"type": "string"},
533 "note": {"type": "string"},
534 },
535 "required": ["url"],
536 }
537 },
538 }
539 )
540 )
541
542 assert wrapped["strict"] is True
543 assert wrapped["parameters"] == {
544 "type": "object",
545 "properties": {
546 "cfg": {"$ref": "#/$defs/Config"},
547 "id": {"type": ["string", "null"]},
548 "label": {"type": ["string", "null"]},
549 },
550 "required": ["cfg", "id", "label"],
551 "additionalProperties": False,
552 "$defs": {
553 "Config": {
554 "type": "object",
555 "properties": {
556 "url": {"type": "string"},
557 "note": {"type": ["string", "null"]},
558 },
559 "required": ["url", "note"],
560 "additionalProperties": False,
561 }
562 },
563 }
564
565 def test_wrap_tool_sends_unsupported_schemas_without_strict(self):
566 from composio.core.provider._openai_responses import OpenAIResponsesProvider
567
568 provider = OpenAIResponsesProvider(strict=True)
569 input_parameters = {
570 "type": "object",
571 "properties": {
572 "headers": {
573 "type": "object",
574 "additionalProperties": {"type": "string"},
575 },
576 "name": {"type": "string"},
577 },
578 "required": ["headers"],
579 }
580 wrapped = provider.wrap_tool(self._tool(input_parameters))
581
582 assert wrapped["strict"] is False
583 assert wrapped["parameters"] == input_parameters
584
585 def test_wrap_tool_emits_empty_closed_object_without_parameters(self):
586 from composio.core.provider._openai_responses import OpenAIResponsesProvider
587
588 provider = OpenAIResponsesProvider(strict=True)
589 wrapped = provider.wrap_tool(self._tool(None))
590
591 assert wrapped["strict"] is True
592 assert wrapped["parameters"] == {
593 "type": "object",
594 "properties": {},
595 "required": [],
596 "additionalProperties": False,
597 }
598
599 def test_wrap_tool_keeps_explicit_empty_schema_without_strict(self):
600 from composio.core.provider._openai_responses import OpenAIResponsesProvider
601
602 provider = OpenAIResponsesProvider(strict=True)
603 wrapped = provider.wrap_tool(self._tool({}))
604
605 assert wrapped["strict"] is False
606 assert wrapped["parameters"] == {}
607
608 def test_execute_tool_call_omits_null_arguments_in_strict_mode(self):
609 from openai.types.responses.response_output_item import ResponseFunctionToolCall
610
611 from composio.core.provider._openai_responses import OpenAIResponsesProvider
612
613 received = {}
614
615 def execute_tool(slug, arguments, modifiers=None, **kwargs):
616 received["slug"] = slug
617 received["arguments"] = arguments
618 return {"data": {}, "error": None, "successful": True}
619
620 provider = OpenAIResponsesProvider(strict=True)
621 provider.set_execute_tool_fn(execute_tool)
622 provider.wrap_tool(
623 self._tool(
624 {
625 "type": "object",
626 "properties": {
627 "cfg": {
628 "type": "object",
629 "properties": {
630 "url": {"type": "string"},
631 "note": {"type": "string"},
632 },
633 "required": ["url"],
634 },
635 "label": {"type": "string"},
636 "clearable": {"type": ["string", "null"]},
637 },
638 "required": ["cfg"],
639 }
640 )
641 )
642 provider.execute_tool_call(
643 user_id="user",
644 tool_call=ResponseFunctionToolCall(
645 type="function_call",
646 call_id="call_1",
647 name="TEST_TOOL",
648 arguments=json.dumps(
649 {
650 "cfg": {"url": "u", "note": None},
651 "label": None,
652 "clearable": None,
653 }
654 ),
655 ),
656 )
657
658 assert received["slug"] == "TEST_TOOL"
659 assert received["arguments"] == {"cfg": {"url": "u"}, "clearable": None}
660
661
662 NULL_BRANCH = {"type": "null"}
663 DIRECTION = {"enum": ["asc", None]}
664
665
666 def _wrapped(schema):
667 return {"anyOf": [schema, NULL_BRANCH]}
668
669
670 class TestNullAcceptance:
671 """Null widening and null omission decide with every keyword, not the first."""
672
673 @pytest.mark.parametrize(
674 "property_schema, expected",
675 [
676 (
677 {"type": "string", "enum": ["asc", "desc"]},
678 _wrapped({"type": "string", "enum": ["asc", "desc"]}),
679 ),
680 (
681 {"type": ["string", "null"], "enum": ["asc", "desc"]},
682 _wrapped({"type": ["string", "null"], "enum": ["asc", "desc"]}),
683 ),
684 (
685 {"type": "string", "const": "asc"},
686 _wrapped({"type": "string", "const": "asc"}),
687 ),
688 (
689 {"type": "integer", "enum": [1, 2], "description": "page size"},
690 {
691 "description": "page size",
692 **_wrapped({"type": "integer", "enum": [1, 2]}),
693 },
694 ),
695 (
696 {"type": "string", "anyOf": [{"minLength": 1}]},
697 _wrapped({"type": "string", "anyOf": [{"minLength": 1}]}),
698 ),
699 (
700 {"enum": ["asc"], "anyOf": [{"type": "string"}, NULL_BRANCH]},
701 _wrapped({"enum": ["asc"], "anyOf": [{"type": "string"}, NULL_BRANCH]}),
702 ),
703 (
704 {"$ref": "#/$defs/Direction", "type": "string"},
705 _wrapped({"$ref": "#/$defs/Direction", "type": "string"}),
706 ),
707 # Already nullable: left alone.
708 (
709 {"type": ["string", "null"], "enum": ["asc", None]},
710 {"type": ["string", "null"], "enum": ["asc", None]},
711 ),
712 ({"$ref": "#/$defs/Direction"}, {"$ref": "#/$defs/Direction"}),
713 (
714 {"anyOf": [{"type": ["string", "null"]}]},
715 {"anyOf": [{"type": ["string", "null"]}]},
716 ),
717 # `type` or `anyOf` as the only obstacle: widened in place.
718 (
719 {"type": "string", "minLength": 1},
720 {"type": ["string", "null"], "minLength": 1},
721 ),
722 (
723 {"anyOf": [{"type": "string"}], "description": "d"},
724 {"anyOf": [{"type": "string"}, NULL_BRANCH], "description": "d"},
725 ),
726 ],
727 )
728 def test_widening_keeps_every_constraint(self, property_schema, expected):
729 source = {
730 "type": "object",
731 "properties": {"value": property_schema},
732 "$defs": {"Direction": DIRECTION},
733 }
734 snapshot = copy.deepcopy(source)
735 result = to_strict_json_schema(source)
736
737 assert result.unsupported == []
738 assert result.schema["properties"]["value"] == expected
739 assert result.schema["required"] == ["value"]
740 assert source == snapshot
741 again = to_strict_json_schema(result.schema)
742 assert again.schema == result.schema
743 assert again.changes == []
744
745 @pytest.mark.parametrize(
746 "property_schema, kept",
747 [
748 ({"type": ["string", "null"], "enum": ["asc", "desc"]}, False),
749 ({"type": ["string", "null"], "const": "asc"}, False),
750 ({"type": ["string", "null"], "enum": ["asc", None]}, True),
751 ({"type": ["string", "null"], "const": None}, True),
752 ({"$ref": "#/$defs/Direction"}, True),
753 ({"$ref": "#/$defs/Direction", "type": "string"}, False),
754 ({"$ref": "#/$defs/Direction", "enum": ["asc"]}, False),
755 ({"$ref": "#/$defs/Missing"}, False),
756 ({"$ref": 1}, False),
757 ({"allOf": [{"type": ["string", "null"]}, {"enum": ["asc"]}]}, False),
758 ({"allOf": [{"type": ["string", "null"]}, DIRECTION]}, True),
759 ({"oneOf": [NULL_BRANCH, {"type": "string"}]}, True),
760 ({"oneOf": [NULL_BRANCH, {"enum": [None]}]}, False),
761 ({"not": NULL_BRANCH}, False),
762 ({"not": {"type": "string"}}, True),
763 ({"if": NULL_BRANCH, "then": {"type": "string"}}, False),
764 ({"if": {"type": "string"}, "then": {"type": "string"}}, True),
765 ({"if": {"type": "string"}, "else": {"type": "string"}}, False),
766 (True, True),
767 (False, False),
768 ],
769 )
770 def test_null_is_kept_only_when_every_keyword_accepts_it(
771 self, property_schema, kept
772 ):
773 schema = {
774 "type": "object",
775 "properties": {"value": property_schema},
776 "$defs": {"Direction": DIRECTION},
777 }
778 arguments = {"value": None}
779 expected = {"value": None} if kept else {}
780 assert omit_null_tool_arguments(arguments, schema) == expected
781 assert arguments == {"value": None}
782
783 @pytest.mark.parametrize(
784 "definitions, kept",
785 [
786 ({"a": {"$ref": "#/$defs/a"}}, False),
787 ({"a": {"$ref": "#/$defs/b"}, "b": {"$ref": "#/$defs/a"}}, False),
788 ({"a": {"anyOf": [{"$ref": "#/$defs/a"}]}}, False),
789 ({"a": {"type": "null", "oneOf": [{"$ref": "#/$defs/a"}]}}, False),
790 (
791 {
792 "a": {"anyOf": [{"$ref": "#/$defs/b"}]},
793 "b": {"anyOf": [{"$ref": "#/$defs/a"}]},
794 },
795 False,
796 ),
797 ({"a": {"anyOf": [{"$ref": "#/$defs/a"}, NULL_BRANCH]}}, True),
798 ({"a": {"oneOf": [{"$ref": "#/$defs/a"}, NULL_BRANCH]}}, True),
799 ],
800 )
801 def test_reference_cycles_terminate(self, definitions, kept):
802 schema = {
803 "type": "object",
804 "properties": {"value": {"$ref": "#/$defs/a"}},
805 "$defs": definitions,
806 }
807 expected = {"value": None} if kept else {}
808 assert omit_null_tool_arguments({"value": None}, schema) == expected
809 assert to_strict_json_schema(schema).unsupported == []
810
811 def test_a_node_reached_twice_outside_a_cycle_is_checked_each_time(self):
812 shared = {"anyOf": [NULL_BRANCH]}
813 schema = {
814 "type": "object",
815 "properties": {"value": {"allOf": [shared, shared]}},
816 }
817 assert omit_null_tool_arguments({"value": None}, schema) == {"value": None}
818
819 @staticmethod
820 def _diamond(levels, keyword, leaf):
821 """``a0`` is the leaf; every ``a(i)`` references ``a(i-1)`` twice."""
822 definitions = {"a0": leaf}
823 for i in range(1, levels + 1):
824 ref = {"$ref": f"#/$defs/a{i - 1}"}
825 definitions[f"a{i}"] = {keyword: [ref, ref]}
826 return {
827 "type": "object",
828 "properties": {"value": {"$ref": f"#/$defs/a{levels}"}},
829 "$defs": definitions,
830 }
831
832 def test_a_definition_shared_by_many_branches_is_evaluated_once(self):
833 rejecting = self._diamond(20, "anyOf", {"type": "string"})
834 strict = to_strict_json_schema(rejecting).schema
835 assert strict["properties"]["value"] == _wrapped({"$ref": "#/$defs/a20"})
836 assert omit_null_tool_arguments({"value": None}, rejecting) == {}
837
838 accepting = self._diamond(20, "allOf", NULL_BRANCH)
839 assert omit_null_tool_arguments({"value": None}, accepting) == {"value": None}
840
841 def test_stays_bounded_when_cycles_keep_answers_from_being_remembered(self):
842 # Every level also points back at the top, so no answer below it is final.
843 levels = 20
844 top = {"$ref": f"#/$defs/a{levels}"}
845 definitions = {"a0": {"type": "string"}}
846 for i in range(1, levels + 1):
847 ref = {"$ref": f"#/$defs/a{i - 1}"}
848 definitions[f"a{i}"] = {"anyOf": [ref, ref, top]}
849 schema = {"type": "object", "properties": {"value": top}, "$defs": definitions}
850 assert omit_null_tool_arguments({"value": None}, schema) == {}
851 strict = to_strict_json_schema(schema).schema
852 assert strict["properties"]["value"] == _wrapped(top)
853
854 def test_null_is_not_proven_through_a_reference_chain_past_the_depth_bound(self):
855 definitions = {"a0": NULL_BRANCH}
856 for i in range(1, 101):
857 definitions[f"a{i}"] = {"$ref": f"#/$defs/a{i - 1}"}
858
859 def schema(name):
860 return {
861 "type": "object",
862 "properties": {"value": {"$ref": f"#/$defs/{name}"}},
863 "$defs": definitions,
864 }
865
866 assert omit_null_tool_arguments({"value": None}, schema("a100")) == {}
867 assert omit_null_tool_arguments({"value": None}, schema("a10")) == {
868 "value": None
869 }
870
871 def test_ref_to_a_target_the_rewrite_types_as_an_object_gets_a_null_branch(self):
872 source = {
873 "type": "object",
874 "properties": {"node": {"$ref": "#/$defs/Node"}},
875 "$defs": {
876 "Node": {
877 "properties": {
878 "label": {"type": "string"},
879 "child": {"$ref": "#/$defs/Node"},
880 }
881 }
882 },
883 }
884 result = to_strict_json_schema(source)
885 node = result.schema["$defs"]["Node"]
886
887 assert result.unsupported == []
888 assert result.schema["properties"]["node"] == _wrapped({"$ref": "#/$defs/Node"})
889 assert node["type"] == "object"
890 assert node["properties"]["child"] == _wrapped({"$ref": "#/$defs/Node"})
891 # The tool's own schema accepts the null, so it is forwarded.
892 assert omit_null_tool_arguments({"node": None}, source) == {"node": None}
893
894 def test_ref_into_a_property_that_wrapping_moved_is_reported(self):
895 result = to_strict_json_schema(
896 {
897 "type": "object",
898 "properties": {
899 "value": {
900 "type": "string",
901 "enum": ["asc"],
902 "$defs": {"Text": {"type": "string"}},
903 },
904 "alias": {"$ref": "#/properties/value/$defs/Text"},
905 },
906 "required": ["alias"],
907 }
908 )
909 assert [(e.path, e.keyword) for e in result.unsupported] == [
910 ("properties.alias", "$ref")
911 ]
912
913 def test_many_properties_share_what_was_learned_about_a_cyclic_definition(self):
914 levels = 20
915 top = f"#/$defs/a{levels}"
916 definitions = {"a0": {"type": "string"}}
917 for i in range(1, levels + 1):
918 lower = f"#/$defs/a{i - 1}"
919 definitions[f"a{i}"] = {
920 "anyOf": [{"$ref": lower}, {"$ref": lower}, {"$ref": top}]
921 }
922 names = [f"p{i}" for i in range(400)]
923 schema = {
924 "type": "object",
925 "properties": {name: {"$ref": top} for name in names},
926 "$defs": definitions,
927 }
928 strict = to_strict_json_schema(schema).schema
929 assert all(
930 strict["properties"][name] == _wrapped({"$ref": top}) for name in names
931 )
932 assert omit_null_tool_arguments(dict.fromkeys(names), schema) == {}
933
934 def test_a_check_that_runs_out_of_budget_does_not_affect_the_next_one(self):
935 schema = {
936 "type": "object",
937 "properties": {
938 "wide": {"anyOf": [{"type": "string"} for _ in range(5000)]},
939 "note": {"type": ["string", "null"]},
940 "same": {"anyOf": [{"type": "string"}, NULL_BRANCH]},
941 },
942 }
943 arguments = {"wide": None, "note": None, "same": None}
944 assert omit_null_tool_arguments(arguments, schema) == {
945 "note": None,
946 "same": None,
947 }
948 strict = to_strict_json_schema(schema).schema["properties"]
949 assert strict["note"] == {"type": ["string", "null"]}
950 assert strict["same"] == {"anyOf": [{"type": "string"}, NULL_BRANCH]}
951
952 def test_a_check_that_meets_a_bound_proves_nothing(self):
953 # Both branches accept null, so `oneOf` rejects it; the second is only
954 # reachable past the depth bound and must not count as a rejection.
955 definitions = {"a0": NULL_BRANCH}
956 for i in range(1, 71):
957 definitions[f"a{i}"] = {"$ref": f"#/$defs/a{i - 1}"}
958 deep = {"$ref": "#/$defs/a70"}
959 schema = {
960 "type": "object",
961 "properties": {
962 "one": {"oneOf": [NULL_BRANCH, deep]},
963 "negated": {"not": deep},
964 "guarded": {"if": deep, "else": NULL_BRANCH},
965 # Reaches a node the checks above left unproven.
966 "later": {"not": {"$ref": "#/$defs/a69"}},
967 },
968 "$defs": definitions,
969 }
970 arguments = dict.fromkeys(schema["properties"])
971 assert omit_null_tool_arguments(arguments, schema) == {}
972
973 def test_an_unproven_definition_is_not_evaluated_again_for_each_property(self):
974 # `top` is cyclic and wider than the budget, so nothing about it settles.
975 definitions = {
976 "top": {"anyOf": [{"$ref": f"#/$defs/b{i}"} for i in range(3000)]}
977 }
978 for i in range(3000):
979 definitions[f"b{i}"] = {
980 "anyOf": [{"$ref": "#/$defs/top"}, {"type": "string"}]
981 }
982 names = [f"p{i}" for i in range(2000)]
983 schema = {
984 "type": "object",
985 "properties": {name: {"$ref": "#/$defs/top"} for name in names},
986 "$defs": definitions,
987 }
988 assert omit_null_tool_arguments(dict.fromkeys(names), schema) == {}
989 strict = to_strict_json_schema(schema).schema["properties"]
990 assert all(strict[name] == _wrapped({"$ref": "#/$defs/top"}) for name in names)