Skip to content

[Bug]: "default" argument in Field not respected for FunctionTool #17324

@lucasvw

Description

@lucasvw

Bug Description

When creating a FunctionTool and using the Pydantic Field object to specify the "default" value, the default value is not set when calling the function. Somehow the type of the argument is pydantic.fields.FieldInfo

Version

lama-index 0.12.5

Steps to Reproduce

from pydantic import Field
from llama_index.core.tools import FunctionTool
from typing import Optional


def get_weather(
    location: Optional[str] = Field(
        default="Berlin"
    ),
) -> str:
    """Usfeful for getting the weather for a given location."""
    if location == "Berlin":
        return "nice weather in Berlin"
    return "bad weather!"


tool = FunctionTool.from_defaults(get_weather)
> tool().content
'bad weather!'

> tool("Berlin").content
'nice weather in Berlin'

Relevant Logs/Tracbacks

This fixes it by the way:

from pydantic import Field
from pydantic.fields import FieldInfo
from llama_index.core.tools import FunctionTool
from typing import Optional


def get_weather(
    location: Optional[str] = Field(
        default="Berlin"
    ),
) -> str:
    """Usfeful for getting the weather for a given location."""
    if isinstance(location, FieldInfo):
        location = location.get_default()
    if location == "Berlin":
        return "nice weather in Berlin"
    return "bad weather!"


tool = FunctionTool.from_defaults(get_weather)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriageIssue needs to be triaged/prioritized

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions