Skip to content

[BUG] - Cannot use tool decorator on functions that use regular typing features such as Literal or Union #1277

@VianneyMI

Description

@VianneyMI

Describe the bug
The tool decorator fails when using regular typing features such as Literal or Union.

Code to reproduce the error

"""Test the smolagents package."""

from typing import Literal

import pytest
from smolagents import tool


ReturnType = Literal["str", "bool"]


def dummy_function_to_be_tooled(return_type: ReturnType) -> str | bool:
    """Dummy function to be tooled.

    Args:
        return_type: ReturnType
            The type of the return value indicated as a string.

    Returns:
        The return value.
    """

    if return_type == "str":
        return "Hello, world!"
    else:
        return True


def test_tool_decorator_on_function_that_returns_union_type():
    """Test the tool decorator on a function that returns a union type."""

    with pytest.raises(TypeError):
        tool(dummy_function_to_be_tooled)

==> I cannot use Literal while typing my functions.

"""Test the smolagents package."""

from typing import Literal

import pytest
from smolagents import tool


ReturnType = Literal["str", "bool"]


def dummy_function_to_be_tooled(return_type: str) -> str | bool:
    """Dummy function to be tooled.

    Args:
        return_type: ReturnType
            The type of the return value indicated as a string.

    Returns:
        The return value.
    """

    if return_type == "str":
        return "Hello, world!"
    else:
        return True


def test_tool_decorator_on_function_that_returns_union_type():
    """Test the tool decorator on a function that returns a union type."""

    with pytest.raises(TypeError):
        tool(dummy_function_to_be_tooled)

This fails with a TypeError and the following error message:

Attribute output_type should have type str, got <class 'list'> instead.

==> I cannot use Union when typing my functions

Expected behavior

I want to be able to use those common typing feature in the functions that I want to toolify.

Packages version:
smolagents==1.9.2

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions