Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,26 @@ As mentioned before, we automatically parse the function signature to extract th

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

### Constraining and describing arguments with Pydantic Field

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.

```python
from typing import Annotated
from pydantic import Field
from agents import function_tool

# Default-based form
@function_tool
def score_a(score: int = Field(..., ge=0, le=100, description="Score from 0 to 100")) -> str:
return f"Score recorded: {score}"

# Annotated form
@function_tool
def score_b(score: Annotated[int, Field(..., ge=0, le=100, description="Score from 0 to 100")]) -> str:
return f"Score recorded: {score}"
Comment on lines +295 to +298

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remove claim that Annotated Field constraints are applied

The docs state that the Annotated form (Annotated[int, Field(..., ge=1)]) is supported and that constraints are included in the generated schema, but function_schema currently strips Annotated metadata via _strip_annotated() and only keeps string metadata for descriptions. This means Field(...) inside Annotated is discarded, so constraints like ge/le are not applied to tool args. Unless the runtime is updated to preserve Field metadata from Annotated, this example is misleading and can cause users to believe constraints will be enforced when they will not.

Useful? React with 👍 / 👎.

```

## Agents as tools

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.
Expand Down