Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lucasvw opened this issue Dec 19, 2024 · 1 comment
Open

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

lucasvw opened this issue Dec 19, 2024 · 1 comment
Labels
bug Something isn't working triage Issue needs to be triaged/prioritized

Comments

@lucasvw
Copy link

lucasvw commented Dec 19, 2024

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)
@lucasvw lucasvw added bug Something isn't working triage Issue needs to be triaged/prioritized labels Dec 19, 2024
Copy link

dosubot bot commented Dec 19, 2024

Hey @lucasvw! I'm here to help you with any bugs, questions, or contributions. Let's get this sorted out!

The issue you're encountering with the "default" argument in the Pydantic Field not being respected when creating a FunctionTool in version 0.12.5 of LlamaIndex is due to the from_defaults method of the FunctionTool class. This method explicitly removes FieldInfo defaults from the function signature. The code iterates over the parameters of the function signature and replaces any default value that is an instance of FieldInfo with inspect.Parameter.empty, effectively removing the default value [1].

To work around this, you might consider setting default values directly in the function signature without using Pydantic's Field, or handle defaults within the function logic itself.

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Issue needs to be triaged/prioritized
Projects
None yet
Development

No branches or pull requests

1 participant