{"record":{"id":"7ec86cf54693dd2e","repo":"huggingface/smolagents","slug":"tool-return-type-not-found-make-sure-your-functio","errorCode":null,"errorMessage":"Tool return type not found: make sure your function has a return type hint!","messagePattern":"Tool return type not found: make sure your function has a return type hint!","errorType":"exception","errorClass":"TypeHintParsingException","httpStatus":null,"severity":"error","filePath":"src/smolagents/tools.py","lineNumber":1076,"sourceCode":"            yield cls(tools)\n\n\ndef tool(tool_function: Callable) -> Tool:\n    \"\"\"\n    Convert a function into an instance of a dynamically created Tool subclass.\n\n    Args:\n        tool_function (`Callable`): Function to convert into a Tool subclass.\n            Should have type hints for each input and a type hint for the output.\n            Should also have a docstring including the description of the function\n            and an 'Args:' part where each argument is described.\n    \"\"\"\n    tool_json_schema = get_json_schema(tool_function)[\"function\"]\n    if \"return\" not in tool_json_schema:\n        if len(tool_json_schema[\"parameters\"][\"properties\"]) == 0:\n            tool_json_schema[\"return\"] = {\"type\": \"null\"}\n        else:\n            raise TypeHintParsingException(\n                \"Tool return type not found: make sure your function has a return type hint!\"\n            )\n\n    class SimpleTool(Tool):\n        def __init__(self):\n            self.is_initialized = True\n\n    # Set the class attributes\n    SimpleTool.name = tool_json_schema[\"name\"]\n    SimpleTool.description = tool_json_schema[\"description\"]\n    SimpleTool.inputs = tool_json_schema[\"parameters\"][\"properties\"]\n    SimpleTool.output_type = tool_json_schema[\"return\"][\"type\"]\n\n    # Set output_schema if it exists in the JSON schema\n    if \"output_schema\" in tool_json_schema:\n        SimpleTool.output_schema = tool_json_schema[\"output_schema\"]\n    elif \"return\" in tool_json_schema and \"schema\" in tool_json_schema[\"return\"]:\n        SimpleTool.output_schema = tool_json_schema[\"return\"][\"schema\"]","sourceCodeStart":1058,"sourceCodeEnd":1094,"githubUrl":"https://github.com/huggingface/smolagents/blob/30bb1161095dbae2271e6bc3cc4c219cc3897a57/src/smolagents/tools.py#L1058-L1094","documentation":"The @tool decorator builds a JSON schema for the wrapped function and requires a return annotation. If no return type hint is present and the function takes no parameters, smolagents assumes a null return; otherwise it raises TypeHintParsingException because downstream agents need the return type to interpret tool output.","triggerScenarios":"Applying `@tool` to a function that has one or more parameters but no `-> ...` return annotation, e.g. `def add(a: int, b: int):` (missing `-> int`). Zero-arg functions without hints fall back to a null type and do not raise.","commonSituations":"Quickly wrapping existing helper functions as agent tools without adding type hints; functions returning None implicitly where the author forgot `-> None`.","solutions":["Add an explicit return type hint to the function (e.g. `-> str`, `-> int`, or `-> None` for void tools)","Annotate parameter types too so input schema generation is accurate","Run a quick lint (mypy --strict or pyright) on tool modules to catch missing annotations"],"exampleFix":"# before\n@tool\ndef get_weather(city: str):\n    return fetch(city)\n# after\n@tool\ndef get_weather(city: str) -> str:\n    return fetch(city)","handlingStrategy":"type-guard","validationCode":"import inspect\nsig = inspect.signature(my_func)\nif sig.return_annotation is inspect.Signature.empty and sig.parameters:\n    raise TypeError(\"add a return type hint before wrapping with @tool\")","typeGuard":"def is_tool_ready(func) -> bool:\n    sig = inspect.signature(func)\n    return sig.return_annotation is not inspect.Signature.empty or not sig.parameters","tryCatchPattern":"from smolagents.utils import TypeHintParsingException\ntry:\n    my_tool = tool(my_func)\nexcept TypeHintParsingException:\n    my_func.__annotations__['return'] = str\n    my_tool = tool(my_func)","preventionTips":["Always annotate tool functions' parameters and return types","Run mypy/pyright over tool modules","Use `-> None` explicitly for tools returning nothing"],"tags":["tool-decorator","type-hints","schema"],"backgroundTag":"missing-type-annotation","analyzedSha":"30bb1161095dbae2271e6bc3cc4c219cc3897a57","analyzedAt":"2026-08-28T18:52:54.169Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}