@devries-cameron20 / composio-1 / commits / bbeacac65
Fork 0

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

fix(python): preserve and police dynamic object content in schema conversion

`json_schema_to_model` and `json_schema_to_pydantic_type` now share one compiled object policy, so provider-facing Pydantic models stop silently discarding valid free-form arguments and stop ignoring `patternProperties` and explicit `additionalProperties` controls. Accepted dynamic keys are written back into `__pydantic_extra__`, which is what `LangchainProvider.wrap_tool` re-reads with `getattr`. Acceptance and rejection are asserted from the shared cross-SDK corpus, so the Python entry points cannot diverge from the TypeScript converters.

jkomyno committed Aug 6, 2026 at 20:40 UTC bbeacac65363bfedd9c0fa8135dd73f6a1e1a7b0
6 files changed +524 -5
python/composio/utils/schema_converter.py
+224 -4
@@ -9,6 +9,7 @@ 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 re
13 import typing as t
14 from functools import reduce
15
@@ -17,13 +18,22 @@ from json_schema_to_pydantic import (
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."""
@@ -221,6 +231,208 @@ def _contains_unsatisfiable_schema(schema: t.Any) -> bool:
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]]:
@@ -354,12 +566,20 @@ def _convert_with_library(
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
python/composio/utils/shared.py
+8 -1
@@ -22,6 +22,7 @@ from composio.utils.schema_converter import (
22 CONTAINER_TYPE,
23 FALLBACK_VALUES,
24 PYDANTIC_TYPE_TO_PYTHON_TYPE,
25 + apply_object_policy,
26 json_schema_to_pydantic_type,
27 )
28
@@ -457,7 +458,13 @@ def json_schema_to_model(
458 skip_default=skip_default,
459 )
460 field_definitions[updated_name] = (pydantic_type, pydantic_field)
460 - return create_model(model_name, **field_definitions) # type: ignore
461 + # The dynamic-key policy is shared with `json_schema_to_pydantic_type` so the
462 + # two entry points cannot disagree about which arguments survive conversion.
463 + return apply_object_policy(
464 + json_schema,
465 + create_model(model_name, **field_definitions), # type: ignore
466 + model_name=model_name,
467 + )
468
469
470 def pydantic_model_from_param_schema(param_schema: t.Dict) -> t.Type:
python/tests/fixtures/json_schema_conversion_corpus.py new
+127
@@ -0,0 +1,127 @@
1 +"""Decoder for the shared cross-SDK JSON Schema conversion corpus.
2 +
3 +The corpus is one logical fixture with a byte-identical copy per language. This
4 +module resolves the Python copy module-relatively and decodes it into typed
5 +models, so a malformed corpus fails at the test boundary rather than surfacing
6 +as a confusing assertion error deep inside a converter test.
7 +"""
8 +
9 +import json
10 +import typing as t
11 +from pathlib import Path
12 +
13 +from pydantic import BaseModel, ConfigDict, Field
14 +
15 +CORPUS_PATH = Path(__file__).parent / "json-schema-conversion" / "object-cases.json"
16 +
17 +Language = t.Literal["zod", "effect", "python"]
18 +LANGUAGES: t.Tuple[Language, ...] = ("zod", "effect", "python")
19 +
20 +_UNSET = object()
21 +
22 +
23 +class LanguageExpectation(BaseModel):
24 + """Per-language expectation. `output` is absent when the case does not pin one."""
25 +
26 + model_config = ConfigDict(extra="forbid")
27 +
28 + output: t.Any = _UNSET
29 + accepted: t.Optional[bool] = None
30 +
31 + @property
32 + def has_output(self) -> bool:
33 + return self.output is not _UNSET
34 +
35 +
36 +class Divergence(BaseModel):
37 + model_config = ConfigDict(extra="forbid")
38 +
39 + reason: str = Field(min_length=1)
40 +
41 +
42 +class CorpusInstance(BaseModel):
43 + model_config = ConfigDict(extra="forbid")
44 +
45 + input: t.Dict[str, t.Any]
46 + accepted: bool
47 + zod: t.Optional[LanguageExpectation] = None
48 + effect: t.Optional[LanguageExpectation] = None
49 + python: t.Optional[LanguageExpectation] = None
50 + divergence: t.Optional[Divergence] = None
51 +
52 + def accepted_for(self, language: Language) -> bool:
53 + """Acceptance for one language: the shared flag unless a declared divergence overrides it."""
54 + expectation: t.Optional[LanguageExpectation] = getattr(self, language)
55 + if expectation is not None and expectation.accepted is not None:
56 + return expectation.accepted
57 + return self.accepted
58 +
59 +
60 +class Ingress(BaseModel):
61 + model_config = ConfigDict(extra="forbid")
62 +
63 + accepted: bool
64 + preserved: t.Dict[str, t.Any]
65 +
66 +
67 +class CorpusCase(BaseModel):
68 + model_config = ConfigDict(extra="forbid")
69 +
70 + id: str = Field(min_length=1)
71 + description: t.Optional[str] = None
72 + divergesFromJsonSchema: t.Optional[str] = None # noqa: N815
73 + schema_: t.Dict[str, t.Any] = Field(alias="schema")
74 + ingress: t.Optional[Ingress] = None
75 + instances: t.List[CorpusInstance] = Field(min_length=1)
76 +
77 +
78 +class Corpus(BaseModel):
79 + model_config = ConfigDict(extra="forbid")
80 +
81 + cases: t.List[CorpusCase] = Field(min_length=1)
82 +
83 +
84 +def assert_corpus_invariants(cases: t.Sequence[CorpusCase]) -> None:
85 + """Ids are unique, and a language may only disagree about acceptance when it says why."""
86 + seen: t.Set[str] = set()
87 + for case in cases:
88 + if case.id in seen:
89 + raise ValueError(f"Duplicate corpus case id: {case.id}")
90 + seen.add(case.id)
91 +
92 + for index, instance in enumerate(case.instances):
93 + for language in LANGUAGES:
94 + expectation: t.Optional[LanguageExpectation] = getattr(
95 + instance, language
96 + )
97 + override = expectation.accepted if expectation else None
98 + if (
99 + override is not None
100 + and override != instance.accepted
101 + and instance.divergence is None
102 + ):
103 + raise ValueError(
104 + f"Corpus case {case.id} instance {index} overrides {language} "
105 + "acceptance without a divergence reason"
106 + )
107 +
108 +
109 +_cached: t.Optional[t.List[CorpusCase]] = None
110 +
111 +
112 +def load_object_cases() -> t.List[CorpusCase]:
113 + """Decode the shared corpus and enforce its cross-language invariants."""
114 + global _cached
115 + if _cached is None:
116 + cases = Corpus.model_validate(json.loads(CORPUS_PATH.read_text())).cases
117 + assert_corpus_invariants(cases)
118 + _cached = cases
119 + return _cached
120 +
121 +
122 +def find_case(case_id: str) -> CorpusCase:
123 + """Look up one case by id, failing loudly when the corpus no longer carries it."""
124 + for case in load_object_cases():
125 + if case.id == case_id:
126 + return case
127 + raise LookupError(f"Corpus is missing the {case_id} case")
python/tests/test_provider.py
+79
@@ -15,7 +15,10 @@ from composio.client.types import Tool, tool_list_response
15 from composio.core.models.base import allow_tracking
16 from composio.core.models.tools import Tools
17 from composio.core.provider import AgenticProvider, NonAgenticProvider
18 +from pydantic import ValidationError
19 +
20 from tests.conftest import mock_http_client
21 +from tests.fixtures.json_schema_conversion_corpus import find_case
22
23
24 @pytest.fixture(autouse=True)
@@ -902,6 +905,82 @@ class TestLangchainReservedKeywords:
905 assert captured == {"from": "2024-01-01"}
906
907
908 +class TestLangchainFreeFormObjectArguments:
909 + """Regression for issue #4064 at the Python provider boundary.
910 +
911 + `LangchainProvider.wrap_tool` builds its `args_schema` from
912 + `json_schema_to_model` and re-reads each argument with `getattr`, so a
913 + property-less object that validates but drops its content never reaches
914 + execution.
915 + """
916 +
917 + def _make_tool(self, properties: dict):
918 + return Tool(
919 + name="Metabase Create Card",
920 + slug="METABASE_POST_API_CARD",
921 + description="A tool with a free-form object argument",
922 + input_parameters={
923 + "type": "object",
924 + "title": "MetabaseCreateCardRequest",
925 + "properties": properties,
926 + "required": list(properties),
927 + },
928 + output_parameters={},
929 + available_versions=["12012025_00"],
930 + version="12012025_00",
931 + scopes=[],
932 + toolkit=tool_list_response.ItemToolkit(
933 + name="Metabase", slug="metabase", logo=""
934 + ),
935 + deprecated=tool_list_response.ItemDeprecated(
936 + available_versions=["12012025_00"],
937 + displayName="Metabase Create Card",
938 + version="12012025_00",
939 + toolkit=tool_list_response.ItemDeprecatedToolkit(logo=""),
940 + is_deprecated=False,
941 + ),
942 + is_deprecated=False,
943 + no_auth=False,
944 + tags=[],
945 + )
946 +
947 + def test_free_form_object_reaches_execution_intact(self):
948 + from composio_langchain import LangchainProvider
949 +
950 + case = find_case("nested-free-form-object")
951 + payload = case.instances[0].input
952 +
953 + captured = {}
954 +
955 + def mock_execute(slug, arguments):
956 + captured.update(arguments)
957 + return {"data": {}, "error": None, "successful": True}
958 +
959 + wrapped = LangchainProvider().wrap_tool(
960 + self._make_tool(case.schema_["properties"]), mock_execute
961 + )
962 + wrapped.run(payload)
963 +
964 + assert captured == payload
965 +
966 + def test_unknown_keys_are_still_rejected_before_execution(self):
967 + from composio_langchain import LangchainProvider
968 +
969 + executed = []
970 +
971 + def mock_execute(slug, arguments):
972 + executed.append(arguments)
973 + return {"data": {}, "error": None, "successful": True}
974 +
975 + wrapped = LangchainProvider().wrap_tool(
976 + self._make_tool({"name": {"type": "string"}}), mock_execute
977 + )
978 +
979 + with pytest.raises(ValidationError):
980 + wrapped.args_schema.model_validate({"name": "a", "typo": 1})
981 + assert executed == []
982 +
983 +
984 class TestProviderEdgeCases:
985 """Test edge cases and error handling for providers."""
986
python/tests/test_schema_converter.py
+37
@@ -8,6 +8,7 @@ from composio.utils.schema_converter import (
8 json_schema_to_pydantic_type,
9 )
10 from composio.utils.shared import json_schema_to_model
11 +from tests.fixtures.json_schema_conversion_corpus import load_object_cases
12
13
14 def test_true_becomes_empty_schema():
@@ -194,3 +195,39 @@ def test_schema_converter_required_unsatisfiable_property_has_no_valid_value():
195 for impossible_value in (None, "some string", 123):
196 with pytest.raises(ValidationError):
197 model(impossible=impossible_value)
198 +
199 +
200 +@pytest.mark.parametrize(
201 + "case,index",
202 + [
203 + (case, index)
204 + for case in load_object_cases()
205 + for index in range(len(case.instances))
206 + ],
207 + ids=[
208 + f"{case.id}[{index}]"
209 + for case in load_object_cases()
210 + for index in range(len(case.instances))
211 + ],
212 +)
213 +def test_shared_object_corpus_through_json_schema_to_pydantic_type(case, index):
214 + """`json_schema_to_pydantic_type` must agree with `json_schema_to_model`.
215 +
216 + The two entry points return different things — a Pydantic type versus a
217 + `BaseModel` subclass — so the type is exercised through a `TypeAdapter` and
218 + compared as a JSON-shaped value.
219 + """
220 + instance = case.instances[index]
221 + adapter = TypeAdapter(json_schema_to_pydantic_type(case.schema_))
222 +
223 + if not instance.accepted_for("python"):
224 + with pytest.raises(ValidationError):
225 + adapter.validate_python(instance.input)
226 + return
227 +
228 + result = adapter.validate_python(instance.input)
229 + if instance.python is not None and instance.python.has_output:
230 + assert (
231 + adapter.dump_python(result, mode="json", by_alias=True)
232 + == instance.python.output
233 + )
python/tests/test_schema_parser.py
+49
@@ -11,6 +11,8 @@ import pytest
11 from pydantic import BaseModel, TypeAdapter, ValidationError
12 from pydantic.fields import PydanticUndefined
13
14 +from tests.fixtures.json_schema_conversion_corpus import find_case, load_object_cases
15 +
16 from composio.utils.shared import (
17 get_signature_format_from_schema_params,
18 json_schema_to_fields_dict,
@@ -1715,3 +1717,50 @@ class TestGetSignatureFormatFromSchemaParams:
1717
1718 if __name__ == "__main__":
1719 pytest.main([__file__, "-v"])
1720 +
1721 +
1722 +class TestSharedObjectCorpusThroughJsonSchemaToModel:
1723 + """`json_schema_to_model` must satisfy the shared cross-SDK object contract.
1724 +
1725 + This is the provider-facing entry point (`args_schema`), so a model that
1726 + accepts an argument but discards it is as much a defect as one that rejects
1727 + a valid payload.
1728 + """
1729 +
1730 + @pytest.mark.parametrize(
1731 + "case,index",
1732 + [
1733 + (case, index)
1734 + for case in load_object_cases()
1735 + for index in range(len(case.instances))
1736 + ],
1737 + ids=[
1738 + f"{case.id}[{index}]"
1739 + for case in load_object_cases()
1740 + for index in range(len(case.instances))
1741 + ],
1742 + )
1743 + def test_case(self, case, index: int) -> None:
1744 + instance = case.instances[index]
1745 + model = json_schema_to_model(case.schema_)
1746 +
1747 + if not instance.accepted_for("python"):
1748 + with pytest.raises(ValidationError):
1749 + model.model_validate(instance.input)
1750 + return
1751 +
1752 + result = model.model_validate(instance.input)
1753 + if instance.python is not None and instance.python.has_output:
1754 + assert (
1755 + result.model_dump(mode="json", by_alias=True) == instance.python.output
1756 + )
1757 +
1758 + def test_free_form_content_is_readable_as_attributes(self) -> None:
1759 + """Preserved dynamic keys must survive `getattr`, not just `model_dump`."""
1760 + case = find_case("root-free-form-absent-properties")
1761 + model = json_schema_to_model(case.schema_)
1762 +
1763 + result = model.model_validate(case.instances[0].input)
1764 +
1765 + assert getattr(result, "anything") == {"a": 1}
1766 + assert getattr(result, "other") == "x"