@devries-cameron20 / composio-1 / commits / 44a4df818
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): handle list-valued type when coercing schema defaults (#5051)

This PR: - fixes #5050: `json_schema_to_model()` crashes with `TypeError: unhashable type: 'list'` for a nullable property such as `{"type": ["string", "null"], "default": "asc"}`. - collects expected types from single names and type arrays, including `anyOf`, `oneOf`, and `allOf` branches, while preserving the existing default-coercion precedence. - adds five regression cases covering nullable string/boolean defaults and all three combiners. - independently reproduces the public model-conversion failure on current `next` and verifies the patched model accepts both string and null values. - passes `.venv/bin/python -m pytest python/tests/test_schema_parser.py -q`: 246 tests, and `.venv/bin/mypy --config-file python/config/mypy.ini python/composio/utils/shared.py`. - passes Ruff check and formatting checks for both changed Python files using `python/config/ruff.toml`. Updated with current `next`. Validation used an isolated environment with the declared `composio-client==2.0.0rc23` and relevant versions from `uv.lock`. Python-only changes do not need a Changeset. --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: jkomyno <alberto@composio.dev>

r2jaff committed Nov 2, 2026 at 01:32 UTC 44a4df81843bd4875e103107628b6f3f873436fe
2 files changed +79 -9
python/composio/utils/shared.py
+11 -9
@@ -427,19 +427,21 @@ def _coerce_default_value(
427 # Collect expected types from schema
428 expected_types: t.Set[t.Any] = set()
429
430 - if "type" in json_schema:
431 - py_type = PYDANTIC_TYPE_TO_PYTHON_TYPE.get(json_schema["type"])
432 - if py_type is not None:
433 - expected_types.add(py_type)
430 + def add_types(schema_type: t.Any) -> None:
431 + # `type` may be a single name or a list of names (e.g. ["string", "null"]).
432 + names = schema_type if isinstance(schema_type, list) else [schema_type]
433 + for type_name in names:
434 + if isinstance(type_name, str):
435 + py_type = PYDANTIC_TYPE_TO_PYTHON_TYPE.get(type_name)
436 + if py_type is not None:
437 + expected_types.add(py_type)
438 +
439 + add_types(json_schema.get("type"))
440
441 for combiner in ("anyOf", "oneOf", "allOf"):
442 for option in json_schema.get(combiner, []):
443 if isinstance(option, dict):
438 - option_type = option.get("type")
439 - if isinstance(option_type, str):
440 - py_type = PYDANTIC_TYPE_TO_PYTHON_TYPE.get(option_type)
441 - if py_type is not None:
442 - expected_types.add(py_type)
444 + add_types(option.get("type"))
445
446 # If string is expected, no coercion needed
447 if str in expected_types:
python/tests/test_schema_parser.py
+68
@@ -1296,6 +1296,74 @@ class TestBooleanDefaultCoercion:
1296 assert prop.get("default") == 1
1297 assert isinstance(prop.get("default"), int)
1298
1299 + @pytest.mark.unit
1300 + @pytest.mark.schema
1301 + def test_type_list_with_string_default(self):
1302 + """A list-valued `type` (e.g. ["string", "null"]) must not crash default coercion."""
1303 + json_schema = {
1304 + "title": "TestRequest",
1305 + "type": "object",
1306 + "properties": {
1307 + "order": {
1308 + "type": ["string", "null"],
1309 + "default": "asc",
1310 + "description": "Sort order",
1311 + },
1312 + },
1313 + }
1314 +
1315 + model_class = json_schema_to_model(json_schema)
1316 + generated_schema = model_class.model_json_schema()
1317 + prop = generated_schema["properties"]["order"]
1318 +
1319 + # String is an expected type, so the default stays a string
1320 + assert prop.get("default") == "asc"
1321 +
1322 + @pytest.mark.unit
1323 + @pytest.mark.schema
1324 + def test_type_list_boolean_null_with_string_default_coerced(self):
1325 + """Stringified defaults are coerced when `type` is a list."""
1326 + json_schema = {
1327 + "title": "TestRequest",
1328 + "type": "object",
1329 + "properties": {
1330 + "enabled": {
1331 + "type": ["boolean", "null"],
1332 + "default": "false",
1333 + "description": "Enable feature.",
1334 + },
1335 + },
1336 + }
1337 +
1338 + model_class = json_schema_to_model(json_schema)
1339 + generated_schema = model_class.model_json_schema()
1340 + prop = generated_schema["properties"]["enabled"]
1341 +
1342 + assert prop.get("default") is False
1343 +
1344 + @pytest.mark.unit
1345 + @pytest.mark.schema
1346 + @pytest.mark.parametrize("combiner", ["anyOf", "oneOf", "allOf"])
1347 + def test_type_list_inside_combiner_with_string_default_coerced(self, combiner):
1348 + """A list-valued `type` inside a combiner branch also drives coercion."""
1349 + json_schema = {
1350 + "title": "TestRequest",
1351 + "type": "object",
1352 + "properties": {
1353 + "limit": {
1354 + combiner: [{"type": ["integer", "null"]}],
1355 + "default": "5",
1356 + "description": "Page size.",
1357 + },
1358 + },
1359 + }
1360 +
1361 + model_class = json_schema_to_model(json_schema)
1362 + generated_schema = model_class.model_json_schema()
1363 + prop = generated_schema["properties"]["limit"]
1364 +
1365 + assert prop.get("default") == 5
1366 +
1367 @pytest.mark.unit
1368 @pytest.mark.schema
1369 def test_float_with_string_default_coerced(self):