-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Closed
Labels
enhancementNew feature or requestNew feature or request
Description
Currently, the @function_tool
decorator does not support the pydantic Field
annotation to constrain the values of the function arguments, e.g. to be an integer within a certain range.
Example
Consider the example below:
import json
from pydantic import Field
from agents import function_tool
@function_tool
def my_tool(age: int = Field(..., gt=0)) -> str:
return f"The age is {age}"
print(json.dumps(my_tool.params_json_schema, indent=2))
Output:
{
"properties": {
"age": {
"title": "Age",
"type": "integer"
}
},
"title": "my_tool_args",
"type": "object",
"additionalProperties": false,
"required": [
"age"
]
}
The constraint that the age should be greater than 0 is not part of the JSON schema.
Workaround
It is currently possible to get such constraints in nested BaseModel
s. However, I don't think that putting the actual function arguments in a nested BaseModel
is an ideal solution.
import json
from pydantic import BaseModel, Field
from agents import function_tool
class MyToolInput(BaseModel):
age: int = Field(..., gt=0)
@function_tool
def my_tool(input: MyToolInput) -> str:
return f"The age is {input.age}"
print(json.dumps(my_tool.params_json_schema, indent=2))
Output:
{
"$defs": {
"MyToolInput": {
"properties": {
"age": {
"exclusiveMinimum": 0,
"title": "Age",
"type": "integer"
}
},
"required": [
"age"
],
"title": "MyToolInput",
"type": "object",
"additionalProperties": false
}
},
"properties": {
"input": {
"$ref": "#/$defs/MyToolInput"
}
},
"required": [
"input"
],
"title": "my_tool_args",
"type": "object",
"additionalProperties": false
}
In the workaround, the age
has an exclusiveMinimum
. My feature request is that the exclusiveMinimum
would also be present in the original example above.
I will submit a PR to implement this functionality shortly.
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request