@devries-cameron20 / composio-1 / commits / 5fd744dcf
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(py): coerce stringified default values to match JSON schema (#2506)

Co-authored-by: jkomyno <12381818+jkomyno@users.noreply.github.com>

Alberto Schiabel committed Jan 29, 2026 at 08:06 UTC 5fd744dcf365a4393a78e5d8853d0c5cbd2a5e3f
2 files changed +406
python/composio/utils/shared.py
+70
@@ -37,6 +37,72 @@ __all__ = [
37 reserved_names = ["validate"]
38
39
40 +def _coerce_default_value(
41 + default: t.Any,
42 + json_schema: t.Dict[str, t.Any],
43 +) -> t.Any:
44 + """
45 + Coerce a default value to match the expected type from JSON schema.
46 +
47 + Handles common mismatches where string defaults should be boolean/int/float.
48 + This fixes issues where API returns stringified defaults like "true" instead of true.
49 +
50 + Coercion precedence: boolean > integer > float. This means values like "1" and "0"
51 + become booleans when both bool and int are expected types.
52 +
53 + :param default: The default value from the JSON schema.
54 + :param json_schema: The JSON schema property definition.
55 + :return: The coerced default value, or original if no coercion possible.
56 + """
57 + if default is None or not isinstance(default, str):
58 + return default
59 +
60 + # Collect expected types from schema
61 + expected_types: t.Set[t.Any] = set()
62 +
63 + if "type" in json_schema:
64 + py_type = PYDANTIC_TYPE_TO_PYTHON_TYPE.get(json_schema["type"])
65 + if py_type is not None:
66 + expected_types.add(py_type)
67 +
68 + for combiner in ("anyOf", "oneOf", "allOf"):
69 + for option in json_schema.get(combiner, []):
70 + if isinstance(option, dict):
71 + option_type = option.get("type")
72 + if isinstance(option_type, str):
73 + py_type = PYDANTIC_TYPE_TO_PYTHON_TYPE.get(option_type)
74 + if py_type is not None:
75 + expected_types.add(py_type)
76 +
77 + # If string is expected, no coercion needed
78 + if str in expected_types:
79 + return default
80 +
81 + # Boolean coercion (takes precedence over int for "1"/"0")
82 + if bool in expected_types:
83 + lower_default = default.lower()
84 + if lower_default in ("true", "yes", "1"):
85 + return True
86 + if lower_default in ("false", "no", "0"):
87 + return False
88 +
89 + # Integer coercion
90 + if int in expected_types:
91 + try:
92 + return int(default)
93 + except ValueError:
94 + pass
95 +
96 + # Float coercion
97 + if float in expected_types:
98 + try:
99 + return float(default)
100 + except ValueError:
101 + pass
102 +
103 + return default
104 +
105 +
106 def json_schema_to_pydantic_field(
107 name: str,
108 json_schema: t.Dict[str, t.Any],
@@ -61,6 +127,10 @@ def json_schema_to_pydantic_field(
127 examples = json_schema.get("examples", [])
128 default = json_schema.get("default")
129
130 + # Coerce default value to match expected type from schema
131 + if default is not None:
132 + default = _coerce_default_value(default, json_schema)
133 +
134 # Check if the field name is a reserved Pydantic name
135 if name in reserved_names:
136 name = f"{name}_"
python/tests/test_schema_parser.py
+336
@@ -1099,5 +1099,341 @@ class TestBooleanSchemas:
1099 assert result.__origin__ is t.Union
1100
1101
1102 +class TestBooleanDefaultCoercion:
1103 + """Regression tests for PLEN-1311 - Boolean default type mismatch in LangchainProvider."""
1104 +
1105 + @pytest.mark.unit
1106 + @pytest.mark.schema
1107 + def test_anyof_boolean_null_with_boolean_default(self):
1108 + """
1109 + Test that anyOf [boolean, null] with boolean default preserves types.
1110 +
1111 + Regression test for PLEN-1311: GOOGLEDRIVE_FIND_FILE supportsAllDrives
1112 + field was incorrectly converted to string type with string default.
1113 + """
1114 + json_schema = {
1115 + "title": "FindFileRequest",
1116 + "type": "object",
1117 + "properties": {
1118 + "supportsAllDrives": {
1119 + "anyOf": [{"type": "boolean"}, {"type": "null"}],
1120 + "default": True,
1121 + "description": "Whether to search all drives.",
1122 + },
1123 + },
1124 + }
1125 +
1126 + model_class = json_schema_to_model(json_schema)
1127 +
1128 + # Verify the model accepts boolean values
1129 + instance = model_class(supportsAllDrives=True)
1130 + assert instance.supportsAllDrives is True
1131 +
1132 + instance_false = model_class(supportsAllDrives=False)
1133 + assert instance_false.supportsAllDrives is False
1134 +
1135 + # Verify the generated JSON schema preserves boolean type
1136 + generated_schema = model_class.model_json_schema()
1137 + prop = generated_schema["properties"]["supportsAllDrives"]
1138 +
1139 + # Should NOT be string type
1140 + assert prop.get("type") != "string"
1141 +
1142 + # Default should be boolean True, not string "true"
1143 + assert prop.get("default") is True
1144 +
1145 + @pytest.mark.unit
1146 + @pytest.mark.schema
1147 + def test_anyof_boolean_null_with_string_default_coerced(self):
1148 + """
1149 + Test that string "true"/"false" defaults are coerced to boolean.
1150 +
1151 + This handles cases where the API returns stringified boolean defaults.
1152 + """
1153 + json_schema = {
1154 + "title": "FindFileRequest",
1155 + "type": "object",
1156 + "properties": {
1157 + "supportsAllDrives": {
1158 + "anyOf": [{"type": "boolean"}, {"type": "null"}],
1159 + "default": "true", # String, should be coerced to True
1160 + "description": "Whether to search all drives.",
1161 + },
1162 + },
1163 + }
1164 +
1165 + model_class = json_schema_to_model(json_schema)
1166 +
1167 + # Verify the model accepts boolean values
1168 + instance = model_class(supportsAllDrives=True)
1169 + assert instance.supportsAllDrives is True
1170 +
1171 + # Verify the generated JSON schema has coerced default
1172 + generated_schema = model_class.model_json_schema()
1173 + prop = generated_schema["properties"]["supportsAllDrives"]
1174 +
1175 + # Should NOT have string type
1176 + assert prop.get("type") != "string"
1177 +
1178 + # Default should be coerced to boolean True
1179 + assert prop.get("default") is True
1180 +
1181 + @pytest.mark.unit
1182 + @pytest.mark.schema
1183 + def test_anyof_boolean_null_with_string_false_default_coerced(self):
1184 + """
1185 + Test that string "false" default is coerced to boolean False.
1186 + """
1187 + json_schema = {
1188 + "title": "TestRequest",
1189 + "type": "object",
1190 + "properties": {
1191 + "enabled": {
1192 + "anyOf": [{"type": "boolean"}, {"type": "null"}],
1193 + "default": "false", # String, should be coerced to False
1194 + "description": "Enable feature.",
1195 + },
1196 + },
1197 + }
1198 +
1199 + model_class = json_schema_to_model(json_schema)
1200 + generated_schema = model_class.model_json_schema()
1201 + prop = generated_schema["properties"]["enabled"]
1202 +
1203 + # Default should be coerced to boolean False
1204 + assert prop.get("default") is False
1205 +
1206 + @pytest.mark.unit
1207 + @pytest.mark.schema
1208 + def test_integer_with_string_default_coerced(self):
1209 + """Test that string integer defaults are coerced."""
1210 + json_schema = {
1211 + "title": "TestRequest",
1212 + "type": "object",
1213 + "properties": {
1214 + "page": {
1215 + "type": "integer",
1216 + "default": "1", # String, should be coerced to 1
1217 + "description": "Page number",
1218 + },
1219 + },
1220 + }
1221 +
1222 + model_class = json_schema_to_model(json_schema)
1223 + generated_schema = model_class.model_json_schema()
1224 + prop = generated_schema["properties"]["page"]
1225 +
1226 + # Default should be integer 1, not string "1"
1227 + assert prop.get("default") == 1
1228 + assert isinstance(prop.get("default"), int)
1229 +
1230 + @pytest.mark.unit
1231 + @pytest.mark.schema
1232 + def test_float_with_string_default_coerced(self):
1233 + """Test that string float defaults are coerced."""
1234 + json_schema = {
1235 + "title": "TestRequest",
1236 + "type": "object",
1237 + "properties": {
1238 + "rate": {
1239 + "type": "number",
1240 + "default": "3.14", # String, should be coerced to 3.14
1241 + "description": "Rate value",
1242 + },
1243 + },
1244 + }
1245 +
1246 + model_class = json_schema_to_model(json_schema)
1247 + generated_schema = model_class.model_json_schema()
1248 + prop = generated_schema["properties"]["rate"]
1249 +
1250 + # Default should be float 3.14, not string "3.14"
1251 + assert prop.get("default") == 3.14
1252 + assert isinstance(prop.get("default"), float)
1253 +
1254 + @pytest.mark.unit
1255 + @pytest.mark.schema
1256 + def test_boolean_default_not_coerced_when_already_correct(self):
1257 + """Test that boolean defaults that are already correct are not modified."""
1258 + json_schema = {
1259 + "title": "TestRequest",
1260 + "type": "object",
1261 + "properties": {
1262 + "enabled": {
1263 + "type": "boolean",
1264 + "default": True, # Already boolean
1265 + "description": "Enable feature.",
1266 + },
1267 + },
1268 + }
1269 +
1270 + model_class = json_schema_to_model(json_schema)
1271 + generated_schema = model_class.model_json_schema()
1272 + prop = generated_schema["properties"]["enabled"]
1273 +
1274 + assert prop.get("default") is True
1275 + assert isinstance(prop.get("default"), bool)
1276 +
1277 + @pytest.mark.unit
1278 + @pytest.mark.schema
1279 + def test_string_default_not_coerced_for_string_type(self):
1280 + """Test that string defaults for string type fields are preserved."""
1281 + json_schema = {
1282 + "title": "TestRequest",
1283 + "type": "object",
1284 + "properties": {
1285 + "name": {
1286 + "type": "string",
1287 + "default": "true", # String value, should stay as string
1288 + "description": "Name field",
1289 + },
1290 + },
1291 + }
1292 +
1293 + model_class = json_schema_to_model(json_schema)
1294 + generated_schema = model_class.model_json_schema()
1295 + prop = generated_schema["properties"]["name"]
1296 +
1297 + # Default should stay as string "true"
1298 + assert prop.get("default") == "true"
1299 + assert isinstance(prop.get("default"), str)
1300 +
1301 + @pytest.mark.unit
1302 + @pytest.mark.schema
1303 + def test_allof_boolean_with_string_default_coerced(self):
1304 + """Test that allOf with boolean type coerces string default."""
1305 + json_schema = {
1306 + "title": "TestRequest",
1307 + "type": "object",
1308 + "properties": {
1309 + "flag": {
1310 + "allOf": [{"type": "boolean"}],
1311 + "default": "true",
1312 + "description": "Flag field",
1313 + },
1314 + },
1315 + }
1316 +
1317 + model_class = json_schema_to_model(json_schema)
1318 + generated_schema = model_class.model_json_schema()
1319 + prop = generated_schema["properties"]["flag"]
1320 +
1321 + # Default should be coerced to boolean True
1322 + assert prop.get("default") is True
1323 +
1324 + @pytest.mark.unit
1325 + @pytest.mark.schema
1326 + def test_invalid_boolean_string_not_coerced(self):
1327 + """Test that invalid boolean strings are not coerced and return as-is."""
1328 + json_schema = {
1329 + "title": "TestRequest",
1330 + "type": "object",
1331 + "properties": {
1332 + "flag": {
1333 + "anyOf": [{"type": "boolean"}, {"type": "null"}],
1334 + "default": "invalid", # Not a valid boolean string
1335 + "description": "Flag field",
1336 + },
1337 + },
1338 + }
1339 +
1340 + model_class = json_schema_to_model(json_schema)
1341 + generated_schema = model_class.model_json_schema()
1342 + prop = generated_schema["properties"]["flag"]
1343 +
1344 + # Default should remain as string "invalid" since it can't be coerced
1345 + assert prop.get("default") == "invalid"
1346 +
1347 + @pytest.mark.unit
1348 + @pytest.mark.schema
1349 + def test_empty_string_default_not_coerced(self):
1350 + """Test that empty string defaults are not coerced."""
1351 + json_schema = {
1352 + "title": "TestRequest",
1353 + "type": "object",
1354 + "properties": {
1355 + "value": {
1356 + "anyOf": [{"type": "integer"}, {"type": "null"}],
1357 + "default": "", # Empty string
1358 + "description": "Value field",
1359 + },
1360 + },
1361 + }
1362 +
1363 + model_class = json_schema_to_model(json_schema)
1364 + generated_schema = model_class.model_json_schema()
1365 + prop = generated_schema["properties"]["value"]
1366 +
1367 + # Default should remain as empty string since it can't be coerced to int
1368 + assert prop.get("default") == ""
1369 +
1370 + @pytest.mark.unit
1371 + @pytest.mark.schema
1372 + def test_non_string_default_not_coerced(self):
1373 + """Test that non-string defaults (like int, list) are returned as-is."""
1374 + json_schema = {
1375 + "title": "TestRequest",
1376 + "type": "object",
1377 + "properties": {
1378 + "count": {
1379 + "type": "integer",
1380 + "default": 42, # Already an integer
1381 + "description": "Count field",
1382 + },
1383 + "items": {
1384 + "type": "array",
1385 + "default": [1, 2, 3], # Already a list
1386 + "description": "Items field",
1387 + },
1388 + },
1389 + }
1390 +
1391 + model_class = json_schema_to_model(json_schema)
1392 + generated_schema = model_class.model_json_schema()
1393 +
1394 + count_prop = generated_schema["properties"]["count"]
1395 + assert count_prop.get("default") == 42
1396 + assert isinstance(count_prop.get("default"), int)
1397 +
1398 + items_prop = generated_schema["properties"]["items"]
1399 + assert items_prop.get("default") == [1, 2, 3]
1400 + assert isinstance(items_prop.get("default"), list)
1401 +
1402 + @pytest.mark.unit
1403 + @pytest.mark.schema
1404 + def test_boolean_coercion_case_insensitive(self):
1405 + """Test that boolean coercion handles various case combinations."""
1406 + test_cases = [
1407 + ("TRUE", True),
1408 + ("True", True),
1409 + ("FALSE", False),
1410 + ("False", False),
1411 + ("YES", True),
1412 + ("Yes", True),
1413 + ("NO", False),
1414 + ("No", False),
1415 + ]
1416 +
1417 + for string_value, expected_bool in test_cases:
1418 + json_schema = {
1419 + "title": "TestRequest",
1420 + "type": "object",
1421 + "properties": {
1422 + "flag": {
1423 + "type": "boolean",
1424 + "default": string_value,
1425 + },
1426 + },
1427 + }
1428 +
1429 + model_class = json_schema_to_model(json_schema)
1430 + generated_schema = model_class.model_json_schema()
1431 + prop = generated_schema["properties"]["flag"]
1432 +
1433 + assert prop.get("default") is expected_bool, (
1434 + f"Expected '{string_value}' to coerce to {expected_bool}"
1435 + )
1436 +
1437 +
1438 if __name__ == "__main__":
1439 pytest.main([__file__, "-v"])