Skip to content

convert_function_to_ollama_tool breaks under from __future__ import annotations (PEP 563) #1507

Closed

Description

@planetf1

Description

convert_function_to_ollama_tool() in mellea/backends/tools.py builds a dynamic Pydantic model from a tool function's parameter annotations:

schema = type(
    func.__name__,
    (BaseModel,),
    {
        "__annotations__": {
            k: v.annotation if v.annotation != inspect._empty else str
            for k, v in inspect.signature(func).parameters.items()
        },
        "__signature__": inspect.signature(func),
        "__doc__": parsed_docstring[doc_string_hash],
    },
).model_json_schema()  # type: ignore

Under from __future__ import annotations (PEP 563), inspect.signature(func).parameters[...].annotation is a string (e.g. "str", "Requirement"), not the real type object. Pydantic treats these as unresolved forward references.

Builtin-type parameters (str, int, etc.) happen to resolve because Pydantic can look them up by name in builtins. Custom-class parameters fail hard:

from __future__ import annotations
from dataclasses import dataclass
from mellea.backends.tools import convert_function_to_ollama_tool

@dataclass
class Requirement:
    id: str

def my_tool(req: Requirement) -> str:
    """A tool with a custom-class param."""
    return req.id

convert_function_to_ollama_tool(my_tool)

raises:

PydanticUserError: 'my_tool' is not fully defined; you should define 'Requirement', then call 'my_tool.model_rebuild()'.

Any module that defines an Ollama tool function under from __future__ import annotations and uses a non-builtin parameter type will hit this at tool-conversion time.

Root cause

Same root cause as #1503: raw inspect.signature(func) annotations are read without resolving PEP 563 postponed (string) annotations — e.g. via inspect.signature(func, eval_str=True) or typing.get_type_hints(func).

Activity

  1. self-assigned this
    on Aug 6, 2026
  2. added
    bug
    area/tools
    p1
    on Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/toolsbugp1

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions