@devries-cameron20 / composio-1 / commits / bcd436609
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 parser for oneof

haxzie committed Sep 30, 2025 at 11:48 UTC bcd436609e070581250de8cb6c2683187d0d9e66
2 files changed +125 -1
python/composio/utils/shared.py
+4 -1
@@ -59,7 +59,10 @@ def json_schema_to_pydantic_type(
59 if len(pydantic_types) == 1:
60 return pydantic_types[0]
61 # Create Union with any number of types
62 - return t.Union[tuple(t.cast(t.Type, ptype) for ptype in pydantic_types)]
62 + # Cast all types and create tuple
63 + cast_types = tuple(t.cast(t.Type, ptype) for ptype in pydantic_types)
64 + # typing.Union[tuple] works in Python 3.8+ and maintains __origin__ attribute
65 + return t.Union[cast_types]
66
67 # Add fallback type - string
68 if "type" not in json_schema:
python/tests/test_schema_parser.py
+121
@@ -5,6 +5,8 @@ This module tests the core schema parsing functionality in composio.utils.shared
5 particularly focusing on the required field propagation bug that was fixed.
6 """
7
8 +import typing as t
9 +
10 import pytest
11 from pydantic import BaseModel
12 from pydantic.fields import PydanticUndefined
@@ -420,6 +422,125 @@ class TestJsonSchemaToPydanticType:
422 # Should create a Union type
423 assert hasattr(result, "__origin__")
424
425 + def test_oneof_unlimited_types(self):
426 + """Test oneOf schemas with unlimited number of types (fixes the 3-type limit bug)."""
427 + # Test 4 types (previously would fail)
428 + json_schema_4 = {
429 + "oneOf": [
430 + {"type": "string"},
431 + {"type": "integer"},
432 + {"type": "boolean"},
433 + {"type": "number"},
434 + ]
435 + }
436 + result_4 = json_schema_to_pydantic_type(json_schema_4)
437 + assert hasattr(result_4, "__origin__")
438 + assert result_4.__origin__ is t.Union
439 + assert len(result_4.__args__) == 4
440 + assert str in result_4.__args__
441 + assert int in result_4.__args__
442 + assert bool in result_4.__args__
443 + assert float in result_4.__args__
444 +
445 + # Test 5 types
446 + json_schema_5 = {
447 + "oneOf": [
448 + {"type": "string"},
449 + {"type": "integer"},
450 + {"type": "boolean"},
451 + {"type": "number"},
452 + {"type": "array"},
453 + ]
454 + }
455 + result_5 = json_schema_to_pydantic_type(json_schema_5)
456 + assert hasattr(result_5, "__origin__")
457 + assert result_5.__origin__ is t.Union
458 + assert len(result_5.__args__) == 5
459 +
460 + # Test 6 types (stress test, avoiding null which expands to Optional[Any])
461 + json_schema_6 = {
462 + "oneOf": [
463 + {"type": "string"},
464 + {"type": "integer"},
465 + {"type": "boolean"},
466 + {"type": "number"},
467 + {"type": "array"},
468 + {"type": "object"},
469 + ]
470 + }
471 + result_6 = json_schema_to_pydantic_type(json_schema_6)
472 + assert hasattr(result_6, "__origin__")
473 + assert result_6.__origin__ is t.Union
474 + assert len(result_6.__args__) == 6
475 +
476 + def test_oneof_single_type(self):
477 + """Test oneOf with single type returns the type directly."""
478 + json_schema = {"oneOf": [{"type": "string"}]}
479 + result = json_schema_to_pydantic_type(json_schema)
480 + assert result is str
481 + # Single type should not create a Union
482 + assert not hasattr(result, "__origin__")
483 +
484 + def test_oneof_with_complex_types(self):
485 + """Test oneOf with complex types like objects and arrays."""
486 + json_schema = {
487 + "oneOf": [
488 + {"type": "string"},
489 + {"type": "array", "items": {"type": "integer"}},
490 + {
491 + "type": "object",
492 + "title": "ComplexObject",
493 + "properties": {"field": {"type": "string"}},
494 + },
495 + ]
496 + }
497 + result = json_schema_to_pydantic_type(json_schema)
498 + assert hasattr(result, "__origin__")
499 + assert result.__origin__ is t.Union
500 + assert len(result.__args__) == 3
501 + # Check that we have string, List[int], and a BaseModel subclass
502 + args = result.__args__
503 + assert str in args
504 + # One should be a List type
505 + list_types = [arg for arg in args if hasattr(arg, "__origin__") and arg.__origin__ is list]
506 + assert len(list_types) == 1
507 + # One should be a BaseModel subclass
508 + model_types = [arg for arg in args if isinstance(arg, type) and issubclass(arg, BaseModel)]
509 + assert len(model_types) >= 1
510 +
511 + def test_oneof_nested_in_object(self):
512 + """Test oneOf field within an object schema."""
513 + json_schema = {
514 + "type": "object",
515 + "title": "ObjectWithOneOf",
516 + "properties": {
517 + "flexible_field": {
518 + "oneOf": [
519 + {"type": "string"},
520 + {"type": "integer"},
521 + {"type": "boolean"},
522 + {"type": "number"},
523 + ]
524 + },
525 + "normal_field": {"type": "string"},
526 + },
527 + "required": ["flexible_field"],
528 + }
529 +
530 + # Test that the model can be created
531 + model_class = json_schema_to_model(json_schema)
532 +
533 + # Test with different oneOf values
534 + instance1 = model_class(flexible_field="hello", normal_field="world")
535 + instance2 = model_class(flexible_field=42, normal_field="world")
536 + instance3 = model_class(flexible_field=True, normal_field="world")
537 + instance4 = model_class(flexible_field=3.14, normal_field="world")
538 +
539 + assert instance1.flexible_field == "hello"
540 + assert instance2.flexible_field == 42
541 + assert instance3.flexible_field is True
542 + assert instance4.flexible_field == 3.14
543 +
544 def test_fallback_to_string(self):
545 """Test that missing type defaults to string."""
546 json_schema = {} # No type specified