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 2,920 lines 103 KB
Raw
1 """
2 JSON Schema to Pydantic type conversion using json-schema-to-pydantic library.
3
4 This module provides a wrapper around json-schema-to-pydantic that maintains
5 backwards compatibility with the existing json_schema_to_pydantic_type() API.
6
7 The library handles most JSON Schema features correctly, but doesn't support
8 boolean schemas (true/false values that are valid in JSON Schema draft-06+).
9 We handle those by pre-filtering them before passing to the library.
10 """
11
12 import decimal
13 import functools
14 import importlib
15 import re
16 import types
17 import typing as t
18 from functools import reduce
19 from urllib.parse import unquote
20
21 from json_schema_to_pydantic import (
22 CombinerError,
23 SchemaError,
24 )
25 from json_schema_to_pydantic import (
26 create_model as create_model_from_schema,
27 )
28 from jsonschema import exceptions as jsonschema_exceptions
29 from jsonschema import validators as jsonschema_validators
30 from jsonschema.protocols import Validator
31 from pydantic import (
32 AfterValidator,
33 BaseModel,
34 BeforeValidator,
35 ConfigDict,
36 Field,
37 TypeAdapter,
38 ValidationError,
39 WithJsonSchema,
40 model_validator,
41 )
42 from pydantic import (
43 create_model as create_pydantic_model,
44 )
45 from pydantic_core import core_schema
46 from referencing import Registry
47
48 from composio.utils.logging import get as get_logger
49
50 logger = get_logger(__name__)
51
52 _MISSING = object()
53 _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE = "__composio_explicit_default_fields__"
54
55
56 class _UnsatisfiableSchema:
57 """Pydantic type for JSON schemas that reject every value.
58
59 JSON Schema (draft-06+) allows the literal ``false`` anywhere a schema is
60 expected, meaning "no value is valid". This marker stands in for it during
61 conversion so combiners can tell an always-rejecting member apart from an
62 absent one, and so the exact validator can turn it back into ``false``.
63 The literal ``true`` ("any value is valid") simply becomes ``t.Any``.
64 """
65
66 @staticmethod
67 def _reject(_value: t.Any) -> t.NoReturn:
68 raise ValueError("schema is unsatisfiable (JSON Schema `false`)")
69
70 @classmethod
71 def __get_pydantic_core_schema__(
72 cls,
73 _source_type: t.Any,
74 _handler: t.Any,
75 ) -> core_schema.CoreSchema:
76 return core_schema.no_info_plain_validator_function(cls._reject)
77
78 @classmethod
79 def __get_pydantic_json_schema__(
80 cls,
81 _core_schema: core_schema.CoreSchema,
82 _handler: t.Any,
83 ) -> t.Dict[str, t.Any]:
84 return {"not": {}}
85
86
87 def _is_unsatisfiable_schema(schema: t.Any) -> bool:
88 return schema is _UnsatisfiableSchema
89
90
91 # Type mapping for simple cases where we don't need full model creation
92 PYDANTIC_TYPE_TO_PYTHON_TYPE = {
93 "string": str,
94 "integer": int,
95 "number": float,
96 "boolean": bool,
97 "array": t.List,
98 "object": t.Dict,
99 "null": type(None),
100 }
101
102 CONTAINER_TYPE = ("array", "object")
103
104 # Keywords that constrain which instances a schema accepts. A schema whose keys
105 # are all outside this set (title, description, default, examples, ...) is pure
106 # annotation and accepts every value, exactly like the empty schema.
107 _ASSERTION_KEYWORDS = frozenset(
108 {
109 "type",
110 "enum",
111 "const",
112 "anyOf",
113 "allOf",
114 "oneOf",
115 "not",
116 "if",
117 "then",
118 "else",
119 "$ref",
120 "properties",
121 "patternProperties",
122 "additionalProperties",
123 "propertyNames",
124 "required",
125 "dependencies",
126 "minProperties",
127 "maxProperties",
128 "items",
129 "additionalItems",
130 "contains",
131 "minItems",
132 "maxItems",
133 "uniqueItems",
134 "minLength",
135 "maxLength",
136 "pattern",
137 "minimum",
138 "maximum",
139 "exclusiveMinimum",
140 "exclusiveMaximum",
141 "multipleOf",
142 }
143 )
144
145 _SCALAR_CONSTRAINT_KEYWORDS = frozenset(
146 {
147 "minLength",
148 "maxLength",
149 "pattern",
150 "minimum",
151 "maximum",
152 "exclusiveMinimum",
153 "exclusiveMaximum",
154 "multipleOf",
155 }
156 )
157
158 _STRUCTURAL_KEYWORDS = frozenset(
159 {"anyOf", "allOf", "oneOf", "not", "if", "$ref", "properties", "items"}
160 )
161
162 _TYPELESS_TYPE_SCOPED_KEYWORDS = frozenset(
163 {
164 "properties",
165 "patternProperties",
166 "additionalProperties",
167 "propertyNames",
168 "required",
169 "dependencies",
170 "minProperties",
171 "maxProperties",
172 "items",
173 "additionalItems",
174 "contains",
175 "minItems",
176 "maxItems",
177 "uniqueItems",
178 }
179 )
180
181 # Keywords each type-array member keeps when the array is rewritten to anyOf.
182 _TYPE_SCOPED_KEYWORDS: t.Dict[str, frozenset] = {
183 "string": frozenset({"minLength", "maxLength", "pattern"}),
184 "integer": frozenset(
185 {"minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum", "multipleOf"}
186 ),
187 "number": frozenset(
188 {"minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum", "multipleOf"}
189 ),
190 "array": frozenset(
191 {
192 "items",
193 "additionalItems",
194 "contains",
195 "minItems",
196 "maxItems",
197 "uniqueItems",
198 }
199 ),
200 "object": frozenset(
201 {
202 "properties",
203 "patternProperties",
204 "additionalProperties",
205 "propertyNames",
206 "required",
207 "dependencies",
208 "minProperties",
209 "maxProperties",
210 }
211 ),
212 "boolean": frozenset(),
213 "null": frozenset(),
214 }
215
216 _TYPE_ARRAY_SHARED_KEYWORDS = frozenset({"enum", "const", "format"})
217 _TYPE_ARRAY_WRAPPER_KEYWORDS = frozenset(
218 {"title", "description", "default", "examples"}
219 )
220
221 # Should be deprecated,
222 # required values will always be provided by users
223 # Non-required values are nullable(None) if default value not provided.
224 FALLBACK_VALUES = {
225 "string": "",
226 "number": 0.0,
227 "integer": 0,
228 "boolean": False,
229 "object": {},
230 "array": [],
231 "null": None,
232 }
233
234
235 def _filter_boolean_schemas(
236 schema: t.Union[t.Dict[str, t.Any], bool, t.List],
237 ) -> t.Any:
238 """
239 Pre-filter boolean schemas from anyOf/allOf/oneOf arrays.
240
241 JSON Schema draft-06+ allows `true` and `false` as valid schemas:
242 - `true` means "accept any value" (equivalent to {})
243 - `false` means "reject all values" (equivalent to {"not": {}})
244
245 The json-schema-to-pydantic library doesn't handle these, so we:
246 - Replace `true` with {} (empty schema, accepts anything)
247 - Drop `false` from union combiners (`anyOf`/`oneOf`)
248 - Propagate an unsatisfiable marker through conjunctions and nested schemas
249
250 `None` remains the standalone-`false` filter sentinel for compatibility.
251 Composed schemas use `_UnsatisfiableSchema` so callers can distinguish
252 "drop this union branch" from "reject every value."
253 """
254 if isinstance(schema, bool):
255 if schema:
256 # true -> empty schema (accepts any value)
257 return {}
258 else:
259 # false -> reject all, return None to filter out
260 return None
261
262 if isinstance(schema, list):
263 # Filter list items (e.g., for anyOf arrays)
264 filtered = []
265 for item in schema:
266 result = _filter_boolean_schemas(item)
267 if result is not None and not _is_unsatisfiable_schema(result):
268 filtered.append(result)
269 return filtered if filtered else None
270
271 if not isinstance(schema, dict):
272 return schema
273
274 # A schema that enumerates no acceptable values rejects everything, exactly
275 # like `false`. Normalizing here routes both spellings through the same
276 # unsatisfiable-property handling (which tolerates the property's absence).
277 if schema.get("enum") == [] or schema.get("type") == []:
278 return _UnsatisfiableSchema
279
280 # Make a copy to avoid mutating the original
281 result = {}
282 for key, value in schema.items():
283 if key == "allOf" and isinstance(value, list):
284 filtered_members = [_filter_boolean_schemas(member) for member in value]
285 if any(
286 member is None or _is_unsatisfiable_schema(member)
287 for member in filtered_members
288 ):
289 return _UnsatisfiableSchema
290 result[key] = filtered_members
291 elif key in ("anyOf", "oneOf") and isinstance(value, list):
292 filtered_members = []
293 for member in value:
294 filtered_member = _filter_boolean_schemas(member)
295 if filtered_member is not None and not _is_unsatisfiable_schema(
296 filtered_member
297 ):
298 filtered_members.append(filtered_member)
299 if not filtered_members:
300 return _UnsatisfiableSchema
301 result[key] = filtered_members
302 elif key == "items" and isinstance(value, (dict, bool)):
303 # Handle array items schema
304 filtered_items = _filter_boolean_schemas(value)
305 result[key] = (
306 _UnsatisfiableSchema if filtered_items is None else filtered_items
307 )
308 elif key == "properties" and isinstance(value, dict):
309 # Recursively filter property schemas
310 filtered_props = {}
311 for prop_name, prop_schema in value.items():
312 filtered_prop = _filter_boolean_schemas(prop_schema)
313 filtered_props[prop_name] = (
314 _UnsatisfiableSchema if filtered_prop is None else filtered_prop
315 )
316 result[key] = filtered_props
317 elif key in ("$defs", "definitions") and isinstance(value, dict):
318 # Recursively filter definitions
319 filtered_defs = {}
320 for def_name, def_schema in value.items():
321 filtered_def = _filter_boolean_schemas(def_schema)
322 filtered_defs[def_name] = (
323 _UnsatisfiableSchema if filtered_def is None else filtered_def
324 )
325 result[key] = filtered_defs
326 else:
327 result[key] = value
328
329 return result
330
331
332 def _resolve_unsatisfiable_references(schema: t.Any) -> t.Any:
333 """Replace local references to unsatisfiable definitions with the marker."""
334 if not isinstance(schema, dict):
335 return schema
336
337 unsatisfiable_refs = set()
338 for definitions_key in ("$defs", "definitions"):
339 definitions = schema.get(definitions_key)
340 if not isinstance(definitions, dict):
341 continue
342 for name, definition in definitions.items():
343 if _is_unsatisfiable_schema(definition):
344 unsatisfiable_refs.add(f"#/{definitions_key}/{name}")
345
346 if not unsatisfiable_refs:
347 return schema
348
349 def replace(value: t.Any) -> t.Any:
350 if isinstance(value, list):
351 return [replace(item) for item in value]
352 if not isinstance(value, dict):
353 return value
354 if value.get("$ref") in unsatisfiable_refs:
355 return _UnsatisfiableSchema
356
357 replaced = {}
358 for key, item in value.items():
359 if key in ("$defs", "definitions") and isinstance(item, dict):
360 replaced[key] = {
361 name: replace(definition)
362 for name, definition in item.items()
363 if not _is_unsatisfiable_schema(definition)
364 }
365 else:
366 replaced[key] = replace(item)
367 return replaced
368
369 return replace(schema)
370
371
372 def _contains_unsatisfiable_schema(schema: t.Any) -> bool:
373 if _is_unsatisfiable_schema(schema):
374 return True
375 if isinstance(schema, list):
376 return any(_contains_unsatisfiable_schema(item) for item in schema)
377 if isinstance(schema, dict):
378 return any(_contains_unsatisfiable_schema(value) for value in schema.values())
379 return False
380
381
382 class _DynamicKeyValidator(t.NamedTuple):
383 """Exact JSON Schema validation with optional default materialization."""
384
385 validator: Validator
386 materializer: t.Optional[TypeAdapter]
387
388 def validate(self, value: t.Any) -> None:
389 error = next(self.validator.iter_errors(value), None)
390 if error is not None:
391 raise ValueError(error.message)
392
393 def materialize(self, value: t.Any) -> t.Any:
394 if self.materializer is None:
395 return value
396 try:
397 return _materialized_value_to_python(
398 self.materializer.validate_python(_materialized_value_to_python(value))
399 )
400 except (TypeError, ValueError):
401 # JSON Schema alone decides acceptance. Pydantic is only retained
402 # for the existing default-materialization behavior, and an
403 # incomplete Pydantic representation must not reject valid input.
404 logger.debug("Could not materialize dynamic-key defaults; preserving input")
405 return value
406
407
408 def _materialized_value_to_python(value: t.Any) -> t.Any:
409 """Convert a previous materializer result back into validation input."""
410 if isinstance(value, BaseModel):
411 included_fields = set(value.model_fields_set)
412 included_fields.update(
413 getattr(type(value), _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE, ())
414 )
415 result = {}
416 for name, field in type(value).model_fields.items():
417 if name not in included_fields:
418 continue
419 alias = field.serialization_alias or field.alias
420 output_name = alias if isinstance(alias, str) else name
421 result[output_name] = _materialized_value_to_python(getattr(value, name))
422 for name, item in (value.model_extra or {}).items():
423 result[name] = _materialized_value_to_python(item)
424 return result
425 if isinstance(value, dict):
426 return {key: _materialized_value_to_python(item) for key, item in value.items()}
427 if isinstance(value, (list, tuple)):
428 return [_materialized_value_to_python(item) for item in value]
429 return value
430
431
432 class _ObjectPolicy(t.NamedTuple):
433 """Compiled dynamic-key policy for one JSON Schema object node.
434
435 This is the single place the covered object contract lives, so
436 ``json_schema_to_model`` and ``json_schema_to_pydantic_type`` cannot drift
437 apart on which keys are accepted, validated, or preserved.
438 """
439
440 declared: t.FrozenSet[str]
441 # Key matchers come from `_linear_pattern_matcher`, never Python `re`: keys
442 # are attacker-influenced and the schema's patterns are untrusted.
443 patterns: t.Tuple[t.Tuple[t.Callable[[str], bool], _DynamicKeyValidator], ...]
444 additional: t.Optional[_DynamicKeyValidator]
445 rejects_unmatched: bool
446
447 @property
448 def is_trivial(self) -> bool:
449 """No dynamic keys to police and nothing to reject."""
450 return (
451 not self.patterns and not self.rejects_unmatched and self.additional is None
452 )
453
454
455 def object_is_open(schema: t.Dict[str, t.Any]) -> bool:
456 """Whether an object schema accepts arbitrary keys it says nothing about.
457
458 An object is open when it declares no named properties and either omits
459 ``additionalProperties`` or sets it to ``true``. Objects that *do* name
460 properties stay strict on omission, matching the TypeScript converters.
461 """
462 additional = schema.get("additionalProperties", _MISSING)
463 return not (schema.get("properties") or {}) and additional in (_MISSING, True)
464
465
466 def _contains_default(schema: t.Any) -> bool:
467 """Whether Pydantic must run after validation to materialize a default."""
468 if isinstance(schema, list):
469 return any(_contains_default(item) for item in schema)
470 if not isinstance(schema, dict):
471 return False
472 return "default" in schema or any(
473 _contains_default(value) for value in schema.values()
474 )
475
476
477 def _resolve_local_json_pointer(
478 reference: str,
479 root_schema: t.Dict[str, t.Any],
480 ) -> t.Any:
481 """Resolve a local JSON Pointer or fail while the model is constructed."""
482 if reference == "#":
483 return root_schema
484 if not reference.startswith("#/"):
485 raise ValueError(
486 f"Dynamic-key schema reference {reference!r} must be a local JSON Pointer"
487 )
488
489 current: t.Any = root_schema
490 for raw_token in unquote(reference[2:]).split("/"):
491 token = raw_token.replace("~1", "/").replace("~0", "~")
492 if isinstance(current, dict) and token in current:
493 current = current[token]
494 continue
495 if isinstance(current, list):
496 try:
497 current = current[int(token)]
498 continue
499 except (ValueError, IndexError):
500 pass
501 raise ValueError(f"Unresolvable dynamic-key schema reference: {reference!r}")
502 return current
503
504
505 def _resolve_default_metadata_schema(
506 schema: t.Any,
507 root_schema: t.Dict[str, t.Any],
508 visited_refs: t.Optional[t.Set[str]] = None,
509 ) -> t.Any:
510 """Resolve a schema node without traversing instance-valued annotations."""
511 if not isinstance(schema, dict):
512 return schema
513 reference = schema.get("$ref")
514 if not isinstance(reference, str) or not reference.startswith("#/"):
515 return schema
516 if visited_refs is None:
517 visited_refs = set()
518 if reference in visited_refs:
519 return {key: value for key, value in schema.items() if key != "$ref"}
520 visited_refs.add(reference)
521
522 target = _resolve_local_json_pointer(reference, root_schema)
523 if not isinstance(target, dict):
524 return schema
525 resolved = _resolve_default_metadata_schema(target, root_schema, visited_refs)
526 if not isinstance(resolved, dict):
527 return schema
528 return {
529 **resolved,
530 **{key: value for key, value in schema.items() if key != "$ref"},
531 }
532
533
534 def _default_metadata_properties(
535 schema: t.Any,
536 root_schema: t.Dict[str, t.Any],
537 ) -> t.Dict[str, t.Any]:
538 schema = _resolve_default_metadata_schema(schema, root_schema)
539 if not isinstance(schema, dict) or "anyOf" in schema or "oneOf" in schema:
540 return {}
541 properties = schema.get("properties")
542 result = dict(properties) if isinstance(properties, dict) else {}
543 for option in schema.get("allOf", []):
544 result.update(_default_metadata_properties(option, root_schema))
545 return result
546
547
548 def _default_metadata_schema_for_model(
549 schema: t.Any,
550 model: t.Type[BaseModel],
551 root_schema: t.Dict[str, t.Any],
552 ) -> t.Any:
553 schema = _resolve_default_metadata_schema(schema, root_schema)
554 if not isinstance(schema, dict):
555 return None
556 for combiner in ("anyOf", "oneOf"):
557 titled = [
558 option
559 for option in schema.get(combiner, [])
560 if isinstance(option, dict) and option.get("title") == model.__name__
561 ]
562 if len(titled) == 1:
563 return titled[0]
564 return schema if "anyOf" not in schema and "oneOf" not in schema else None
565
566
567 def _default_metadata_field_aliases(
568 model: t.Type[BaseModel],
569 ) -> t.Dict[str, t.Tuple[str, t.Any]]:
570 fields = {}
571 for name, field in model.model_fields.items():
572 alias = field.serialization_alias or field.alias
573 fields[alias if isinstance(alias, str) else name] = (name, field)
574 return fields
575
576
577 def _mark_explicit_default_fields(
578 annotation: t.Any,
579 schema: t.Any,
580 root_schema: t.Dict[str, t.Any],
581 visiting: t.Optional[t.Set[t.Type[BaseModel]]] = None,
582 ) -> None:
583 """Attach recursive default-presence metadata to generated model classes."""
584 origin = t.get_origin(annotation)
585 arguments = t.get_args(annotation)
586 if origin is t.Annotated:
587 # Exact-validation wrappers must not hide the materialized type below.
588 _mark_explicit_default_fields(arguments[0], schema, root_schema, visiting)
589 return
590 if origin in (t.Union, types.UnionType):
591 options: t.List[t.Dict[str, t.Any]] = []
592 resolved_schema = _resolve_default_metadata_schema(schema, root_schema)
593 if isinstance(resolved_schema, dict):
594 for combiner in ("anyOf", "oneOf"):
595 options.extend(
596 option
597 for option in resolved_schema.get(combiner, [])
598 if isinstance(option, dict)
599 )
600 for index, argument in enumerate(arguments):
601 argument_schema = None
602 if isinstance(argument, type) and issubclass(argument, BaseModel):
603 titled = [
604 option
605 for option in options
606 if option.get("title") == argument.__name__
607 ]
608 if len(titled) == 1:
609 argument_schema = titled[0]
610 if argument_schema is None and index < len(options):
611 argument_schema = options[index]
612 _mark_explicit_default_fields(
613 argument,
614 argument_schema,
615 root_schema,
616 visiting,
617 )
618 return
619
620 if origin in (list, set, frozenset):
621 resolved_schema = _resolve_default_metadata_schema(schema, root_schema)
622 item_schema = (
623 resolved_schema.get("items") if isinstance(resolved_schema, dict) else None
624 )
625 if arguments:
626 _mark_explicit_default_fields(
627 arguments[0],
628 item_schema,
629 root_schema,
630 visiting,
631 )
632 return
633
634 if origin is tuple:
635 resolved_schema = _resolve_default_metadata_schema(schema, root_schema)
636 items = (
637 resolved_schema.get("items") if isinstance(resolved_schema, dict) else None
638 )
639 for index, argument in enumerate(arguments):
640 item_schema = (
641 items[index]
642 if isinstance(items, list) and index < len(items)
643 else items
644 if isinstance(items, dict)
645 else None
646 )
647 _mark_explicit_default_fields(
648 argument,
649 item_schema,
650 root_schema,
651 visiting,
652 )
653 return
654
655 if origin is dict:
656 resolved_schema = _resolve_default_metadata_schema(schema, root_schema)
657 additional = (
658 resolved_schema.get("additionalProperties")
659 if isinstance(resolved_schema, dict)
660 else None
661 )
662 if len(arguments) == 2:
663 _mark_explicit_default_fields(
664 arguments[1],
665 additional,
666 root_schema,
667 visiting,
668 )
669 return
670
671 if not isinstance(annotation, type) or not issubclass(annotation, BaseModel):
672 return
673
674 model_schema = _default_metadata_schema_for_model(
675 schema,
676 annotation,
677 root_schema,
678 )
679 properties = _default_metadata_properties(model_schema, root_schema)
680 fields = _default_metadata_field_aliases(annotation)
681 explicit_defaults = {
682 internal_name
683 for alias, (internal_name, field) in fields.items()
684 if (not field.is_required() and field.default is not None)
685 or (isinstance(properties.get(alias), dict) and "default" in properties[alias])
686 }
687 explicit_defaults.update(
688 getattr(annotation, _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE, ())
689 )
690 setattr(
691 annotation,
692 _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE,
693 frozenset(explicit_defaults),
694 )
695
696 if visiting is None:
697 visiting = set()
698 if annotation in visiting:
699 return
700 visiting.add(annotation)
701 try:
702 for alias, (_, field) in fields.items():
703 _mark_explicit_default_fields(
704 field.annotation,
705 properties.get(alias),
706 root_schema,
707 visiting,
708 )
709 finally:
710 visiting.remove(annotation)
711
712
713 # Draft 7 keywords whose value is itself a schema. `items` and `dependencies`
714 # are handled separately because they also accept non-schema forms.
715 _SCHEMA_VALUED_KEYWORDS = frozenset(
716 {
717 "additionalItems",
718 "additionalProperties",
719 "contains",
720 "else",
721 "if",
722 "not",
723 "propertyNames",
724 "then",
725 }
726 )
727
728 # Draft 7 keywords whose value is a list of schemas.
729 _SCHEMA_LIST_KEYWORDS = frozenset({"allOf", "anyOf", "oneOf"})
730
731 # Draft 7 keywords whose value maps names to schemas.
732 _SCHEMA_MAP_KEYWORDS = frozenset(
733 {"$defs", "definitions", "patternProperties", "properties"}
734 )
735
736
737 def _check_dynamic_references(
738 schema: t.Any,
739 root_schema: t.Dict[str, t.Any],
740 checked_references: t.Optional[t.Set[str]] = None,
741 ) -> None:
742 """Reject external, anchored, and unresolved references before validation.
743
744 Only schema positions are walked. ``const``, ``default``, ``enum``, and
745 ``examples`` hold instance data, so a ``$ref``-shaped value stored there is
746 a payload rather than a reference and must not block tool wrapping.
747 """
748 if checked_references is None:
749 checked_references = set()
750 if isinstance(schema, list):
751 for item in schema:
752 _check_dynamic_references(item, root_schema, checked_references)
753 return
754 if not isinstance(schema, dict):
755 return
756
757 reference = schema.get("$ref")
758 if reference is not None:
759 if not isinstance(reference, str):
760 raise ValueError("Dynamic-key schema `$ref` must be a string")
761 resolved = _resolve_local_json_pointer(reference, root_schema)
762 if not isinstance(resolved, (dict, bool)):
763 raise ValueError(
764 f"Dynamic-key schema reference {reference!r} does not target a schema"
765 )
766 if reference not in checked_references:
767 checked_references.add(reference)
768 _check_dynamic_references(resolved, root_schema, checked_references)
769
770 for keyword, value in schema.items():
771 if keyword in _SCHEMA_VALUED_KEYWORDS or keyword in _SCHEMA_LIST_KEYWORDS:
772 _check_dynamic_references(value, root_schema, checked_references)
773 elif keyword in _SCHEMA_MAP_KEYWORDS:
774 if isinstance(value, dict):
775 for entry in value.values():
776 _check_dynamic_references(entry, root_schema, checked_references)
777 elif keyword == "items":
778 # A schema, or a list of schemas for tuple validation.
779 _check_dynamic_references(value, root_schema, checked_references)
780 elif keyword == "dependencies":
781 # Each entry is either a schema or a list of required property names.
782 if isinstance(value, dict):
783 for entry in value.values():
784 if isinstance(entry, (dict, bool)):
785 _check_dynamic_references(
786 entry, root_schema, checked_references
787 )
788
789
790 def _validate_dynamic_key_schema(
791 schema: t.Any,
792 root_schema: t.Dict[str, t.Any],
793 root_validator: Validator,
794 ) -> None:
795 """Validate one dynamic-key subschema without constructing a materializer."""
796 validator_type = type(root_validator)
797 validator_type.check_schema(schema)
798 _check_dynamic_references(schema, root_schema)
799
800
801 def _dynamic_key_validator(
802 schema: t.Any,
803 root_schema: t.Dict[str, t.Any],
804 root_validator: Validator,
805 ) -> _DynamicKeyValidator:
806 """Compile a complete inline JSON subschema without coercive acceptance."""
807 _validate_dynamic_key_schema(schema, root_schema, root_validator)
808
809 materializer = None
810 if _contains_default(schema):
811 annotation = json_schema_to_pydantic_type(schema, root_schema=root_schema)
812 _mark_explicit_default_fields(annotation, schema, root_schema)
813 materializer = TypeAdapter(annotation)
814
815 return _DynamicKeyValidator(
816 validator=root_validator.evolve(schema=schema),
817 materializer=materializer,
818 )
819
820
821 def _compile_pattern_property(pattern: str) -> t.Callable[[str], bool]:
822 """Compile a `patternProperties` key on the linear-time engine only.
823
824 A key pattern decides which subschema validates a value, so it cannot be
825 dropped the way an unenforceable `pattern` constraint is. Patterns that
826 engine rejects (look-around, backreferences) fail conversion instead of
827 running on Python's backtracking `re` against attacker-chosen keys.
828 """
829 matcher = _linear_pattern_matcher(pattern)
830 if matcher is not None:
831 return matcher
832 try:
833 re.compile(pattern)
834 except (re.error, OverflowError, RecursionError, TypeError) as exc:
835 raise ValueError(
836 f"Invalid patternProperties regular expression: {pattern!r}"
837 ) from exc
838 raise ValueError(_unsupported_pattern_property_message(pattern))
839
840
841 def _unsupported_pattern_property_message(pattern: t.Any) -> str:
842 return (
843 f"Unsupported patternProperties regular expression: {pattern!r}; "
844 "only patterns the linear-time regex engine accepts are supported "
845 "(look-around and backreferences can backtrack catastrophically)"
846 )
847
848
849 def _validate_object_policy_schema(
850 schema: t.Dict[str, t.Any],
851 root_schema: t.Dict[str, t.Any],
852 ) -> None:
853 """Validate a dynamic object policy without materializing defaults."""
854 validator_type = _with_safe_regex_keywords(
855 jsonschema_validators.validator_for(
856 root_schema,
857 default=jsonschema_validators.Draft7Validator,
858 )
859 )
860 root_validator = validator_type(root_schema)
861
862 for pattern, pattern_schema in (schema.get("patternProperties") or {}).items():
863 _compile_pattern_property(pattern)
864 _validate_dynamic_key_schema(
865 pattern_schema,
866 root_schema,
867 root_validator,
868 )
869
870 additional = schema.get("additionalProperties")
871 if isinstance(additional, dict):
872 _validate_dynamic_key_schema(additional, root_schema, root_validator)
873
874
875 def _compile_object_policy(
876 schema: t.Dict[str, t.Any],
877 root_schema: t.Optional[t.Dict[str, t.Any]] = None,
878 ) -> _ObjectPolicy:
879 document_root = root_schema if root_schema is not None else schema
880 declared = frozenset(schema.get("properties") or {})
881 additional = schema.get("additionalProperties", _MISSING)
882 validator_type = _with_safe_regex_keywords(
883 jsonschema_validators.validator_for(
884 document_root,
885 default=jsonschema_validators.Draft7Validator,
886 )
887 )
888 root_validator = validator_type(document_root)
889
890 patterns = []
891 for pattern, pattern_schema in (schema.get("patternProperties") or {}).items():
892 patterns.append(
893 (
894 _compile_pattern_property(pattern),
895 _dynamic_key_validator(
896 pattern_schema,
897 document_root,
898 root_validator,
899 ),
900 )
901 )
902
903 return _ObjectPolicy(
904 declared=declared,
905 patterns=tuple(patterns),
906 additional=(
907 _dynamic_key_validator(additional, document_root, root_validator)
908 if isinstance(additional, dict)
909 else None
910 ),
911 rejects_unmatched=additional is False
912 or (additional is _MISSING and bool(declared)),
913 )
914
915
916 def _iter_reachable_schemas(
917 schema: t.Any,
918 root_schema: t.Dict[str, t.Any],
919 visited: t.Optional[t.Set[t.Tuple[int, t.Any]]] = None,
920 validator_class: t.Any = None,
921 ) -> t.Iterator[t.Dict[str, t.Any]]:
922 """Yield schema nodes and local targets under their effective dialect."""
923 if visited is None:
924 visited = set()
925 if validator_class is None:
926 validator_class = jsonschema_validators.validator_for(
927 root_schema, default=jsonschema_validators.Draft7Validator
928 )
929 if isinstance(schema, list):
930 for item in schema:
931 yield from _iter_reachable_schemas(
932 item, root_schema, visited, validator_class
933 )
934 return
935 if not isinstance(schema, dict):
936 return
937 if "$schema" in schema:
938 validator_class = jsonschema_validators.validator_for(
939 schema, default=validator_class
940 )
941 visit_key = (id(schema), validator_class)
942 if visit_key in visited:
943 return
944 visited.add(visit_key)
945 yield schema
946
947 reference = schema.get("$ref")
948 if isinstance(reference, str) and (reference == "#" or reference.startswith("#/")):
949 try:
950 resolved = _resolve_local_json_pointer(reference, root_schema)
951 except ValueError:
952 # Ordinary references retain the converter's legacy fallback.
953 # Dynamic-key references are checked when their owning policy is
954 # validated.
955 resolved = None
956 if isinstance(resolved, (dict, list)):
957 yield from _iter_reachable_schemas(
958 resolved, root_schema, visited, validator_class
959 )
960
961 for keyword in ("properties", "patternProperties", "dependentSchemas"):
962 if keyword == "dependentSchemas" and keyword not in validator_class.VALIDATORS:
963 continue
964 values = schema.get(keyword)
965 if isinstance(values, dict):
966 for child in values.values():
967 yield from _iter_reachable_schemas(
968 child, root_schema, visited, validator_class
969 )
970
971 value_keywords = _SCHEMA_VALUED_KEYWORDS | (
972 {"unevaluatedItems", "unevaluatedProperties"}
973 & validator_class.VALIDATORS.keys()
974 )
975 for keyword in value_keywords:
976 child = schema.get(keyword)
977 if isinstance(child, (dict, list)):
978 yield from _iter_reachable_schemas(
979 child, root_schema, visited, validator_class
980 )
981
982 list_keywords = _SCHEMA_LIST_KEYWORDS | (
983 {"prefixItems"} & validator_class.VALIDATORS.keys()
984 )
985 for keyword in list_keywords:
986 children = schema.get(keyword)
987 if isinstance(children, list):
988 yield from _iter_reachable_schemas(
989 children, root_schema, visited, validator_class
990 )
991
992 items = schema.get("items")
993 if isinstance(items, (dict, list)):
994 yield from _iter_reachable_schemas(items, root_schema, visited, validator_class)
995
996 dependencies = schema.get("dependencies")
997 if isinstance(dependencies, dict):
998 for child in dependencies.values():
999 if isinstance(child, (dict, bool)):
1000 yield from _iter_reachable_schemas(
1001 child, root_schema, visited, validator_class
1002 )
1003
1004
1005 def _has_dynamic_object_policy(schema: t.Dict[str, t.Any]) -> bool:
1006 return bool(schema.get("patternProperties")) or isinstance(
1007 schema.get("additionalProperties"), dict
1008 )
1009
1010
1011 def _validate_dynamic_object_policies(
1012 schema: t.Any,
1013 root_schema: t.Dict[str, t.Any],
1014 ) -> None:
1015 """Validate every reachable dynamic object policy before conversion fallback."""
1016 for candidate in _iter_reachable_schemas(schema, root_schema):
1017 if _has_dynamic_object_policy(candidate):
1018 _validate_object_policy_schema(candidate, root_schema)
1019
1020
1021 def _contains_inline_object_policy(
1022 schema: t.Any,
1023 visited: t.Optional[t.Set[int]] = None,
1024 ) -> bool:
1025 """Whether direct object or array nesting contains a dynamic policy."""
1026 if visited is None:
1027 visited = set()
1028 if isinstance(schema, list):
1029 return any(_contains_inline_object_policy(item, visited) for item in schema)
1030 if not isinstance(schema, dict) or id(schema) in visited:
1031 return False
1032 visited.add(id(schema))
1033 if _has_dynamic_object_policy(schema):
1034 return True
1035
1036 properties = schema.get("properties")
1037 if isinstance(properties, dict) and any(
1038 _contains_inline_object_policy(child, visited) for child in properties.values()
1039 ):
1040 return True
1041
1042 items = schema.get("items")
1043 return isinstance(items, (dict, list)) and _contains_inline_object_policy(
1044 items,
1045 visited,
1046 )
1047
1048
1049 def _apply_nested_object_policies(
1050 schema: t.Dict[str, t.Any],
1051 base_model: t.Type[BaseModel],
1052 root_schema: t.Dict[str, t.Any],
1053 ) -> t.Type[BaseModel]:
1054 """Replace fields whose schemas contain dynamic object policies."""
1055 field_overrides = {}
1056 for name, property_schema in (schema.get("properties") or {}).items():
1057 if not _contains_inline_object_policy(property_schema):
1058 continue
1059 field = base_model.model_fields.get(name)
1060 if field is None:
1061 continue
1062 field_overrides[name] = (
1063 _filtered_schema_to_pydantic_type(
1064 property_schema,
1065 root_schema=root_schema,
1066 ),
1067 field,
1068 )
1069
1070 if not field_overrides:
1071 return base_model
1072
1073 return create_pydantic_model( # type: ignore[call-overload]
1074 schema.get("title", "GeneratedModel"),
1075 __base__=base_model,
1076 **field_overrides,
1077 )
1078
1079
1080 class _DynamicObjectModel(BaseModel):
1081 """Base for object models that must police and preserve dynamic keys.
1082
1083 ``extra="allow"`` is what makes preservation possible at all: Pydantic's
1084 default silently discards accepted arguments, so a provider that re-reads
1085 them (``LangchainProvider`` uses ``getattr``) would lose valid payloads.
1086 Materialized defaults are written back into ``__pydantic_extra__`` so they
1087 show up in ``model_dump()`` and remain readable as attributes.
1088 """
1089
1090 model_config = ConfigDict(extra="allow")
1091
1092 @model_validator(mode="before")
1093 @classmethod
1094 def _validate_object_policy(cls, value: t.Any) -> t.Any:
1095 policy: t.Optional[_ObjectPolicy] = getattr(
1096 cls, "__composio_object_policy__", None
1097 )
1098 if policy is None or policy.is_trivial or not isinstance(value, dict):
1099 return value
1100
1101 unrecognized = []
1102 for key, item in value.items():
1103 matched = False
1104 for matches_key, validator in policy.patterns:
1105 if matches_key(key):
1106 matched = True
1107 validator.validate(item)
1108
1109 if matched or key in policy.declared:
1110 continue
1111
1112 if policy.rejects_unmatched:
1113 unrecognized.append(key)
1114 elif policy.additional is not None:
1115 policy.additional.validate(item)
1116
1117 if unrecognized:
1118 raise ValueError(
1119 f"Unrecognized key(s) in object: {', '.join(repr(key) for key in unrecognized)}"
1120 )
1121
1122 return value
1123
1124 @model_validator(mode="after")
1125 def _materialize_dynamic_defaults(self):
1126 policy: t.Optional[_ObjectPolicy] = getattr(
1127 type(self), "__composio_object_policy__", None
1128 )
1129 if policy is None or policy.is_trivial:
1130 return self
1131
1132 extra = self.__pydantic_extra__
1133 if not extra:
1134 return self
1135
1136 for key, value in list(extra.items()):
1137 matched = False
1138 for matches_key, validator in policy.patterns:
1139 if matches_key(key):
1140 matched = True
1141 extra[key] = validator.materialize(extra[key])
1142
1143 if matched or key in policy.declared:
1144 continue
1145
1146 if policy.additional is not None:
1147 extra[key] = policy.additional.materialize(value)
1148
1149 return self
1150
1151
1152 def apply_object_policy(
1153 schema: t.Dict[str, t.Any],
1154 base_model: t.Type[BaseModel],
1155 model_name: t.Optional[str] = None,
1156 *,
1157 root_schema: t.Optional[t.Dict[str, t.Any]] = None,
1158 ) -> t.Type[BaseModel]:
1159 """Wrap ``base_model`` with the covered dynamic-key policy for ``schema``."""
1160 policy = _compile_object_policy(schema, root_schema)
1161 name = model_name or base_model.__name__
1162 json_schema_extra: t.Dict[str, t.Any] = {}
1163 if "patternProperties" in schema:
1164 json_schema_extra["patternProperties"] = schema["patternProperties"]
1165 if "additionalProperties" in schema:
1166 json_schema_extra["additionalProperties"] = schema["additionalProperties"]
1167 elif policy.rejects_unmatched:
1168 json_schema_extra["additionalProperties"] = False
1169
1170 return t.cast(
1171 t.Type[BaseModel],
1172 type(
1173 name,
1174 (_DynamicObjectModel, base_model),
1175 {
1176 "__module__": __name__,
1177 "__qualname__": name,
1178 "model_config": ConfigDict(
1179 extra="allow",
1180 json_schema_extra=json_schema_extra,
1181 ),
1182 "__composio_object_policy__": policy,
1183 },
1184 ),
1185 )
1186
1187
1188 def json_schema_to_pydantic_type(
1189 json_schema: t.Union[t.Dict[str, t.Any], bool],
1190 *,
1191 root_schema: t.Optional[t.Dict[str, t.Any]] = None,
1192 ) -> t.Union[t.Type, t.Optional[t.Any]]:
1193 """
1194 Converts a JSON schema type to a Pydantic type.
1195
1196 Uses json-schema-to-pydantic for complex schemas (anyOf, allOf, oneOf),
1197 falls back to simple type mapping for primitive types.
1198
1199 :param json_schema: The JSON schema to convert (can be dict or boolean).
1200 :param root_schema: Full schema document used to resolve nested local references.
1201 :return: A Pydantic type.
1202 """
1203 # A schema may be the literal `true` or `false` instead of an object
1204 # (JSON Schema draft-06+), e.g. `additionalProperties: false` or a member
1205 # of `anyOf`. `true` accepts every value; `false` rejects every value.
1206 if isinstance(json_schema, bool):
1207 if json_schema:
1208 return t.Any
1209 return _UnsatisfiableSchema
1210
1211 # Pre-filter boolean schemas from combiners
1212 filtered_schema = _filter_boolean_schemas(json_schema)
1213 filtered_schema = _resolve_unsatisfiable_references(filtered_schema)
1214 document_root = root_schema if root_schema is not None else json_schema
1215 _validate_dynamic_object_policies(json_schema, document_root)
1216 annotation = _filtered_schema_to_pydantic_type(
1217 filtered_schema,
1218 root_schema=document_root,
1219 )
1220 # Two layers: Pydantic materializes the value (defaults, aliases, models),
1221 # while the source schema decides acceptance. Keywords Pydantic cannot
1222 # express get a Draft 7 "exact" check that runs before Pydantic coercion.
1223 # Generated models already carry that check (see `_with_exact_validation`),
1224 # so only non-model annotations are wrapped here. When Pydantic would
1225 # narrow a valid value (e.g. `$ref`, `allOf`), the value is kept as-is.
1226 if (
1227 _requires_whole_schema_validation(json_schema)
1228 and not _is_unsatisfiable_schema(annotation)
1229 and not (isinstance(annotation, type) and issubclass(annotation, BaseModel))
1230 ):
1231 return _with_exact_validation_annotation(
1232 t.Any if _needs_permissive_materialization(json_schema) else annotation,
1233 json_schema,
1234 document_root,
1235 )
1236 return annotation
1237
1238
1239 def _filtered_schema_to_pydantic_type(
1240 schema: t.Any,
1241 *,
1242 root_schema: t.Optional[t.Dict[str, t.Any]] = None,
1243 ) -> t.Type[t.Any]:
1244 """Convert a schema after boolean-schema normalization."""
1245 if schema is None or _is_unsatisfiable_schema(schema):
1246 return _UnsatisfiableSchema
1247
1248 if not _ASSERTION_KEYWORDS.intersection(schema):
1249 # Pure-annotation schemas (only title/description/default/...) accept
1250 # every value, exactly like the empty schema.
1251 return t.Any
1252
1253 schema_type = schema.get("type")
1254 if isinstance(schema_type, list):
1255 if not schema_type:
1256 return _UnsatisfiableSchema
1257 return _build_union_from_options(
1258 [
1259 {**schema, "type": member, "x-composio-strict-type": True}
1260 for member in schema_type
1261 ],
1262 root_schema=root_schema,
1263 )
1264
1265 if schema_type is None and (
1266 "enum" in schema
1267 or "const" in schema
1268 or (
1269 _SCALAR_CONSTRAINT_KEYWORDS.intersection(schema)
1270 and not _STRUCTURAL_KEYWORDS.intersection(schema)
1271 )
1272 ):
1273 return _apply_scalar_constraints(t.Any, schema)
1274
1275 # Handle simple primitive types without complex combiners
1276 if _is_simple_primitive(schema):
1277 return _convert_simple_type(schema)
1278
1279 # Use library for complex schemas (anyOf, allOf, oneOf, nested objects)
1280 return _convert_with_library(schema, root_schema=root_schema)
1281
1282
1283 def _is_simple_primitive(schema: t.Dict[str, t.Any]) -> bool:
1284 """Check if schema is a simple primitive without combiners."""
1285 has_combiners = any(k in schema for k in ("anyOf", "allOf", "oneOf"))
1286 has_properties = "properties" in schema
1287 schema_type = schema.get("type")
1288
1289 return (
1290 not has_combiners
1291 and not has_properties
1292 and schema_type in PYDANTIC_TYPE_TO_PYTHON_TYPE
1293 and schema_type not in CONTAINER_TYPE
1294 )
1295
1296
1297 def _matches_json_type(type_: str, value: t.Any) -> bool:
1298 """JSON type membership per Draft 7 (booleans are not numbers, 2.0 is an integer)."""
1299 if type_ == "string":
1300 return isinstance(value, str)
1301 if type_ == "boolean":
1302 return isinstance(value, bool)
1303 if type_ == "null":
1304 return value is None
1305 if type_ == "integer":
1306 if isinstance(value, bool):
1307 return False
1308 return isinstance(value, int) or (
1309 isinstance(value, float) and value.is_integer()
1310 )
1311 if type_ == "number":
1312 return isinstance(value, (int, float)) and not isinstance(value, bool)
1313 if type_ == "array":
1314 return isinstance(value, list)
1315 if type_ == "object":
1316 return isinstance(value, dict)
1317 return True
1318
1319
1320 def _json_type_guard(type_: str) -> t.Callable[[t.Any], t.Any]:
1321 def validate(value: t.Any) -> t.Any:
1322 if not _matches_json_type(type_, value):
1323 raise ValueError(f"value is not of JSON type {type_!r}")
1324 return value
1325
1326 return validate
1327
1328
1329 def _strict_json_type_annotation(type_: str) -> t.Any:
1330 """A type-checked annotation that still coerces integral floats for `integer`."""
1331 if type_ == "null":
1332 return type(None)
1333 base = PYDANTIC_TYPE_TO_PYTHON_TYPE.get(type_, str)
1334 return t.Annotated[base, BeforeValidator(_json_type_guard(type_))]
1335
1336
1337 def _convert_simple_type(schema: t.Dict[str, t.Any]) -> t.Type[t.Any]:
1338 """Convert simple primitive types directly."""
1339 type_ = schema.get("type", "string")
1340 use_strict_type = (
1341 schema.get("x-composio-strict-type") is True
1342 or "enum" in schema
1343 or "const" in schema
1344 )
1345 base_type = (
1346 _strict_json_type_annotation(type_)
1347 if use_strict_type
1348 else t.cast(t.Type[t.Any], PYDANTIC_TYPE_TO_PYTHON_TYPE.get(type_, str))
1349 )
1350 return _apply_scalar_constraints(base_type, schema)
1351
1352
1353 def _json_values_equal(left: t.Any, right: t.Any) -> bool:
1354 """Compare JSON values without treating booleans as numbers."""
1355 if isinstance(left, bool) or isinstance(right, bool):
1356 return type(left) is type(right) and left == right
1357
1358 if isinstance(left, list) and isinstance(right, list):
1359 return len(left) == len(right) and all(
1360 _json_values_equal(left_item, right_item)
1361 for left_item, right_item in zip(left, right)
1362 )
1363
1364 if isinstance(left, dict) and isinstance(right, dict):
1365 return left.keys() == right.keys() and all(
1366 _json_values_equal(left[key], right[key]) for key in left
1367 )
1368
1369 return bool(left == right)
1370
1371
1372 def _allowed_values_validator(
1373 allowed: t.Tuple[t.Any, ...],
1374 ) -> t.Callable[[t.Any], t.Any]:
1375 """Create a Pydantic validator for JSON Schema enum/const values."""
1376
1377 def validate(value: t.Any) -> t.Any:
1378 if not any(_json_values_equal(value, candidate) for candidate in allowed):
1379 raise ValueError(f"value must be one of {allowed!r}")
1380 return value
1381
1382 return validate
1383
1384
1385 def _is_numeric(value: t.Any) -> bool:
1386 return isinstance(value, (int, float)) and not isinstance(value, bool)
1387
1388
1389 def _is_json_multiple_of(value: t.Any, multiple: t.Any) -> bool:
1390 """Decimal-scaled multipleOf, matching the Zod and Effect converters.
1391
1392 Raw float modulo misreports common decimal steps (0.3 % 0.1 != 0 in
1393 binary floating point); comparing through `Decimal(str(...))` recovers the
1394 author's decimal intent.
1395 """
1396 try:
1397 return (decimal.Decimal(str(value)) % decimal.Decimal(str(multiple))) == 0
1398 except (decimal.InvalidOperation, decimal.DivisionByZero):
1399 return False
1400
1401
1402 def _multiple_of_validator(multiple: t.Any) -> t.Callable[[t.Any], t.Any]:
1403 def validate(value: t.Any) -> t.Any:
1404 if _is_numeric(value) and not _is_json_multiple_of(value, multiple):
1405 raise ValueError(f"value is not a multiple of {multiple!r}")
1406 return value
1407
1408 return validate
1409
1410
1411 def _with_string_constraints(annotation: t.Any, schema: t.Dict[str, t.Any]) -> t.Any:
1412 field_constraints: t.Dict[str, t.Any] = {}
1413 if "minLength" in schema:
1414 field_constraints["min_length"] = schema["minLength"]
1415 if "maxLength" in schema:
1416 field_constraints["max_length"] = schema["maxLength"]
1417 if field_constraints:
1418 annotation = t.Annotated[annotation, Field(**field_constraints)]
1419 if "pattern" in schema:
1420 annotation = _with_pattern_constraint(annotation, schema["pattern"])
1421 return annotation
1422
1423
1424 def _with_pattern_constraint(annotation: t.Any, pattern: str) -> t.Any:
1425 """Prefer pydantic's native pattern; otherwise use the guarded matcher,
1426 which covers ECMA constructs such as look-around (legal in Draft 7, whose
1427 regex dialect is ECMA-262) only when they cannot backtrack catastrophically.
1428 """
1429 candidate = t.Annotated[annotation, Field(pattern=pattern)]
1430 try:
1431 TypeAdapter(candidate)
1432 return candidate
1433 except Exception: # noqa: BLE001 - pydantic raises SchemaError subclasses
1434 pass
1435
1436 matcher = _string_pattern_matcher(pattern)
1437 if matcher is None:
1438 return annotation
1439
1440 def validate(value: t.Any) -> t.Any:
1441 if isinstance(value, str) and not matcher(value):
1442 raise ValueError(f"string does not match pattern {pattern!r}")
1443 return value
1444
1445 return t.Annotated[annotation, AfterValidator(validate)]
1446
1447
1448 def _with_numeric_constraints(annotation: t.Any, schema: t.Dict[str, t.Any]) -> t.Any:
1449 field_constraints: t.Dict[str, t.Any] = {}
1450 if "minimum" in schema:
1451 field_constraints["gt" if schema.get("exclusiveMinimum") is True else "ge"] = (
1452 schema["minimum"]
1453 )
1454 if _is_numeric(schema.get("exclusiveMinimum")):
1455 field_constraints["gt"] = schema["exclusiveMinimum"]
1456
1457 if "maximum" in schema:
1458 field_constraints["lt" if schema.get("exclusiveMaximum") is True else "le"] = (
1459 schema["maximum"]
1460 )
1461 if _is_numeric(schema.get("exclusiveMaximum")):
1462 field_constraints["lt"] = schema["exclusiveMaximum"]
1463
1464 if field_constraints:
1465 annotation = t.Annotated[annotation, Field(**field_constraints)]
1466 if "multipleOf" in schema:
1467 annotation = t.Annotated[
1468 annotation, AfterValidator(_multiple_of_validator(schema["multipleOf"]))
1469 ]
1470 return annotation
1471
1472
1473 def _check_numeric_keywords(schema: t.Dict[str, t.Any], value: t.Any) -> None:
1474 minimum = schema.get("minimum")
1475 if _is_numeric(minimum):
1476 if schema.get("exclusiveMinimum") is True:
1477 if value <= minimum:
1478 raise ValueError(f"value must be greater than {minimum!r}")
1479 elif value < minimum:
1480 raise ValueError(f"value must be at least {minimum!r}")
1481 exclusive_minimum = schema.get("exclusiveMinimum")
1482 if _is_numeric(exclusive_minimum) and value <= exclusive_minimum:
1483 raise ValueError(f"value must be greater than {exclusive_minimum!r}")
1484
1485 maximum = schema.get("maximum")
1486 if _is_numeric(maximum):
1487 if schema.get("exclusiveMaximum") is True:
1488 if value >= maximum:
1489 raise ValueError(f"value must be less than {maximum!r}")
1490 elif value > maximum:
1491 raise ValueError(f"value must be at most {maximum!r}")
1492 exclusive_maximum = schema.get("exclusiveMaximum")
1493 if _is_numeric(exclusive_maximum) and value >= exclusive_maximum:
1494 raise ValueError(f"value must be less than {exclusive_maximum!r}")
1495
1496 if "multipleOf" in schema and not _is_json_multiple_of(value, schema["multipleOf"]):
1497 raise ValueError(f"value is not a multiple of {schema['multipleOf']!r}")
1498
1499
1500 def _typeless_constraint_validator(
1501 schema: t.Dict[str, t.Any],
1502 ) -> t.Optional[t.Callable[[t.Any], t.Any]]:
1503 """Draft 7 applies scalar constraints per instance type even without `type`."""
1504 if not _SCALAR_CONSTRAINT_KEYWORDS.intersection(schema):
1505 return None
1506
1507 pattern_matcher = (
1508 _string_pattern_matcher(schema["pattern"]) if "pattern" in schema else None
1509 )
1510
1511 def validate(value: t.Any) -> t.Any:
1512 if isinstance(value, str):
1513 if "minLength" in schema and len(value) < schema["minLength"]:
1514 raise ValueError(
1515 f"string must contain at least {schema['minLength']} characters"
1516 )
1517 if "maxLength" in schema and len(value) > schema["maxLength"]:
1518 raise ValueError(
1519 f"string must contain at most {schema['maxLength']} characters"
1520 )
1521 if pattern_matcher is not None and not pattern_matcher(value):
1522 raise ValueError(f"string does not match pattern {schema['pattern']!r}")
1523 if _is_numeric(value):
1524 _check_numeric_keywords(schema, value)
1525 return value
1526
1527 return validate
1528
1529
1530 def _apply_scalar_constraints(
1531 base_type: t.Type[t.Any],
1532 schema: t.Dict[str, t.Any],
1533 ) -> t.Type[t.Any]:
1534 """Apply scalar JSON Schema validation keywords to a Python annotation."""
1535 type_ = schema.get("type")
1536 annotation: t.Any = base_type
1537
1538 if type_ == "string":
1539 annotation = _with_string_constraints(annotation, schema)
1540 elif type_ in ("integer", "number"):
1541 annotation = _with_numeric_constraints(annotation, schema)
1542 elif type_ is None:
1543 validator = _typeless_constraint_validator(schema)
1544 if validator is not None:
1545 annotation = t.Annotated[annotation, AfterValidator(validator)]
1546
1547 allowed: t.Optional[t.Tuple[t.Any, ...]] = None
1548 if "const" in schema and "enum" in schema:
1549 # Both keywords assert independently, so only their intersection is
1550 # acceptable; a const outside the enum makes the schema unsatisfiable.
1551 allowed = tuple(
1552 candidate
1553 for candidate in (schema["const"],)
1554 if any(_json_values_equal(candidate, member) for member in schema["enum"])
1555 )
1556 elif "const" in schema:
1557 allowed = (schema["const"],)
1558 elif "enum" in schema:
1559 allowed = tuple(schema["enum"])
1560
1561 if allowed is not None:
1562 if not allowed:
1563 return _UnsatisfiableSchema
1564 annotation = t.Annotated[
1565 annotation,
1566 AfterValidator(_allowed_values_validator(allowed)),
1567 ]
1568
1569 return t.cast(t.Type[t.Any], annotation)
1570
1571
1572 def _schema_for_exact_validation(value: t.Any) -> t.Any:
1573 """Turn internal boolean-schema markers back into JSON Schema values.
1574
1575 Conversion replaces the literal ``false`` with `_UnsatisfiableSchema`; the
1576 `jsonschema` validator needs the original ``false`` back.
1577 """
1578 if _is_unsatisfiable_schema(value):
1579 return False
1580 if isinstance(value, dict):
1581 return {key: _schema_for_exact_validation(item) for key, item in value.items()}
1582 if isinstance(value, list):
1583 return [_schema_for_exact_validation(item) for item in value]
1584 return value
1585
1586
1587 _EXACT_SCHEMA_MAP_KEYWORDS = frozenset(
1588 {
1589 "$defs",
1590 "definitions",
1591 "dependencies",
1592 "patternProperties",
1593 "properties",
1594 }
1595 )
1596 _EXACT_SCHEMA_ARRAY_KEYWORDS = frozenset({"allOf", "anyOf", "oneOf"})
1597 _EXACT_SCHEMA_VALUE_KEYWORDS = frozenset(
1598 {
1599 "additionalItems",
1600 "additionalProperties",
1601 "contains",
1602 "else",
1603 "if",
1604 "items",
1605 "not",
1606 "propertyNames",
1607 "then",
1608 }
1609 )
1610
1611 _REQUIRES_WHOLE_SCHEMA_VALIDATION = frozenset(
1612 {
1613 "$ref",
1614 "additionalItems",
1615 "contains",
1616 "dependencies",
1617 "else",
1618 "if",
1619 "maxItems",
1620 "maxProperties",
1621 "minItems",
1622 "minProperties",
1623 "not",
1624 "propertyNames",
1625 "required",
1626 "then",
1627 "uniqueItems",
1628 }
1629 )
1630
1631
1632 def _contains_compound_literal(schema: t.Dict[str, t.Any]) -> bool:
1633 values: t.List[t.Any] = []
1634 if "const" in schema:
1635 values.append(schema["const"])
1636 enum = schema.get("enum")
1637 if isinstance(enum, list):
1638 values.extend(enum)
1639 return any(isinstance(value, (dict, list)) for value in values)
1640
1641
1642 def _one_of_needs_exact_validation(options: t.Any) -> bool:
1643 """Return whether two oneOf branches can accept the same JSON type."""
1644 if not isinstance(options, list) or len(options) < 2:
1645 return False
1646
1647 branch_types: t.List[t.Set[str]] = []
1648 all_types = {"array", "boolean", "integer", "null", "number", "object", "string"}
1649 for option in options:
1650 if option is True or not isinstance(option, dict) or "type" not in option:
1651 branch_types.append(set(all_types))
1652 continue
1653 if option is False:
1654 branch_types.append(set())
1655 continue
1656 declared = option["type"]
1657 types = {declared} if isinstance(declared, str) else set(declared)
1658 if "number" in types:
1659 types.add("integer")
1660 branch_types.append(types)
1661
1662 return any(
1663 left.intersection(right)
1664 for index, left in enumerate(branch_types)
1665 for right in branch_types[index + 1 :]
1666 )
1667
1668
1669 def _combiner_has_assertion_siblings(schema: t.Dict[str, t.Any], keyword: str) -> bool:
1670 return bool(_ASSERTION_KEYWORDS.intersection(schema) - {keyword})
1671
1672
1673 def _requires_whole_schema_validation(schema: t.Any) -> bool:
1674 """Whether the materializing converter needs an authoritative final guard.
1675
1676 True when the schema (or any nested schema) uses a keyword that Pydantic
1677 field constraints cannot express faithfully: `required`, `contains`,
1678 `if`/`then`/`else`, `not`, `$ref`, unions (which Pydantic coerces across),
1679 and so on. Those schemas get a Draft 7 check on the raw value first.
1680 """
1681 if isinstance(schema, bool) or not isinstance(schema, dict):
1682 return False
1683 if (
1684 _REQUIRES_WHOLE_SCHEMA_VALIDATION.intersection(schema)
1685 or "allOf" in schema
1686 or (
1687 schema.get("type") is None
1688 and _TYPELESS_TYPE_SCOPED_KEYWORDS.intersection(schema)
1689 )
1690 # Pydantic unions coerce across members (`True` satisfies `integer |
1691 # null`), so every combiner needs the exact check, not only those with
1692 # sibling assertions or overlapping branches.
1693 or "anyOf" in schema
1694 or "oneOf" in schema
1695 or _contains_compound_literal(schema)
1696 ):
1697 return True
1698
1699 for key, child in schema.items():
1700 if key in _EXACT_SCHEMA_MAP_KEYWORDS and isinstance(child, dict):
1701 if any(
1702 _requires_whole_schema_validation(value) for value in child.values()
1703 ):
1704 return True
1705 elif key in _EXACT_SCHEMA_ARRAY_KEYWORDS and isinstance(child, list):
1706 if any(_requires_whole_schema_validation(value) for value in child):
1707 return True
1708 elif key in _EXACT_SCHEMA_VALUE_KEYWORDS:
1709 values = child if isinstance(child, list) else [child]
1710 if any(_requires_whole_schema_validation(value) for value in values):
1711 return True
1712
1713 return False
1714
1715
1716 def _needs_permissive_materialization(
1717 schema: t.Any,
1718 visited: t.Optional[t.Set[int]] = None,
1719 ) -> bool:
1720 """Detect schemas whose exact validator must not pipe into a narrower type.
1721
1722 For `$ref`, `allOf`, and typeless assertions the Pydantic type the library
1723 builds can be narrower than what the schema accepts. Once the Draft 7 check
1724 has accepted the raw value, it is passed through as `t.Any` rather than
1725 risk a second, stricter rejection or a lossy coercion.
1726 """
1727 if isinstance(schema, bool) or not isinstance(schema, dict):
1728 return False
1729 if visited is None:
1730 visited = set()
1731 if id(schema) in visited:
1732 return False
1733 visited.add(id(schema))
1734
1735 if "$ref" in schema or "allOf" in schema:
1736 return True
1737 if schema.get("type") is None and (
1738 _TYPELESS_TYPE_SCOPED_KEYWORDS.intersection(schema)
1739 or {"if", "then", "else", "not"}.intersection(schema)
1740 ):
1741 return True
1742 for key, child in schema.items():
1743 if key in _EXACT_SCHEMA_MAP_KEYWORDS and isinstance(child, dict):
1744 if any(
1745 _needs_permissive_materialization(value, visited)
1746 for value in child.values()
1747 ):
1748 return True
1749 elif key in _EXACT_SCHEMA_ARRAY_KEYWORDS and isinstance(child, list):
1750 if any(
1751 _needs_permissive_materialization(value, visited) for value in child
1752 ):
1753 return True
1754 elif key in _EXACT_SCHEMA_VALUE_KEYWORDS:
1755 values = child if isinstance(child, list) else [child]
1756 if any(
1757 _needs_permissive_materialization(value, visited) for value in values
1758 ):
1759 return True
1760 return False
1761
1762
1763 def _relax_exact_model_fields(
1764 schema: t.Dict[str, t.Any],
1765 base_model: t.Type[BaseModel],
1766 ) -> t.Type[BaseModel]:
1767 """Let exact source validation own fields the library would over-narrow."""
1768 properties = schema.get("properties")
1769 if not isinstance(properties, dict):
1770 return base_model
1771
1772 field_definitions = {}
1773 for internal_name, model_field in base_model.model_fields.items():
1774 external_name = (
1775 model_field.alias if isinstance(model_field.alias, str) else internal_name
1776 )
1777 property_schema = properties.get(external_name)
1778 if not _needs_permissive_materialization(property_schema):
1779 continue
1780
1781 default = ... if model_field.is_required() else model_field.default
1782 field_definitions[internal_name] = (
1783 t.Any,
1784 Field(
1785 default,
1786 alias=model_field.alias,
1787 description=model_field.description,
1788 examples=model_field.examples,
1789 title=model_field.title,
1790 ),
1791 )
1792
1793 if not field_definitions:
1794 return base_model
1795 return create_pydantic_model( # type: ignore[call-overload]
1796 base_model.__name__,
1797 __base__=base_model,
1798 **field_definitions,
1799 )
1800
1801
1802 def _normalize_draft4_exclusive_bounds(value: t.Any) -> t.Any:
1803 """Translate OpenAPI 3.0 boolean bounds without changing nearby keywords."""
1804 if isinstance(value, bool) or not isinstance(value, dict):
1805 return value
1806
1807 normalized: t.Dict[str, t.Any] = {}
1808 for key, child in value.items():
1809 if key in _EXACT_SCHEMA_MAP_KEYWORDS and isinstance(child, dict):
1810 normalized[key] = {
1811 name: _normalize_draft4_exclusive_bounds(schema)
1812 for name, schema in child.items()
1813 }
1814 elif key in _EXACT_SCHEMA_ARRAY_KEYWORDS and isinstance(child, list):
1815 normalized[key] = [
1816 _normalize_draft4_exclusive_bounds(schema) for schema in child
1817 ]
1818 elif key in _EXACT_SCHEMA_VALUE_KEYWORDS:
1819 if isinstance(child, list):
1820 normalized[key] = [
1821 _normalize_draft4_exclusive_bounds(schema) for schema in child
1822 ]
1823 else:
1824 normalized[key] = _normalize_draft4_exclusive_bounds(child)
1825 else:
1826 # Values under enum/const/default/examples are instance data, not
1827 # schemas, and must remain byte-for-byte equivalent.
1828 normalized[key] = child
1829
1830 if normalized.get("exclusiveMinimum") is True and _is_numeric(
1831 normalized.get("minimum")
1832 ):
1833 normalized["exclusiveMinimum"] = normalized.pop("minimum")
1834 elif isinstance(normalized.get("exclusiveMinimum"), bool):
1835 normalized.pop("exclusiveMinimum")
1836
1837 if normalized.get("exclusiveMaximum") is True and _is_numeric(
1838 normalized.get("maximum")
1839 ):
1840 normalized["exclusiveMaximum"] = normalized.pop("maximum")
1841 elif isinstance(normalized.get("exclusiveMaximum"), bool):
1842 normalized.pop("exclusiveMaximum")
1843
1844 return normalized
1845
1846
1847 def _exact_validation_multiple_of(
1848 _validator: t.Any,
1849 multiple: t.Any,
1850 instance: t.Any,
1851 _schema: t.Any,
1852 ) -> t.Iterator[t.Any]:
1853 if _is_numeric(instance) and not _is_json_multiple_of(instance, multiple):
1854 yield jsonschema_exceptions.ValidationError(
1855 f"{instance!r} is not a multiple of {multiple!r}"
1856 )
1857
1858
1859 # Schema-supplied regular expressions.
1860 #
1861 # Tool schemas are third-party input, and so are the strings matched against
1862 # their patterns: LLM tool arguments, including object keys. Python's `re`
1863 # backtracks, so a schema pattern such as `^(a+)+$` can freeze a worker on a
1864 # short argument. Every schema regex is evaluated through one of two helpers:
1865 #
1866 # - `_linear_pattern_matcher`: pydantic-core's Rust regex engine, which runs in
1867 # time linear in the input. `patternProperties` keys use only this, because a
1868 # key pattern decides routing and cannot be dropped.
1869 # - `_string_pattern_matcher`: the same engine, plus a guarded Python `re`
1870 # fallback for the ECMA constructs the Rust engine rejects (look-around,
1871 # backreferences). The fallback runs only when `_fallback_pattern_is_safe`
1872 # proves the pattern cannot backtrack catastrophically, and only on input of
1873 # at most `_BACKTRACKING_FALLBACK_MAX_INPUT` characters. Anything else is not
1874 # enforced. Widening is safe: a hostile schema could omit the pattern.
1875 #
1876 # The static check matches the TypeScript SDK's fallback check.
1877
1878 _PATTERN_CACHE_SIZE = 1024
1879
1880 # Longer input is treated as matching on the `re` fallback. This bounds the
1881 # quadratic scan over start positions that unanchored search performs.
1882 _BACKTRACKING_FALLBACK_MAX_INPUT = 1000
1883
1884 # A quantifier is "large" when it is unbounded or spans more than this many
1885 # repetitions (`m - n` in `{n,m}`).
1886 _LARGE_QUANTIFIER_SPAN = 16
1887
1888 # Polynomial degree budget: one implicit factor for unanchored search, plus
1889 # the large quantifiers outside look-around, plus the most in one look-around.
1890 _MAX_FALLBACK_DEGREE = 2
1891
1892 # Budget for the product of small quantifier spans and alternation widths.
1893 _MAX_FALLBACK_CHOICES = 64
1894
1895 # Absolute explicit repeat bounds also limit zero-width assertion work.
1896 _MAX_FALLBACK_REPEAT_COUNT = 64
1897
1898
1899 @functools.lru_cache(maxsize=_PATTERN_CACHE_SIZE)
1900 def _linear_pattern_matcher(pattern: str) -> t.Optional[t.Callable[[str], bool]]:
1901 """Unanchored (search) matcher on pydantic's linear-time Rust regex, or
1902 None when that engine cannot compile `pattern`."""
1903 try:
1904 adapter = TypeAdapter(t.Annotated[str, Field(pattern=pattern)])
1905 except Exception: # noqa: BLE001 - pydantic raises SchemaError subclasses
1906 return None
1907
1908 def match_with_pydantic(value: str) -> bool:
1909 try:
1910 adapter.validate_python(value, strict=True)
1911 except ValidationError:
1912 return False
1913 return True
1914
1915 return match_with_pydantic
1916
1917
1918 def _pattern_supported_by_pydantic(pattern: str) -> bool:
1919 return _linear_pattern_matcher(pattern) is not None
1920
1921
1922 def _load_regex_parser() -> t.Tuple[t.Any, t.Any]:
1923 try: # Python 3.11+
1924 return (
1925 importlib.import_module("re._parser"),
1926 importlib.import_module("re._constants"),
1927 )
1928 except ImportError: # Python 3.10; deprecated aliases from 3.11 on
1929 return (
1930 importlib.import_module("sre_parse"),
1931 importlib.import_module("sre_constants"),
1932 )
1933
1934
1935 _REGEX_PARSER, _REGEX_CONSTANTS = _load_regex_parser()
1936 _REGEX_REPEATS = frozenset(
1937 op
1938 for op in (
1939 _REGEX_CONSTANTS.MAX_REPEAT,
1940 _REGEX_CONSTANTS.MIN_REPEAT,
1941 getattr(_REGEX_CONSTANTS, "POSSESSIVE_REPEAT", None),
1942 )
1943 if op is not None
1944 )
1945 _REGEX_LOOKAROUNDS = frozenset({_REGEX_CONSTANTS.ASSERT, _REGEX_CONSTANTS.ASSERT_NOT})
1946 _REGEX_ATOMS = frozenset(
1947 {
1948 _REGEX_CONSTANTS.ANY,
1949 _REGEX_CONSTANTS.AT,
1950 _REGEX_CONSTANTS.CATEGORY,
1951 _REGEX_CONSTANTS.IN,
1952 _REGEX_CONSTANTS.LITERAL,
1953 _REGEX_CONSTANTS.NOT_LITERAL,
1954 }
1955 )
1956 _REGEX_ATOMIC_GROUP = getattr(_REGEX_CONSTANTS, "ATOMIC_GROUP", None)
1957
1958
1959 class _BacktrackingRisk(Exception):
1960 """The pattern may backtrack catastrophically under Python `re`."""
1961
1962
1963 class _FallbackPatternCheck:
1964 """Conservative static check over a `re` parse tree.
1965
1966 False positives only leave a pattern unenforced, so every construct that
1967 is not clearly bounded counts as a risk.
1968 """
1969
1970 def __init__(self) -> None:
1971 self.max_large_in_lookaround = 0
1972 self.choices = 1
1973
1974 def multiply_choices(self, factor: int) -> None:
1975 self.choices *= factor
1976 if self.choices > _MAX_FALLBACK_CHOICES:
1977 raise _BacktrackingRisk("too many backtracking choices")
1978
1979 def visit(
1980 self,
1981 subpattern: t.Any,
1982 *,
1983 in_repeat: bool,
1984 in_lookaround: bool,
1985 ) -> int:
1986 """Return the large quantifiers in `subpattern` outside look-around."""
1987 large = 0
1988 for op, av in subpattern.data:
1989 if op in _REGEX_REPEATS:
1990 if in_repeat:
1991 raise _BacktrackingRisk("quantified group contains a quantifier")
1992 low, high, body = av
1993 if low > _MAX_FALLBACK_REPEAT_COUNT or (
1994 high != _REGEX_CONSTANTS.MAXREPEAT
1995 and high > _MAX_FALLBACK_REPEAT_COUNT
1996 ):
1997 raise _BacktrackingRisk("repeat count exceeds the work budget")
1998 if high == _REGEX_CONSTANTS.MAXREPEAT or (
1999 high - low > _LARGE_QUANTIFIER_SPAN
2000 ):
2001 large += 1
2002 elif high > low:
2003 self.multiply_choices(high - low + 1)
2004 large += self.visit(body, in_repeat=True, in_lookaround=in_lookaround)
2005 elif op == _REGEX_CONSTANTS.BRANCH:
2006 if in_repeat:
2007 raise _BacktrackingRisk("quantified group contains alternation")
2008 _, alternatives = av
2009 self.multiply_choices(len(alternatives))
2010 for alternative in alternatives:
2011 large += self.visit(
2012 alternative,
2013 in_repeat=in_repeat,
2014 in_lookaround=in_lookaround,
2015 )
2016 elif op in _REGEX_LOOKAROUNDS:
2017 if in_lookaround:
2018 raise _BacktrackingRisk("nested look-around")
2019 _, body = av
2020 self.max_large_in_lookaround = max(
2021 self.max_large_in_lookaround,
2022 self.visit(body, in_repeat=in_repeat, in_lookaround=True),
2023 )
2024 elif op == _REGEX_CONSTANTS.SUBPATTERN:
2025 large += self.visit(
2026 av[-1], in_repeat=in_repeat, in_lookaround=in_lookaround
2027 )
2028 elif _REGEX_ATOMIC_GROUP is not None and op == _REGEX_ATOMIC_GROUP:
2029 large += self.visit(
2030 av, in_repeat=in_repeat, in_lookaround=in_lookaround
2031 )
2032 elif op not in _REGEX_ATOMS:
2033 # Backreferences (GROUPREF, GROUPREF_EXISTS) and anything this
2034 # check does not model.
2035 raise _BacktrackingRisk(f"unsupported regex construct {op}")
2036 return large
2037
2038
2039 def _fallback_pattern_is_safe(pattern: str) -> bool:
2040 """Whether Python `re` cannot backtrack catastrophically on `pattern`.
2041
2042 Rejects backreferences, quantified groups (look-around included) that
2043 contain a quantifier or alternation, excessive explicit repeat bounds,
2044 nested look-around, more than
2045 `_MAX_FALLBACK_DEGREE` nested polynomial factors, and more than
2046 `_MAX_FALLBACK_CHOICES` combined small-quantifier and alternation choices.
2047
2048 :raises re.error: `pattern` is not a valid Python regular expression.
2049 """
2050 parsed = _REGEX_PARSER.parse(pattern)
2051 check = _FallbackPatternCheck()
2052 try:
2053 large_outside = check.visit(parsed, in_repeat=False, in_lookaround=False)
2054 except _BacktrackingRisk:
2055 return False
2056 anchored = (
2057 bool(parsed.data)
2058 and parsed.data[0]
2059 == (
2060 _REGEX_CONSTANTS.AT,
2061 _REGEX_CONSTANTS.AT_BEGINNING,
2062 )
2063 and not parsed.state.flags & re.MULTILINE
2064 )
2065 degree = (0 if anchored else 1) + large_outside + check.max_large_in_lookaround
2066 return degree <= _MAX_FALLBACK_DEGREE
2067
2068
2069 @functools.lru_cache(maxsize=_PATTERN_CACHE_SIZE)
2070 def _string_pattern_matcher(pattern: str) -> t.Optional[t.Callable[[str], bool]]:
2071 """Search-semantics matcher for a `pattern` string constraint, or None
2072 (with a warning) when it cannot be enforced in bounded time."""
2073 linear = _linear_pattern_matcher(pattern)
2074 if linear is not None:
2075 return linear
2076
2077 try:
2078 safe = _fallback_pattern_is_safe(pattern)
2079 compiled = re.compile(pattern)
2080 except (re.error, OverflowError, RecursionError):
2081 logger.warning(
2082 "Ignoring JSON Schema pattern %r: not a valid Python regular expression",
2083 pattern,
2084 )
2085 return None
2086 if not safe:
2087 logger.warning(
2088 "Ignoring JSON Schema pattern %r: the linear-time regex engine does "
2089 "not support it and Python's backtracking engine could stall on it",
2090 pattern,
2091 )
2092 return None
2093
2094 def match_with_re(value: str) -> bool:
2095 if len(value) > _BACKTRACKING_FALLBACK_MAX_INPUT:
2096 return True
2097 return compiled.search(value) is not None
2098
2099 return match_with_re
2100
2101
2102 def _exact_validation_pattern(
2103 _validator: Validator,
2104 pattern: t.Any,
2105 instance: t.Any,
2106 _schema: t.Any,
2107 ) -> t.Iterator[t.Any]:
2108 if not isinstance(instance, str) or not isinstance(pattern, str):
2109 return
2110 matcher = _string_pattern_matcher(pattern)
2111 if matcher is not None and not matcher(instance):
2112 yield jsonschema_exceptions.ValidationError(
2113 f"{instance!r} does not match {pattern!r}"
2114 )
2115
2116
2117 def _safe_pattern_properties(
2118 validator: t.Any,
2119 pattern_properties: t.Any,
2120 instance: t.Any,
2121 _schema: t.Any,
2122 ) -> t.Iterator[t.Any]:
2123 """jsonschema's `patternProperties` with keys matched in linear time."""
2124 if not validator.is_type(instance, "object"):
2125 return
2126
2127 for pattern, subschema in pattern_properties.items():
2128 matches_key = _linear_pattern_matcher(pattern)
2129 if matches_key is None:
2130 # Conversion rejects these first; fail closed if one slips past.
2131 yield jsonschema_exceptions.ValidationError(
2132 _unsupported_pattern_property_message(pattern)
2133 )
2134 continue
2135 for key, value in instance.items():
2136 if matches_key(key):
2137 yield from validator.descend(
2138 value,
2139 subschema,
2140 path=key,
2141 schema_path=pattern,
2142 )
2143
2144
2145 def _extras_msg(extras: t.Sequence[t.Any]) -> t.Tuple[str, str]:
2146 verb = "was" if len(extras) == 1 else "were"
2147 return ", ".join(repr(extra) for extra in extras), verb
2148
2149
2150 def _safe_additional_properties(
2151 validator: t.Any,
2152 additional: t.Any,
2153 instance: t.Any,
2154 schema: t.Any,
2155 ) -> t.Iterator[t.Any]:
2156 """jsonschema's `additionalProperties` with keys matched in linear time."""
2157 if not validator.is_type(instance, "object"):
2158 return
2159
2160 properties = schema.get("properties", {})
2161 patterns = list(schema.get("patternProperties", {}))
2162 matchers = []
2163 # jsonschema searches the `|`-joined patterns, so a lone "" matches nothing.
2164 if "|".join(patterns):
2165 for pattern in patterns:
2166 matches_key = _linear_pattern_matcher(pattern)
2167 if matches_key is None:
2168 yield jsonschema_exceptions.ValidationError(
2169 _unsupported_pattern_property_message(pattern)
2170 )
2171 return
2172 matchers.append(matches_key)
2173 extras = {
2174 key
2175 for key in instance
2176 if key not in properties
2177 and not any(matches_key(key) for matches_key in matchers)
2178 }
2179
2180 if validator.is_type(additional, "object"):
2181 for extra in extras:
2182 yield from validator.descend(instance[extra], additional, path=extra)
2183 elif not additional and extras:
2184 if "patternProperties" in schema:
2185 verb = "does" if len(extras) == 1 else "do"
2186 joined = ", ".join(repr(each) for each in sorted(extras))
2187 joined_patterns = ", ".join(
2188 repr(each) for each in sorted(schema["patternProperties"])
2189 )
2190 yield jsonschema_exceptions.ValidationError(
2191 f"{joined} {verb} not match any of the regexes: {joined_patterns}"
2192 )
2193 else:
2194 error = "Additional properties are not allowed (%s %s unexpected)"
2195 yield jsonschema_exceptions.ValidationError(
2196 error % _extras_msg(sorted(extras, key=str))
2197 )
2198
2199
2200 class _LinearRegexModule:
2201 """Stands in for `re` inside jsonschema's evaluated-property helpers, whose
2202 only regex use is `re.search(pattern, key)` over `patternProperties`."""
2203
2204 @staticmethod
2205 def search(pattern: t.Any, string: t.Any) -> t.Optional[bool]:
2206 matches_key = _linear_pattern_matcher(pattern)
2207 if matches_key is None:
2208 raise ValueError(_unsupported_pattern_property_message(pattern))
2209 return True if matches_key(string) else None
2210
2211
2212 def _unsupported_unevaluated_properties(
2213 validator: t.Any,
2214 _unevaluated: t.Any,
2215 instance: t.Any,
2216 _schema: t.Any,
2217 ) -> t.Iterator[t.Any]:
2218 if validator.is_type(instance, "object"):
2219 yield jsonschema_exceptions.ValidationError(
2220 "unevaluatedProperties is not supported with the installed jsonschema"
2221 )
2222
2223
2224 def _linear_unevaluated_properties(keyword: t.Any) -> t.Any:
2225 """Rebind jsonschema's `unevaluatedProperties` (2019-09 and 2020-12) so its
2226 `patternProperties` key matching uses `_LinearRegexModule`.
2227
2228 The keyword's semantics are long and version-specific, so the original
2229 code objects run unchanged against copied module globals; nothing global
2230 is patched. If the helper no longer reaches regexes through a module-level
2231 `re`, the keyword fails closed instead of silently using `re`.
2232 """
2233 module_globals = getattr(keyword, "__globals__", None)
2234 finder = (module_globals or {}).get("find_evaluated_property_keys_by_schema")
2235 finder_globals = getattr(finder, "__globals__", None)
2236 if (
2237 not isinstance(keyword, types.FunctionType)
2238 or not isinstance(finder, types.FunctionType)
2239 or keyword.__closure__ is not None
2240 or finder.__closure__ is not None
2241 or finder_globals is None
2242 or finder_globals.get("re") is not re
2243 or "re" not in finder.__code__.co_names
2244 or "re" in keyword.__code__.co_names
2245 ):
2246 return _unsupported_unevaluated_properties
2247
2248 linear_finder_globals = dict(finder_globals)
2249 linear_finder = types.FunctionType(
2250 finder.__code__,
2251 linear_finder_globals,
2252 finder.__name__,
2253 finder.__defaults__,
2254 )
2255 linear_finder_globals["re"] = _LinearRegexModule
2256 linear_finder_globals["find_evaluated_property_keys_by_schema"] = linear_finder
2257
2258 linear_keyword_globals = dict(keyword.__globals__)
2259 linear_keyword_globals["re"] = _LinearRegexModule
2260 linear_keyword_globals["find_evaluated_property_keys_by_schema"] = linear_finder
2261 return types.FunctionType(
2262 keyword.__code__,
2263 linear_keyword_globals,
2264 keyword.__name__,
2265 keyword.__defaults__,
2266 )
2267
2268
2269 _SAFE_REGEX_VALIDATOR_CLASSES: t.Set[t.Any] = set()
2270
2271
2272 @functools.lru_cache(maxsize=None)
2273 def _with_safe_regex_keywords(validator_class: t.Any) -> t.Any:
2274 """Extend a jsonschema validator class so no keyword evaluates a schema
2275 regex with Python `re` on unbounded input.
2276
2277 jsonschema's `evolve` (used by `descend` for every subschema) switches to
2278 the stock class of any subschema that declares `$schema`, which would drop
2279 these overrides. The returned class keeps the dialect switch but re-applies
2280 the regex overrides to the class it switches to.
2281 """
2282 if validator_class in _SAFE_REGEX_VALIDATOR_CLASSES:
2283 return validator_class
2284 overrides = {
2285 "additionalProperties": _safe_additional_properties,
2286 "pattern": _exact_validation_pattern,
2287 "patternProperties": _safe_pattern_properties,
2288 }
2289 unevaluated = validator_class.VALIDATORS.get("unevaluatedProperties")
2290 if unevaluated is not None:
2291 overrides["unevaluatedProperties"] = _linear_unevaluated_properties(unevaluated)
2292 return _install_safe_regex_evolve(
2293 jsonschema_validators.extend(validator_class, overrides)
2294 )
2295
2296
2297 @functools.lru_cache(maxsize=None)
2298 def _exact_validator_class() -> t.Any:
2299 # Align multipleOf with the decimal-scaled checks in the Zod and Effect
2300 # converters instead of raw float modulo.
2301 return _install_safe_regex_evolve(
2302 jsonschema_validators.extend(
2303 _with_safe_regex_keywords(jsonschema_validators.Draft7Validator),
2304 {"multipleOf": _exact_validation_multiple_of},
2305 )
2306 )
2307
2308
2309 def _install_safe_regex_evolve(validator_class: t.Any) -> t.Any:
2310 stock_evolve = validator_class.evolve
2311
2312 def evolve(self: t.Any, **changes: t.Any) -> t.Any:
2313 evolved = stock_evolve(self, **changes)
2314 evolved_class: t.Any = type(evolved)
2315 if evolved_class in _SAFE_REGEX_VALIDATOR_CLASSES:
2316 return evolved
2317 # Rebuild on the safe variant, copying init fields exactly as
2318 # jsonschema's own `evolve` does.
2319 safe_class = _with_safe_regex_keywords(evolved_class)
2320 return safe_class(
2321 **{
2322 field.alias: getattr(evolved, field.name)
2323 for field in evolved_class.__attrs_attrs__
2324 if field.init
2325 }
2326 )
2327
2328 validator_class.evolve = evolve
2329 _SAFE_REGEX_VALIDATOR_CLASSES.add(validator_class)
2330 return validator_class
2331
2332
2333 # A bare registry resolves only references inside the schema document and
2334 # raises on anything else; jsonschema's default registry falls back to
2335 # fetching unknown URIs over the network.
2336 _LOCAL_ONLY_REGISTRY: Registry[t.Any] = Registry()
2337
2338
2339 def _reject_external_references(schema: t.Any) -> None:
2340 """Fail at conversion time on any `$ref` that is not a local JSON Pointer.
2341
2342 Only schema positions are walked; `const`, `default`, `enum`, and
2343 `examples` hold instance data.
2344 """
2345 if isinstance(schema, list):
2346 for item in schema:
2347 _reject_external_references(item)
2348 return
2349 if not isinstance(schema, dict):
2350 return
2351 reference = schema.get("$ref")
2352 if isinstance(reference, str) and not reference.startswith("#"):
2353 raise ValueError(
2354 f"External schema reference {reference!r} is not supported; "
2355 "only local references (#/...) are resolved"
2356 )
2357 for keyword, value in schema.items():
2358 if keyword in _SCHEMA_VALUED_KEYWORDS or keyword in _SCHEMA_LIST_KEYWORDS:
2359 _reject_external_references(value)
2360 elif keyword in _SCHEMA_MAP_KEYWORDS:
2361 if isinstance(value, dict):
2362 for entry in value.values():
2363 _reject_external_references(entry)
2364 elif keyword == "items":
2365 _reject_external_references(value)
2366 elif keyword == "dependencies" and isinstance(value, dict):
2367 for entry in value.values():
2368 if isinstance(entry, (dict, bool)):
2369 _reject_external_references(entry)
2370
2371
2372 def _is_closed_object_branch(branch: t.Any) -> bool:
2373 return (
2374 isinstance(branch, dict)
2375 and isinstance(branch.get("properties"), dict)
2376 and len(branch["properties"]) > 0
2377 and "additionalProperties" not in branch
2378 and "patternProperties" not in branch
2379 and "$ref" not in branch
2380 )
2381
2382
2383 def _close_all_of_branches(schema: t.Any) -> t.Any:
2384 """Apply the omitted-`additionalProperties` strictness rule once per `allOf`.
2385
2386 Draft 7 already lets sibling object branches accept each other's keys, so
2387 the exact validator only needs the strictness a lone named-property object
2388 would have had: the union of every branch's declared keys, applied at the
2389 `allOf` node. Mirrors `parseAllOf` in @composio/json-schema-to-zod and
2390 `openAllOfBranches` in @composio/json-schema-to-effect-schema.
2391 """
2392 if isinstance(schema, list):
2393 return [_close_all_of_branches(item) for item in schema]
2394 if not isinstance(schema, dict):
2395 return schema
2396
2397 result: t.Dict[str, t.Any] = {}
2398 for key, value in schema.items():
2399 if key in _EXACT_SCHEMA_MAP_KEYWORDS and isinstance(value, dict):
2400 result[key] = {
2401 name: _close_all_of_branches(child) for name, child in value.items()
2402 }
2403 elif key in _EXACT_SCHEMA_ARRAY_KEYWORDS or key in _EXACT_SCHEMA_VALUE_KEYWORDS:
2404 result[key] = _close_all_of_branches(value)
2405 else:
2406 result[key] = value
2407
2408 branches = schema.get("allOf")
2409 if (
2410 isinstance(branches, list)
2411 and len(branches) >= 2
2412 and all(_is_closed_object_branch(branch) for branch in branches)
2413 and not {"properties", "patternProperties", "additionalProperties"}
2414 & set(schema)
2415 ):
2416 result["properties"] = {
2417 name: True for branch in branches for name in branch["properties"]
2418 }
2419 result["additionalProperties"] = False
2420 return result
2421
2422
2423 def _reject_unsupported_pattern_properties(
2424 schema: t.Any,
2425 root_schema: t.Dict[str, t.Any],
2426 ) -> None:
2427 """Fail at conversion time on any reachable `patternProperties` key the
2428 linear-time engine cannot match."""
2429 for candidate in _iter_reachable_schemas(schema, root_schema):
2430 pattern_properties = candidate.get("patternProperties")
2431 if isinstance(pattern_properties, dict):
2432 for pattern in pattern_properties:
2433 _compile_pattern_property(pattern)
2434
2435
2436 def _validate_json_schema(
2437 schema: t.Dict[str, t.Any],
2438 root_schema: t.Dict[str, t.Any],
2439 ) -> t.Callable[[t.Any], t.Any]:
2440 """Compile an exact JSON Schema acceptance check for a Pydantic model."""
2441 validation_schema = _close_all_of_branches(
2442 _normalize_draft4_exclusive_bounds(_schema_for_exact_validation(schema))
2443 )
2444 validation_root = _close_all_of_branches(
2445 _normalize_draft4_exclusive_bounds(_schema_for_exact_validation(root_schema))
2446 )
2447 _reject_external_references(validation_root)
2448 _reject_external_references(validation_schema)
2449 # The SDK contract is Draft 7 plus the OpenAPI 3.0 boolean spelling for
2450 # exclusive bounds. Falling the entire document back to Draft 4 would make
2451 # `contains`, `const`, `if`/`then`/`else`, and `propertyNames` disappear.
2452 # Schema regexes (`pattern`, `patternProperties`, `additionalProperties`
2453 # key matching) never run on unbounded Python `re`, which is exponential
2454 # on inputs such as `(a+)+$`.
2455 _reject_unsupported_pattern_properties(validation_schema, validation_root)
2456 validator_class = _exact_validator_class()
2457 validator = validator_class(
2458 validation_root,
2459 registry=_LOCAL_ONLY_REGISTRY,
2460 ).evolve(schema=validation_schema)
2461
2462 def validate(value: t.Any) -> t.Any:
2463 error = next(validator.iter_errors(value), None)
2464 if error is not None:
2465 location = ".".join(str(part) for part in error.path)
2466 message = f"{location}: {error.message}" if location else error.message
2467 raise ValueError(message)
2468 return value
2469
2470 return validate
2471
2472
2473 def _with_exact_validation_annotation(
2474 annotation: t.Any,
2475 schema: t.Dict[str, t.Any],
2476 root_schema: t.Dict[str, t.Any],
2477 ) -> t.Type[t.Any]:
2478 """Validate the raw value before Pydantic coercion or default handling."""
2479 validate_schema = _validate_json_schema(schema, root_schema)
2480 return t.cast(
2481 t.Type[t.Any],
2482 t.Annotated[annotation, BeforeValidator(validate_schema)],
2483 )
2484
2485
2486 def _with_exact_validation(
2487 model: t.Type[BaseModel],
2488 schema: t.Dict[str, t.Any],
2489 root_schema: t.Dict[str, t.Any],
2490 ) -> t.Type[t.Any]:
2491 """Keep model materialization while enforcing the source schema first."""
2492 validate_schema = _validate_json_schema(schema, root_schema)
2493
2494 def validate_source_schema(cls, value: t.Any) -> t.Any:
2495 return validate_schema(value)
2496
2497 validator = model_validator(mode="before")(
2498 t.cast(t.Any, classmethod(validate_source_schema))
2499 )
2500
2501 name = model.__name__
2502 return t.cast(
2503 t.Type[BaseModel],
2504 type(
2505 name,
2506 (model,),
2507 {
2508 "__module__": __name__,
2509 "__qualname__": name,
2510 "_validate_source_schema": validator,
2511 },
2512 ),
2513 )
2514
2515
2516 def _convert_object_with_unsatisfiable_properties(
2517 schema: t.Dict[str, t.Any],
2518 *,
2519 root_schema: t.Optional[t.Dict[str, t.Any]] = None,
2520 ) -> t.Type[t.Any]:
2521 """Build an object model while retaining always-rejecting properties."""
2522 properties = schema.get("properties", {})
2523 required = set(schema.get("required", []))
2524 rejecting_properties = {
2525 name: prop_schema
2526 for name, prop_schema in properties.items()
2527 if _contains_unsatisfiable_schema(prop_schema)
2528 }
2529 ordinary_properties = {
2530 name: prop_schema
2531 for name, prop_schema in properties.items()
2532 if name not in rejecting_properties
2533 }
2534
2535 model_name = schema.get("title", "GeneratedModel")
2536 base_schema = {
2537 **schema,
2538 "title": model_name,
2539 "properties": ordinary_properties,
2540 "required": [
2541 name for name in schema.get("required", []) if name in ordinary_properties
2542 ],
2543 }
2544 base_model = create_model_from_schema(
2545 _normalize_schema_for_library(base_schema),
2546 allow_undefined_array_items=True,
2547 allow_undefined_type=True,
2548 )
2549
2550 field_definitions = {}
2551 for name, prop_schema in rejecting_properties.items():
2552 annotation = _filtered_schema_to_pydantic_type(
2553 prop_schema,
2554 root_schema=root_schema,
2555 )
2556 default = (
2557 ...
2558 if name in required
2559 else prop_schema.get("default")
2560 if isinstance(prop_schema, dict)
2561 else None
2562 )
2563 field_kwargs = {}
2564 if isinstance(prop_schema, dict):
2565 for metadata_key in ("description", "examples", "title"):
2566 if metadata_key in prop_schema:
2567 field_kwargs[metadata_key] = prop_schema[metadata_key]
2568 field_definitions[name] = (
2569 annotation,
2570 Field(default, **field_kwargs),
2571 )
2572
2573 return create_pydantic_model( # type: ignore[call-overload]
2574 model_name,
2575 __base__=base_model,
2576 **field_definitions,
2577 )
2578
2579
2580 _TYPE_ARRAY_CHILD_KEYWORDS_MAPS = (
2581 "properties",
2582 "patternProperties",
2583 "$defs",
2584 "definitions",
2585 )
2586 _TYPE_ARRAY_CHILD_KEYWORDS_LISTS = ("anyOf", "allOf", "oneOf")
2587 _TYPE_ARRAY_CHILD_KEYWORDS_SCHEMAS = (
2588 "items",
2589 "additionalItems",
2590 "additionalProperties",
2591 "contains",
2592 "propertyNames",
2593 "not",
2594 "if",
2595 "then",
2596 "else",
2597 )
2598
2599
2600 def _normalize_schema_for_library(schema: t.Any) -> t.Any:
2601 """Rewrite constructs the json-schema-to-pydantic library mishandles.
2602
2603 - `type: [...]` becomes an equivalent `anyOf` of single-type schemas: the
2604 library applies every sibling constraint to every union member, so
2605 `{"type": ["string", "number"], "minLength": 2, "minimum": 10}` raises
2606 `TypeError` at validation time when a valid string hits the numeric
2607 constraint. Scoping constraints per member is semantically identical
2608 under Draft 7 (mismatched-type keywords are ignored).
2609 - `pattern` values the Rust regex crate rejects (ECMA look-around) are
2610 dropped from the library input; the exact-validation layer still
2611 enforces them through Python `re`.
2612 """
2613 if not isinstance(schema, dict):
2614 return schema
2615
2616 result: t.Dict[str, t.Any] = {}
2617 for key, value in schema.items():
2618 if key in _TYPE_ARRAY_CHILD_KEYWORDS_MAPS and isinstance(value, dict):
2619 result[key] = {
2620 name: _normalize_schema_for_library(child)
2621 for name, child in value.items()
2622 }
2623 elif key in _TYPE_ARRAY_CHILD_KEYWORDS_LISTS and isinstance(value, list):
2624 result[key] = [_normalize_schema_for_library(child) for child in value]
2625 elif key in _TYPE_ARRAY_CHILD_KEYWORDS_SCHEMAS:
2626 result[key] = _normalize_schema_for_library(value)
2627 else:
2628 result[key] = value
2629
2630 pattern = result.get("pattern")
2631 if isinstance(pattern, str) and not _pattern_supported_by_pydantic(pattern):
2632 del result["pattern"]
2633
2634 # Draft-4 boolean exclusive flags: the library reads the boolean itself as
2635 # the numeric bound (lt=True becomes "less than 1"). Translate them to the
2636 # Draft 7 numeric spelling before the library sees them.
2637 if result.get("exclusiveMinimum") is True and _is_numeric(result.get("minimum")):
2638 result["exclusiveMinimum"] = result.pop("minimum")
2639 elif isinstance(result.get("exclusiveMinimum"), bool):
2640 del result["exclusiveMinimum"]
2641 if result.get("exclusiveMaximum") is True and _is_numeric(result.get("maximum")):
2642 result["exclusiveMaximum"] = result.pop("maximum")
2643 elif isinstance(result.get("exclusiveMaximum"), bool):
2644 del result["exclusiveMaximum"]
2645
2646 # A one-member type array is the same as the bare type, and collapsing it
2647 # lets the constraint scoping below apply.
2648 if isinstance(result.get("type"), list) and len(result["type"]) == 1:
2649 result["type"] = result["type"][0]
2650
2651 # The library applies scalar keywords unconditionally, so a keyword that
2652 # does not scope to the declared type (or any keyword on a typeless
2653 # schema) raises TypeError at validation time on mismatched instances.
2654 # Drop them from the library input; the exact-validation layer that wraps
2655 # every object conversion still enforces them per Draft 7.
2656 declared_type = result.get("type")
2657 if isinstance(declared_type, str):
2658 scoped_keywords = _TYPE_SCOPED_KEYWORDS.get(declared_type)
2659 if scoped_keywords is not None:
2660 for keyword in _SCALAR_CONSTRAINT_KEYWORDS - scoped_keywords:
2661 result.pop(keyword, None)
2662 elif declared_type is None:
2663 for keyword in _SCALAR_CONSTRAINT_KEYWORDS:
2664 result.pop(keyword, None)
2665
2666 type_ = result.get("type")
2667 if not isinstance(type_, list) or len(type_) < 2:
2668 return result
2669
2670 members = []
2671 for member in type_:
2672 member_schema: t.Dict[str, t.Any] = {"type": member}
2673 scoped = _TYPE_SCOPED_KEYWORDS.get(member, frozenset())
2674 for keyword in scoped | _TYPE_ARRAY_SHARED_KEYWORDS:
2675 if keyword in result:
2676 member_schema[keyword] = result[keyword]
2677 members.append(member_schema)
2678
2679 wrapper: t.Dict[str, t.Any] = {
2680 key: value
2681 for key, value in result.items()
2682 if key in _TYPE_ARRAY_WRAPPER_KEYWORDS
2683 }
2684 wrapper["anyOf"] = members
2685 return wrapper
2686
2687
2688 def _convert_with_library(
2689 schema: t.Dict[str, t.Any],
2690 *,
2691 root_schema: t.Optional[t.Dict[str, t.Any]] = None,
2692 ) -> t.Union[t.Type, t.Any]:
2693 """Use json-schema-to-pydantic for complex schema conversion."""
2694 # Dynamic-key policy compilation is an acceptance boundary, so it must run
2695 # outside the legacy library-conversion fallback below. Invalid patterns or
2696 # references must fail closed instead of silently changing the field to str.
2697 if schema.get("type") == "object":
2698 document_root = root_schema if root_schema is not None else schema
2699 if "const" in schema or "enum" in schema:
2700 # Object literals must retain their exact value; model defaults
2701 # would change it, and RootModel cannot use the extra-key mixin.
2702 return _with_exact_validation_annotation(
2703 t.Annotated[
2704 t.Dict[str, t.Any],
2705 WithJsonSchema(_schema_for_exact_validation(schema)),
2706 ],
2707 schema,
2708 document_root,
2709 )
2710 if _contains_unsatisfiable_schema(schema.get("properties", {})):
2711 try:
2712 base_model = _convert_object_with_unsatisfiable_properties(
2713 schema,
2714 root_schema=document_root,
2715 )
2716 except (SchemaError, CombinerError) as e:
2717 logger.debug(
2718 f"Library schema conversion failed: {e}, preserving exact validation"
2719 )
2720 return _with_exact_validation_annotation(
2721 t.Any,
2722 schema,
2723 document_root,
2724 )
2725 except Exception as e:
2726 logger.debug(
2727 f"Unexpected error in schema conversion: {e}, preserving exact validation"
2728 )
2729 return _with_exact_validation_annotation(
2730 t.Any,
2731 schema,
2732 document_root,
2733 )
2734 return _with_exact_validation(
2735 apply_object_policy(
2736 schema,
2737 _apply_nested_object_policies(schema, base_model, document_root),
2738 root_schema=document_root,
2739 ),
2740 schema,
2741 document_root,
2742 )
2743 # A property-less object with no dynamic-key constraints accepts and
2744 # preserves arbitrary content. Modelling it as an empty Pydantic
2745 # model instead silently discarded every key (issue #4064).
2746 if object_is_open(schema) and not schema.get("patternProperties"):
2747 return t.cast(t.Type, t.Dict[str, t.Any])
2748 if "title" not in schema:
2749 schema = {**schema, "title": "GeneratedModel"}
2750
2751 try:
2752 base_model = create_model_from_schema(
2753 _normalize_schema_for_library(schema),
2754 allow_undefined_array_items=True,
2755 allow_undefined_type=True,
2756 )
2757 except (SchemaError, CombinerError) as e:
2758 logger.debug(
2759 f"Library schema conversion failed: {e}, preserving exact validation"
2760 )
2761 return _with_exact_validation_annotation(
2762 t.Any,
2763 schema,
2764 document_root,
2765 )
2766 except Exception as e:
2767 logger.debug(
2768 f"Unexpected error in schema conversion: {e}, preserving exact validation"
2769 )
2770 return _with_exact_validation_annotation(
2771 t.Any,
2772 schema,
2773 document_root,
2774 )
2775
2776 base_model = _relax_exact_model_fields(schema, base_model)
2777 return _with_exact_validation(
2778 apply_object_policy(
2779 schema,
2780 _apply_nested_object_policies(schema, base_model, document_root),
2781 root_schema=document_root,
2782 ),
2783 schema,
2784 document_root,
2785 )
2786
2787 try:
2788 # Handle top-level combiner without type (e.g., {"anyOf": [...]})
2789 if (
2790 any(k in schema for k in ("anyOf", "allOf", "oneOf"))
2791 and "type" not in schema
2792 ):
2793 return _handle_toplevel_combiner(schema, root_schema=root_schema)
2794
2795 # For array schemas
2796 if schema.get("type") == "array":
2797 items = schema.get("items")
2798 if _is_unsatisfiable_schema(items):
2799 return t.List[_UnsatisfiableSchema] # type: ignore[return-value]
2800 if items:
2801 item_type = _filtered_schema_to_pydantic_type(
2802 items,
2803 root_schema=root_schema,
2804 )
2805 return t.List[t.cast(t.Type, item_type)] # type: ignore
2806 return t.List
2807
2808 # Fallback to simple type
2809 return _convert_simple_type(schema)
2810
2811 except (SchemaError, CombinerError) as e:
2812 logger.debug(
2813 f"Library schema conversion failed: {e}, preserving exact validation"
2814 )
2815 document_root = root_schema if root_schema is not None else schema
2816 return _with_exact_validation_annotation(t.Any, schema, document_root)
2817 except Exception as e:
2818 logger.debug(
2819 f"Unexpected error in schema conversion: {e}, preserving exact validation"
2820 )
2821 document_root = root_schema if root_schema is not None else schema
2822 return _with_exact_validation_annotation(t.Any, schema, document_root)
2823
2824
2825 def _handle_toplevel_combiner(
2826 schema: t.Dict[str, t.Any],
2827 *,
2828 root_schema: t.Optional[t.Dict[str, t.Any]] = None,
2829 ) -> t.Union[t.Type, t.Any]:
2830 """
2831 Handle top-level combiner schemas (anyOf, allOf, oneOf without "type").
2832
2833 The library can handle these directly - it returns the appropriate type.
2834 """
2835 try:
2836 # Try direct conversion - library handles anyOf/oneOf/allOf at top level
2837 result = create_model_from_schema(
2838 _normalize_schema_for_library(schema),
2839 allow_undefined_array_items=True,
2840 allow_undefined_type=True,
2841 )
2842 if result is type(None):
2843 return type(None)
2844 if "allOf" in schema:
2845 options = schema.get("allOf")
2846 requires_object = isinstance(options, list) and any(
2847 isinstance(option, dict) and option.get("type") == "object"
2848 for option in options
2849 )
2850 if (
2851 requires_object
2852 and isinstance(result, type)
2853 and issubclass(result, BaseModel)
2854 ):
2855 document_root = root_schema if root_schema is not None else schema
2856 return _with_exact_validation(result, schema, document_root)
2857
2858 # The library models scalar intersections as object models.
2859 # Preserve the JSON value and let exact validation own acceptance.
2860 return t.Any
2861 # A library model for anyOf/oneOf drops sibling assertions such as
2862 # `required` and exclusive oneOf matching; let the source schema own it.
2863 if isinstance(result, type) and issubclass(result, BaseModel):
2864 document_root = root_schema if root_schema is not None else schema
2865 return _with_exact_validation(result, schema, document_root)
2866 return result
2867 except (SchemaError, CombinerError):
2868 pass
2869 except Exception:
2870 pass
2871
2872 # Fallback: manually build union type for anyOf/oneOf
2873 if "anyOf" in schema or "oneOf" in schema:
2874 options = schema.get("anyOf", schema.get("oneOf", []))
2875 return _build_union_from_options(options, root_schema=root_schema)
2876
2877 # Fallback: use first option for allOf
2878 if "allOf" in schema and schema["allOf"]:
2879 return json_schema_to_pydantic_type(
2880 schema["allOf"][0],
2881 root_schema=root_schema,
2882 )
2883
2884 return t.Any
2885
2886
2887 def _build_union_from_options(
2888 options: t.List[t.Dict[str, t.Any]],
2889 *,
2890 root_schema: t.Optional[t.Dict[str, t.Any]] = None,
2891 ) -> t.Type:
2892 """Build a Union type from a list of schema options."""
2893 pydantic_types = []
2894 null_type = PYDANTIC_TYPE_TO_PYTHON_TYPE.get("null")
2895 has_null = False
2896
2897 for option in options:
2898 ptype = json_schema_to_pydantic_type(option, root_schema=root_schema)
2899 if ptype is None:
2900 continue
2901 if ptype == null_type or ptype is type(None):
2902 has_null = True
2903 continue
2904 pydantic_types.append(ptype)
2905
2906 if len(pydantic_types) == 0:
2907 return type(None) if has_null else _UnsatisfiableSchema
2908
2909 if len(pydantic_types) == 1:
2910 base_type = pydantic_types[0]
2911 if has_null:
2912 return t.Optional[t.cast(t.Type, base_type)] # type: ignore
2913 return base_type
2914
2915 # Build union
2916 cast_types = [t.cast(t.Type, ptype) for ptype in pydantic_types]
2917 union_type = reduce(lambda a, b: t.Union[a, b], cast_types) # type: ignore
2918 if has_null:
2919 return t.Optional[union_type] # type: ignore
2920 return union_type