-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
[Question]: Pydantic models in function tool arguments #17427
Comments
LlamaIndex does not automatically reify lists of dictionaries to lists of Pydantic model instances. When using Pydantic models in function tool arguments, you need to handle the conversion manually. The framework provides methods like To achieve the desired behavior, you can manually convert the list of dictionaries to a list of from pydantic import BaseModel, Field
class AClass(BaseModel):
value: str = Field(..., description="A value")
def my_function_tool(
list_of_something: list[str] = Field(
description="A list of something"
),
list_of_classes: list[dict] = Field(
description="A list of classes"
)
):
# Convert list of dicts to list of AClass instances
list_of_classes = [AClass.parse_obj(item) for item in list_of_classes]
# Now you can work with list_of_classes as a list of AClass instances This approach ensures that each dictionary in To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other |
@dosu in this case how do I define the properties that map to openapi? |
To define the properties that map to OpenAPI when using Pydantic models with function tool arguments, you can use the Here's a code snippet illustrating this: def get_parameters_dict(self) -> dict:
if self.fn_schema is None:
parameters = {
"type": "object",
"properties": {
"input": {"title": "input query string", "type": "string"},
},
"required": ["input"],
}
else:
parameters = self.fn_schema.model_json_schema()
parameters = {
k: v
for k, v in parameters.items()
if k in ["type", "properties", "required", "definitions", "$defs"]
}
return parameters This method ensures that the properties are correctly mapped to the OpenAPI format by extracting the necessary fields from the Pydantic model's JSON schema. It also supports nested Pydantic models, ensuring that nested structures are correctly represented in the OpenAPI schema using To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other |
@logan-markewich I don't understand, isn't LlamaIndex supposed to reify the function args? |
@ziodave llms can only write string, dict, list, int, and float types. It passes that into the function. Whatever happens after that is up to the function to figure out. In this case, I would just check for dict and convert it There's maybe a way to do type introspection to get the class object and cast the input for you, maybe a PR worth looking into |
Thanks @logan-markewich In the meanwhile this is what I am doing, if it helps others: def _reify_argument(my_list: list[Union[dict | MyClass]]) -> list[MyClass]:
return [MyClass(**m) if isinstance(m, dict) else m for m in my_list]
def function_tool(my_list: list[MyClass] = Field(description="A list of My Classes")):
my_list = _reify_argument(my_list) |
Question Validation
Question
I have the following function:
I connect it to the agent:
However when the function is called, it passing a list[dict] to
list_of_classes
, instead oflist[AClass]
.Shouldn't LlamaIndex reify the
dict
s toAClass
instances?The text was updated successfully, but these errors were encountered: