Skip to content

Commit 35a21e8

Browse files
haasonsaasseratch
andauthored
docs: describe Pydantic Field annotations for tool args (#2436)
Co-authored-by: Kazuhiro Sera <seratch@openai.com>
1 parent 877a7c3 commit 35a21e8

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

docs/tools.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,26 @@ As mentioned before, we automatically parse the function signature to extract th
278278

279279
The code for the schema extraction lives in [`agents.function_schema`][].
280280

281+
### Constraining and describing arguments with Pydantic Field
282+
283+
You can use Pydantic's [`Field`](https://docs.pydantic.dev/latest/concepts/fields/) to add constraints (e.g. min/max for numbers, length or pattern for strings) and descriptions to tool arguments. As in Pydantic, both forms are supported: default-based (`arg: int = Field(..., ge=1)`) and `Annotated` (`arg: Annotated[int, Field(..., ge=1)]`). The generated JSON schema and validation include these constraints.
284+
285+
```python
286+
from typing import Annotated
287+
from pydantic import Field
288+
from agents import function_tool
289+
290+
# Default-based form
291+
@function_tool
292+
def score_a(score: int = Field(..., ge=0, le=100, description="Score from 0 to 100")) -> str:
293+
return f"Score recorded: {score}"
294+
295+
# Annotated form
296+
@function_tool
297+
def score_b(score: Annotated[int, Field(..., ge=0, le=100, description="Score from 0 to 100")]) -> str:
298+
return f"Score recorded: {score}"
299+
```
300+
281301
## Agents as tools
282302

283303
In some workflows, you may want a central agent to orchestrate a network of specialized agents, instead of handing off control. You can do this by modeling agents as tools.

0 commit comments

Comments
 (0)