You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
The text was updated successfully, but these errors were encountered:
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.
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
Relevant Logs/Tracbacks
This fixes it by the way:
The text was updated successfully, but these errors were encountered: