@devries-cameron20 / composio Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 03:37 UTC

next
py 80 lines 2.94 KB
Raw
1 """Drive to_strict_json_schema from the shared strict-mode corpus.
2
3 The corpus pins the exact output of the TypeScript implementation, so this
4 suite is the cross-SDK parity check for strict normalization.
5 """
6
7 import copy
8
9 import pytest
10
11 from composio.utils.strict_schema import omit_null_tool_arguments, to_strict_json_schema
12 from tests.fixtures.json_schema_conversion_corpus import load_strict_cases
13
14
15 def assert_strict_shape(node, path=""):
16 """Structural invariants OpenAI enforces on every node of a strict schema."""
17 if isinstance(node, list):
18 for index, item in enumerate(node):
19 assert_strict_shape(item, f"{path}[{index}]")
20 return
21 if not isinstance(node, dict):
22 return
23 if "anyOf" in node:
24 assert "type" not in node, f"{path}: type beside anyOf"
25 for keyword in (
26 "default",
27 "examples",
28 "oneOf",
29 "patternProperties",
30 "allOf",
31 "prefixItems",
32 ):
33 assert keyword not in node, f"{path}: {keyword}"
34 node_type = node.get("type")
35 is_object = node_type == "object" or (
36 isinstance(node_type, list) and "object" in node_type
37 )
38 if is_object or "properties" in node:
39 properties = node.get("properties") or {}
40 assert node.get("required") == list(properties.keys()), f"{path}: required"
41 assert node.get("additionalProperties") is False, (
42 f"{path}: additionalProperties"
43 )
44 for key, child in node.items():
45 if key in ("enum", "const"):
46 continue
47 child_path = f"{path}.{key}" if path else key
48 if key in ("properties", "$defs", "definitions") and isinstance(child, dict):
49 # Keys of these maps are names, not keywords.
50 for name, sub in child.items():
51 assert_strict_shape(sub, f"{child_path}.{name}")
52 continue
53 assert_strict_shape(child, child_path)
54
55
56 @pytest.mark.parametrize("case", load_strict_cases(), ids=lambda case: case.id)
57 def test_strict_corpus_case(case):
58 snapshot = copy.deepcopy(case.schema_)
59 result = to_strict_json_schema(case.schema_)
60
61 assert case.schema_ == snapshot, "input mutated"
62 if case.strict.unsupported is not None:
63 assert [(e.path, e.keyword) for e in result.unsupported] == [
64 (e.path, e.keyword) for e in case.strict.unsupported
65 ]
66 else:
67 assert result.unsupported == []
68 assert result.schema == case.strict.schema_
69 assert_strict_shape(result.schema)
70 again = to_strict_json_schema(result.schema)
71 assert again.schema == result.schema, "not idempotent"
72 assert again.changes == []
73 assert [(c.path, c.reason) for c in result.changes] == [
74 (c.path, c.reason) for c in (case.changes or [])
75 ]
76 assert result.total_changes == len(result.changes)
77 for arguments in case.arguments or []:
78 assert (
79 omit_null_tool_arguments(arguments.input, result.source) == arguments.output
80 )