Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 25 additions & 0 deletions qtype/application/converters/tools_from_module.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import importlib
import inspect
import types
from typing import Any, Type, Union, get_args, get_origin

from pydantic import BaseModel
Expand Down Expand Up @@ -268,6 +269,30 @@ def _map_python_type_to_variable_type(

# Check for generic types like list[str], list[int], etc.
origin = get_origin(python_type)

# Handle Union types (including Optional which is Union[T, None])
# In Python 3.10+, Type | None creates a types.UnionType
is_union = origin is Union or isinstance(python_type, types.UnionType)

if is_union:
args = get_args(python_type)
# Filter out None to find the actual type
non_none_types = [t for t in args if t is not type(None)]

if len(non_none_types) == 1:
# This is an Optional type (Union[T, None] or T | None)
# Recursively map the non-None type
return _map_python_type_to_variable_type(
non_none_types[0],
custom_type_registry,
custom_type_models,
)
else:
# Multiple non-None types in union - not currently supported
raise ValueError(
f"Union types with multiple non-None types are not supported: {python_type}"
)

if origin is list:
# Handle list[T] annotations
args = get_args(python_type)
Expand Down
55 changes: 55 additions & 0 deletions tests/application/test_tools_from_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
tools_from_module,
)
from qtype.base.types import PrimitiveTypeEnum
from qtype.dsl.domain_types import SearchResult
from qtype.dsl.model import PythonFunctionTool


Expand Down Expand Up @@ -346,3 +347,57 @@ def test_map_python_type_to_type_str(python_type, expected):
"""Test type to string mapping."""
result = _map_python_type_to_type_str(python_type, {}, {})
assert result == expected


def test_tools_from_module_with_optional_domain_type(temp_module):
"""Test function with optional domain type parameter (Union[Type, None])."""
module_name = temp_module(
"""
from qtype.dsl.domain_types import SearchResult

def process_result(result: SearchResult | None = None) -> str:
'''Process a search result.'''
if result is None:
return "No result"
return str(result.doc_id)
""",
"optional_domain_module",
)

tools, custom_types = tools_from_module(module_name)

assert len(tools) == 1
tool = tools[0]
assert tool.name == "process_result"
assert tool.inputs is not None
assert len(tool.inputs) == 1

# Check that the parameter is properly typed as optional SearchResult
result_param = next((v for v in tool.inputs if v.id == "result"), None)
assert result_param is not None
assert result_param.type == SearchResult
assert result_param.optional


def test_map_python_type_to_variable_type_union_with_none():
"""Test mapping Union type with None to optional variable type."""
from typing import Union

# Test with domain type
result = _map_python_type_to_variable_type(
Union[SearchResult, None], {}, {}
)
assert result == "SearchResult"

# Test with primitive type
result = _map_python_type_to_variable_type(Union[str, None], {}, {})
assert result == PrimitiveTypeEnum.text

# Test with Pydantic model
custom_type_registry = {}
custom_type_models = {}
result = _map_python_type_to_variable_type(
Union[SampleModel, None], custom_type_registry, custom_type_models
)
assert result == "SampleModel"
assert "SampleModel" in custom_type_registry