Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fixes Cannot take a Union of no types.
Fixes #71
  • Loading branch information
grll committed Sep 17, 2025
commit c1e11e846c1caf5150f2e5ada946b51fd8cf9159
15 changes: 11 additions & 4 deletions src/mcpadapt/utils/modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ def get_field_type(field_name: str, field_schema: Dict[str, Any], required: set)

types.append(created_models[ref_name])

field_type = types[0] if len(types) == 1 else Union[tuple(types)] # type: ignore
if len(types) == 0:
field_type = Any
elif len(types) == 1:
field_type = types[0]
else:
field_type = Union[tuple(types)] # type: ignore
default = field_schema.get("default")
is_required = field_name in required and default is None

Expand All @@ -158,10 +163,12 @@ def get_field_type(field_name: str, field_schema: Dict[str, Any], required: set)
mapped_type = json_type_mapping.get(t, Any)
types.append(mapped_type)

if len(types) > 1:
field_type = Union[tuple(types)] # type: ignore
if len(types) == 0:
field_type = Any
elif len(types) == 1:
field_type = types[0]
else:
field_type = types[0] if types else Any
field_type = Union[tuple(types)] # type: ignore
else:
# Original code for simple types
field_type = json_type_mapping.get(json_type, Any) # type: ignore
Expand Down
7 changes: 3 additions & 4 deletions tests/test_crewai_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,9 @@ async def arun():


def test_empty_union_type_error(empty_union_server_script):
"""Test that empty unions (anyOf with only null types) raise TypeError.
"""Test that empty unions (anyOf with only null types) do not raise TypeError.

This test reproduces issue #71 where schemas with anyOf containing only
This test reproduced issue #71 where schemas with anyOf containing only
null types cause 'Cannot take a Union of no types' error.
"""
with MCPAdapt(
Expand All @@ -494,5 +494,4 @@ def test_empty_union_type_error(empty_union_server_script):
),
CrewAIAdapter(),
) as tools:
# Should raise TypeError before reaching here
pass
assert len(tools) == 1