9
We handle those by pre-filtering them before passing to the library.
10
"""
11
12
+import re
13
import typing as t
14
from functools import reduce
15
18
SchemaError,
19
create_model as create_model_from_schema,
20
)
20
-from pydantic import Field, create_model as create_pydantic_model
21
+from pydantic import (
22
+ BaseModel,
23
+ ConfigDict,
24
+ Field,
25
+ TypeAdapter,
26
+ create_model as create_pydantic_model,
27
+ model_validator,
28
+)
29
from pydantic_core import core_schema
30
31
from composio.utils.logging import get as get_logger
32
33
logger = get_logger(__name__)
34
35
+_MISSING = object()
36
+
37
38
class _UnsatisfiableSchema:
39
"""Pydantic type for JSON schemas that reject every value."""
231
return False
232
233
234
+_PRIMITIVE_TYPES = frozenset({"string", "number", "integer", "boolean", "null"})
235
+
236
+# JSON Schema keyword -> Pydantic `Field` constraint, by instance type. Simple
237
+# primitives lose their constraints in `PYDANTIC_TYPE_TO_PYTHON_TYPE`, which is
238
+# fine for declared fields the library models but not for dynamic keys, where
239
+# this module is the only validator.
240
+_CONSTRAINTS: t.Dict[str, t.Dict[str, str]] = {
241
+ "string": {
242
+ "minLength": "min_length",
243
+ "maxLength": "max_length",
244
+ "pattern": "pattern",
245
+ },
246
+ "array": {"minItems": "min_length", "maxItems": "max_length"},
247
+}
248
+_NUMERIC_CONSTRAINTS = {
249
+ "minimum": "ge",
250
+ "maximum": "le",
251
+ "exclusiveMinimum": "gt",
252
+ "exclusiveMaximum": "lt",
253
+ "multipleOf": "multiple_of",
254
+}
255
+_CONSTRAINTS["number"] = _NUMERIC_CONSTRAINTS
256
+_CONSTRAINTS["integer"] = _NUMERIC_CONSTRAINTS
257
+
258
+
259
+class _DynamicKeyValidator(t.NamedTuple):
260
+ """Validator for one dynamic-key schema, plus how strictly to apply it."""
261
+
262
+ adapter: TypeAdapter
263
+ strict: bool
264
+
265
+ def coerce(self, value: t.Any) -> t.Any:
266
+ return self.adapter.dump_python(
267
+ self.adapter.validate_python(value, strict=self.strict)
268
+ )
269
+
270
+
271
+class _ObjectPolicy(t.NamedTuple):
272
+ """Compiled dynamic-key policy for one JSON Schema object node.
273
+
274
+ This is the single place the covered object contract lives, so
275
+ ``json_schema_to_model`` and ``json_schema_to_pydantic_type`` cannot drift
276
+ apart on which keys are accepted, validated, or preserved.
277
+ """
278
+
279
+ declared: t.FrozenSet[str]
280
+ patterns: t.Tuple[t.Tuple[t.Pattern[str], t.Optional[_DynamicKeyValidator]], ...]
281
+ additional: t.Optional[_DynamicKeyValidator]
282
+ rejects_unmatched: bool
283
+
284
+ @property
285
+ def is_trivial(self) -> bool:
286
+ """No dynamic keys to police and nothing to reject."""
287
+ return (
288
+ not self.patterns and not self.rejects_unmatched and self.additional is None
289
+ )
290
+
291
+
292
+def object_is_open(schema: t.Dict[str, t.Any]) -> bool:
293
+ """Whether an object schema accepts arbitrary keys it says nothing about.
294
+
295
+ An object is open when it declares no named properties and either omits
296
+ ``additionalProperties`` or sets it to ``true``. Objects that *do* name
297
+ properties stay strict on omission, matching the TypeScript converters.
298
+ """
299
+ additional = schema.get("additionalProperties", _MISSING)
300
+ return not (schema.get("properties") or {}) and additional in (_MISSING, True)
301
+
302
+
303
+def _dynamic_key_validator(schema: t.Any) -> t.Optional[_DynamicKeyValidator]:
304
+ """Build the validator for one dynamic-key schema, or ``None`` if unmodellable."""
305
+ try:
306
+ annotation: t.Any = json_schema_to_pydantic_type(schema)
307
+ strict = False
308
+
309
+ if isinstance(schema, dict):
310
+ schema_type = schema.get("type") or ""
311
+ constraints = {
312
+ field: schema[keyword]
313
+ for keyword, field in _CONSTRAINTS.get(schema_type, {}).items()
314
+ if keyword in schema
315
+ }
316
+ if constraints:
317
+ annotation = t.Annotated[annotation, Field(**constraints)]
318
+ # Primitives validate strictly so Pydantic's lax coercion cannot
319
+ # admit a value the TypeScript converters reject (`true` as a
320
+ # number, `1` as a boolean).
321
+ strict = schema_type in _PRIMITIVE_TYPES
322
+
323
+ return _DynamicKeyValidator(TypeAdapter(annotation), strict)
324
+ except Exception as exc: # pragma: no cover - defensive
325
+ logger.debug(f"Could not build a dynamic-key validator for {schema!r}: {exc}")
326
+ return None
327
+
328
+
329
+def _compile_object_policy(schema: t.Dict[str, t.Any]) -> _ObjectPolicy:
330
+ declared = frozenset(schema.get("properties") or {})
331
+ additional = schema.get("additionalProperties", _MISSING)
332
+
333
+ patterns = []
334
+ for pattern, pattern_schema in (schema.get("patternProperties") or {}).items():
335
+ try:
336
+ patterns.append(
337
+ (re.compile(pattern), _dynamic_key_validator(pattern_schema))
338
+ )
339
+ except re.error:
340
+ logger.debug(f"Skipping uncompilable patternProperties regex: {pattern!r}")
341
+
342
+ return _ObjectPolicy(
343
+ declared=declared,
344
+ patterns=tuple(patterns),
345
+ additional=(
346
+ _dynamic_key_validator(additional) if isinstance(additional, dict) else None
347
+ ),
348
+ rejects_unmatched=additional is False
349
+ or (additional is _MISSING and bool(declared)),
350
+ )
351
+
352
+
353
+class _DynamicObjectModel(BaseModel):
354
+ """Base for object models that must police and preserve dynamic keys.
355
+
356
+ ``extra="allow"`` is what makes preservation possible at all: Pydantic's
357
+ default silently discards accepted arguments, so a provider that re-reads
358
+ them (``LangchainProvider`` uses ``getattr``) would lose valid payloads.
359
+ Coerced values are written back into ``__pydantic_extra__`` so they show up
360
+ in ``model_dump()`` and remain readable as attributes.
361
+ """
362
+
363
+ model_config = ConfigDict(extra="allow")
364
+
365
+ @model_validator(mode="after")
366
+ def _apply_object_policy(self):
367
+ policy: t.Optional[_ObjectPolicy] = getattr(
368
+ type(self), "__composio_object_policy__", None
369
+ )
370
+ if policy is None or policy.is_trivial:
371
+ return self
372
+
373
+ # A key declared in `properties` is still checked against every matching
374
+ # pattern; its declared value is authoritative and is not overwritten.
375
+ for name, field in type(self).model_fields.items():
376
+ if name not in self.model_fields_set:
377
+ continue
378
+ key = field.alias or name
379
+ for regex, validator in policy.patterns:
380
+ if validator is not None and regex.search(key):
381
+ validator.coerce(getattr(self, name))
382
+
383
+ extra = self.__pydantic_extra__
384
+ if not extra:
385
+ return self
386
+
387
+ unrecognized = []
388
+ for key, value in list(extra.items()):
389
+ matched = False
390
+ for regex, validator in policy.patterns:
391
+ if regex.search(key):
392
+ matched = True
393
+ if validator is not None:
394
+ extra[key] = validator.coerce(value)
395
+
396
+ if matched or key in policy.declared:
397
+ continue
398
+
399
+ if policy.rejects_unmatched:
400
+ unrecognized.append(key)
401
+ elif policy.additional is not None:
402
+ extra[key] = policy.additional.coerce(value)
403
+
404
+ if unrecognized:
405
+ raise ValueError(
406
+ f"Unrecognized key(s) in object: {', '.join(repr(key) for key in unrecognized)}"
407
+ )
408
+
409
+ return self
410
+
411
+
412
+def apply_object_policy(
413
+ schema: t.Dict[str, t.Any],
414
+ base_model: t.Type[BaseModel],
415
+ model_name: t.Optional[str] = None,
416
+) -> t.Type[BaseModel]:
417
+ """Wrap ``base_model`` with the covered dynamic-key policy for ``schema``."""
418
+ policy = _compile_object_policy(schema)
419
+ name = model_name or base_model.__name__
420
+
421
+ return t.cast(
422
+ t.Type[BaseModel],
423
+ type(
424
+ name,
425
+ (_DynamicObjectModel, base_model),
426
+ {
427
+ "__module__": __name__,
428
+ "__qualname__": name,
429
+ "model_config": ConfigDict(extra="allow"),
430
+ "__composio_object_policy__": policy,
431
+ },
432
+ ),
433
+ )
434
+
435
+
436
def json_schema_to_pydantic_type(
437
json_schema: t.Union[t.Dict[str, t.Any], bool],
438
) -> t.Union[t.Type, t.Optional[t.Any]]:
566
if schema.get("type") == "object":
567
if _contains_unsatisfiable_schema(schema.get("properties", {})):
568
return _convert_object_with_unsatisfiable_properties(schema)
569
+ # A property-less object with no dynamic-key constraints accepts and
570
+ # preserves arbitrary content. Modelling it as an empty Pydantic
571
+ # model instead silently discarded every key (issue #4064).
572
+ if object_is_open(schema) and not schema.get("patternProperties"):
573
+ return t.cast(t.Type, t.Dict[str, t.Any])
574
if "title" not in schema:
575
schema = {**schema, "title": "GeneratedModel"}
359
- return create_model_from_schema(
576
+ return apply_object_policy(
577
schema,
361
- allow_undefined_array_items=True,
362
- allow_undefined_type=True,
578
+ create_model_from_schema(
579
+ schema,
580
+ allow_undefined_array_items=True,
581
+ allow_undefined_type=True,
582
+ ),
583
)
584
585
# For array schemas