@devries-cameron20 / composio-1 / commits / 84684c6c8
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.

fix(python): mirror the strict-mode incompatibility and branch rules

to_strict_json_schema reports the same constructs as the TypeScript implementation (tuple items, boolean subschemas, malformed properties, oneOf beside anyOf, conditional and dependency keywords), accepts a root typed ["object"], leaves enum/const that already include null alone, and omit_null_tool_arguments follows the composition branch matching the argument's shape. Co-authored-by: AseemPrasad <aseemprasad0520@gmail.com> Claude-Session: https://claude.ai/code/session_01TDrxCHn2hg51HmxVstSUgs

jkomyno committed Aug 26, 2026 at 17:59 UTC 84684c6c8d5f1104bf0e2a27d0a3f241c977e89f
1 file changed +74 -4
python/composio/utils/strict_schema.py
+74 -4
@@ -62,7 +62,29 @@ SCHEMA_MAP_KEYWORDS = frozenset(
62 )
63
64 # Keywords OpenAI structured outputs reject and that have no lossless rewrite.
65 -STRICT_UNSUPPORTED_KEYWORDS = ("allOf", "prefixItems")
65 +STRICT_UNSUPPORTED_KEYWORDS = (
66 + "allOf",
67 + "prefixItems",
68 + "additionalItems",
69 + "contains",
70 + "dependencies",
71 + "dependentSchemas",
72 + "else",
73 + "if",
74 + "not",
75 + "propertyNames",
76 + "then",
77 + "unevaluatedItems",
78 + "unevaluatedProperties",
79 +)
80 +
81 +
82 +def _is_object_type(node_type: t.Any) -> bool:
83 + """Whether a ``type`` value declares exactly the object type."""
84 + return node_type == "object" or (
85 + isinstance(node_type, list) and node_type == ["object"]
86 + )
87 +
88
89 StrictSchemaChangeReason = t.Literal[
90 "optional-property-nullable",
@@ -115,6 +137,10 @@ def _widen_to_nullable(node: dict[str, t.Any]) -> dict[str, t.Any]:
137 return node if node_type == "null" else {**node, "type": [node_type, "null"]}
138 if isinstance(node_type, list):
139 return node if "null" in node_type else {**node, "type": [*node_type, "null"]}
140 + if (isinstance(node.get("enum"), list) and None in node["enum"]) or (
141 + "const" in node and node["const"] is None
142 + ):
143 + return node
144 annotations: dict[str, t.Any] = {
145 k: node[k] for k in ("description", "title") if k in node
146 }
@@ -253,6 +279,14 @@ class _Walker:
279 for keyword in STRICT_UNSUPPORTED_KEYWORDS:
280 if keyword in node:
281 self.reject(path, keyword, f'"{keyword}" has no strict-mode equivalent')
282 + if "oneOf" in node:
283 + self.reject(path, "oneOf", "oneOf beside anyOf cannot be merged")
284 + if isinstance(node.get("items"), list):
285 + self.reject(path, "items", "tuple-form items has no strict-mode equivalent")
286 + elif isinstance(node.get("items"), bool):
287 + self.reject(path, "items", "boolean subschema")
288 + if "properties" in node and not isinstance(node["properties"], dict):
289 + self.reject(path, "properties", "properties is not an object")
290
291 out = self.walk_children(node, mode, depth, path)
292
@@ -270,6 +304,8 @@ class _Walker:
304 )
305 if not declares_object and not isinstance(out.get("properties"), dict):
306 return out
307 + if "properties" in out and not isinstance(out["properties"], dict):
308 + return out
309
310 properties: dict[str, t.Any] = dict(out.get("properties") or {})
311 required = out.get("required")
@@ -279,6 +315,13 @@ class _Walker:
315 if isinstance(entry, str)
316 }
317 for name, property_schema in properties.items():
318 + if isinstance(property_schema, bool):
319 + self.reject(
320 + _join_path(path, f"properties.{name}"),
321 + "properties",
322 + "boolean subschema",
323 + )
324 + continue
325 if name in declared_required or not isinstance(property_schema, dict):
326 continue
327 properties[name] = _widen_to_nullable(property_schema)
@@ -326,7 +369,7 @@ def to_strict_json_schema(schema: t.Any) -> StrictJsonSchemaResult:
369 root: dict[str, t.Any] = schema if isinstance(schema, dict) else {}
370 walker = _Walker(root)
371 normalized = _dedupe_required(walker.walk(root, "schema", 0, ""))
329 - if not isinstance(normalized, dict) or normalized.get("type") != "object":
372 + if not isinstance(normalized, dict) or not _is_object_type(normalized.get("type")):
373 walker.reject("", "type", "root must be a non-nullable object")
374 return StrictJsonSchemaResult(
375 schema=normalized,
@@ -355,6 +398,34 @@ def omit_null_tool_arguments(
398 return t.cast(dict[str, t.Any], _omit_nulls(arguments, root, root, 0))
399
400
401 +def _select_branch_for(
402 + schema: t.Any, value: t.Any, root: dict[str, t.Any]
403 +) -> dict[str, t.Any] | None:
404 + """Pick the composition branch that describes a value's shape."""
405 + resolved = _resolve_local_refs(schema, root)
406 + if not isinstance(resolved, dict):
407 + return None
408 + wanted = "items" if isinstance(value, list) else "properties"
409 +
410 + def find(node: t.Any, depth: int) -> dict[str, t.Any] | None:
411 + candidate = _resolve_local_refs(node, root)
412 + if not isinstance(candidate, dict) or depth > MAX_NODE_DEPTH:
413 + return None
414 + if wanted in candidate:
415 + return candidate
416 + for keyword in ("anyOf", "oneOf"):
417 + branches = candidate.get(keyword)
418 + if not isinstance(branches, list):
419 + continue
420 + for branch in branches:
421 + found = find(branch, depth + 1)
422 + if found is not None:
423 + return found
424 + return None
425 +
426 + return find(resolved, 0) or resolved
427 +
428 +
429 def _omit_nulls(
430 value: t.Any, schema: t.Any, root: dict[str, t.Any], depth: int
431 ) -> t.Any:
@@ -362,8 +433,7 @@ def _omit_nulls(
433 raise ValueError(
434 f"Tool arguments exceed maximum nesting depth of {MAX_NODE_DEPTH}"
435 )
365 - resolved = _resolve_local_refs(schema, root)
366 - node = resolved if isinstance(resolved, dict) else {}
436 + node = _select_branch_for(schema, value, root) or {}
437 if isinstance(value, list):
438 items = node.get("items") if isinstance(node.get("items"), dict) else None
439 return [_omit_nulls(item, items, root, depth + 1) for item in value]