Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix(py): fix SALESFORCE_CREATE_LEAD type error by implementing anyOf/allOf
jkomynoaa216f8ea6a9
diff --git a/python/composio/utils/shared.py b/python/composio/utils/shared.pyindex e7e9b4591..57e1c772b 100644--- a/python/composio/utils/shared.py+++ b/python/composio/utils/shared.py@@ -69,6 +69,55 @@ def json_schema_to_pydantic_type(# Use reduce to create Union[Type1, Type2, ...] properlyreturn reduce(lambda a, b: t.Union[a, b], cast_types) # type: ignore+ # Handle anyOf schemas (commonly used for nullable types)+ if "anyOf" in json_schema:+ any_of_options = json_schema["anyOf"]+ pydantic_types = [+ json_schema_to_pydantic_type(option) for option in any_of_options+ ]+ # Filter out None/null types for the base type+ # "null" type maps to t.Optional[t.Any], so we need to check for that too+ null_type = PYDANTIC_TYPE_TO_PYTHON_TYPE.get("null")+ has_null = any(+ ptype == null_type or ptype is type(None)+ for ptype in pydantic_types+ if ptype is not None+ )+ valid_types = [+ ptype+ for ptype in pydantic_types+ if ptype is not None and ptype is not type(None) and ptype != null_type+ ]+ if len(valid_types) == 0:+ return str # fallback to string type+ if len(valid_types) == 1:+ base_type = valid_types[0]+ # If null was one of the options, make it Optional+ if has_null:+ return t.Optional[t.cast(t.Type, base_type)] # type: ignore+ return base_type+ cast_types = [t.cast(t.Type, ptype) for ptype in valid_types]+ union_type = reduce(lambda a, b: t.Union[a, b], cast_types) # type: ignore+ # If null was one of the options, include None in the Union+ if has_null:+ return t.Optional[union_type] # type: ignore+ return union_type++ # Handle allOf schemas (usually for combining schemas)+ if "allOf" in json_schema:+ all_of_options = json_schema["allOf"]+ # For allOf with single option, use that option's type+ if len(all_of_options) == 1:+ return json_schema_to_pydantic_type(all_of_options[0])+ # For multiple allOf options, try to find the primary type+ # Usually the first non-ref schema or the one with "type" key+ for option in all_of_options:+ if "type" in option:+ return json_schema_to_pydantic_type(option)+ # Fallback: process first option+ if all_of_options:+ return json_schema_to_pydantic_type(all_of_options[0])+# Add fallback type - stringif "type" not in json_schema:json_schema["type"] = "string"diff --git a/python/tests/test_schema_parser.py b/python/tests/test_schema_parser.pyindex 98de25234..d52b6f7af 100644--- a/python/tests/test_schema_parser.py+++ b/python/tests/test_schema_parser.py@@ -559,6 +559,89 @@ class TestJsonSchemaToPydanticType:with pytest.raises(ValueError, match="Unsupported JSON schema type"):json_schema_to_pydantic_type(json_schema)+ def test_anyof_nullable_object(self):+ """Test anyOf with object and null types (common for nullable fields)."""+ json_schema = {+ "anyOf": [{"type": "object", "additionalProperties": {}}, {"type": "null"}]+ }+ result = json_schema_to_pydantic_type(json_schema)+ # Should return Optional[Dict], not str (to allow both dict and None)+ assert hasattr(result, "__origin__")+ assert result.__origin__ is t.Union+ assert t.Dict in result.__args__+ assert type(None) in result.__args__++ def test_anyof_nullable_object_with_properties(self):+ """Test anyOf with object (with properties) and null types."""+ json_schema = {+ "anyOf": [+ {+ "type": "object",+ "title": "CustomFields",+ "properties": {"field1": {"type": "string"}},+ },+ {"type": "null"},+ ]+ }+ result = json_schema_to_pydantic_type(json_schema)+ # Should return Optional[BaseModel subclass] (Union with None)+ assert hasattr(result, "__origin__")+ assert result.__origin__ is t.Union+ # One of the args should be a BaseModel subclass+ model_types = [+ arg+ for arg in result.__args__+ if isinstance(arg, type) and issubclass(arg, BaseModel)+ ]+ assert len(model_types) == 1+ # None should also be in the union+ assert type(None) in result.__args__++ def test_anyof_multiple_types(self):+ """Test anyOf with multiple non-null types."""+ json_schema = {+ "anyOf": [{"type": "string"}, {"type": "integer"}, {"type": "object"}]+ }+ result = json_schema_to_pydantic_type(json_schema)+ assert hasattr(result, "__origin__")+ assert result.__origin__ is t.Union++ def test_anyof_single_type(self):+ """Test anyOf with single type returns the type directly."""+ json_schema = {"anyOf": [{"type": "string"}]}+ result = json_schema_to_pydantic_type(json_schema)+ assert result is str+ assert not hasattr(result, "__origin__")++ def test_allof_single_option(self):+ """Test allOf with single option."""+ json_schema = {+ "allOf": [+ {+ "type": "object",+ "properties": {"name": {"type": "string"}},+ "title": "Test",+ }+ ]+ }+ result = json_schema_to_pydantic_type(json_schema)+ assert isinstance(result, type)+ assert issubclass(result, BaseModel)++ def test_allof_multiple_options_with_type(self):+ """Test allOf with multiple options where one has type."""+ json_schema = {+ "allOf": [{"description": "Some description"}, {"type": "string"}]+ }+ result = json_schema_to_pydantic_type(json_schema)+ assert result is str++ def test_allof_empty_options(self):+ """Test allOf with empty options falls back to string."""+ json_schema = {"allOf": []}+ result = json_schema_to_pydantic_type(json_schema)+ assert result is str+class TestJsonSchemaToFieldsDict:"""Test cases for json_schema_to_fields_dict function."""@@ -755,5 +838,131 @@ class TestEdgeCases:pass+class TestCrewAICustomFieldsBug:+ """Regression tests for PLEN-1177 - CustomFields type error with CrewAI."""++ @pytest.mark.unit+ @pytest.mark.schema+ def test_custom_fields_object_type_preserved(self):+ """+ Test that CustomFields with anyOf [object, null] returns Dict, not str.++ This is the exact bug scenario from PLEN-1177.+ """+ # Schema similar to what Salesforce tools return for CustomFields+ json_schema = {+ "title": "CreateLeadRequest",+ "type": "object",+ "properties": {+ "Company": {"type": "string", "title": "Company"},+ "LastName": {"type": "string", "title": "LastName"},+ "CustomFields": {+ "anyOf": [+ {"type": "object", "additionalProperties": {}},+ {"type": "null"},+ ],+ "default": None,+ "description": "Dictionary of custom field API names and their values.",+ "title": "CustomFields",+ },+ },+ "required": ["Company", "LastName"],+ }++ model_class = json_schema_to_model(json_schema)++ # The model should accept a dict for CustomFields+ instance = model_class(+ Company="Test Corp",+ LastName="Smith",+ CustomFields={"Custom_Field__c": "Value"},+ )+ assert instance.CustomFields == {"Custom_Field__c": "Value"}++ # The model should also accept None+ instance_none = model_class(+ Company="Test Corp",+ LastName="Smith",+ CustomFields=None,+ )+ assert instance_none.CustomFields is None++ @pytest.mark.unit+ @pytest.mark.schema+ def test_pydantic_model_json_schema_preserves_object_type(self):+ """+ Test that when Pydantic model is converted back to JSON schema,+ the object type is preserved (not converted to string).+ """+ json_schema = {+ "title": "TestModel",+ "type": "object",+ "properties": {+ "custom_dict": {+ "anyOf": [{"type": "object"}, {"type": "null"}],+ "default": None,+ }+ },+ }++ model_class = json_schema_to_model(json_schema)+ generated_schema = model_class.model_json_schema()++ # The generated schema should have object type, not string+ custom_dict_schema = generated_schema["properties"]["custom_dict"]++ # It might be wrapped in anyOf or have direct type+ if "anyOf" in custom_dict_schema:+ types = [opt.get("type") for opt in custom_dict_schema["anyOf"]]+ assert "object" in types+ assert "string" not in types+ else:+ assert (+ custom_dict_schema.get("type") == "object"+ or custom_dict_schema.get("additionalProperties") is not None+ )++ @pytest.mark.unit+ @pytest.mark.schema+ def test_anyof_with_nested_custom_fields(self):+ """+ Test anyOf handling with more complex nested CustomFields scenario.+ """+ json_schema = {+ "title": "SalesforceRequest",+ "type": "object",+ "properties": {+ "leadData": {+ "type": "object",+ "title": "LeadData",+ "properties": {+ "Name": {"type": "string", "title": "Name"},+ "CustomFields": {+ "anyOf": [+ {"type": "object", "additionalProperties": {}},+ {"type": "null"},+ ],+ "default": None,+ },+ },+ "required": ["Name"],+ }+ },+ "required": ["leadData"],+ }++ model_class = json_schema_to_model(json_schema)++ # Test with CustomFields as dict+ instance = model_class(+ leadData={"Name": "John", "CustomFields": {"Industry__c": "Tech"}}+ )+ assert instance.leadData.CustomFields == {"Industry__c": "Tech"}++ # Test with CustomFields as None+ instance_none = model_class(leadData={"Name": "John", "CustomFields": None})+ assert instance_none.leadData.CustomFields is None++if __name__ == "__main__":pytest.main([__file__, "-v"])