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",
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
}
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
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")
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)
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,
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:
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]