def _parse_union_type()

in src/smolagents/_function_type_hints_utils.py [0:0]


def _parse_union_type(args: tuple[Any, ...]) -> dict:
    subtypes = [_parse_type_hint(t) for t in args if t is not type(None)]
    if len(subtypes) == 1:
        # A single non-null type can be expressed directly
        return_dict = subtypes[0]
    elif all(isinstance(subtype["type"], str) for subtype in subtypes):
        # A union of basic types can be expressed as a list in the schema
        return_dict = {"type": sorted([subtype["type"] for subtype in subtypes])}
    else:
        # A union of more complex types requires "anyOf"
        return_dict = {"anyOf": subtypes}
    if type(None) in args:
        return_dict["nullable"] = True
    return return_dict