Skip to content

feat(function_schema): Add support for pydantic Field annotations in tool arguments (for tools decorated with @function_schema) #1124

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

Merged
merged 3 commits into from
Jul 15, 2025

Conversation

georg-wolflein
Copy link
Contributor

Summary

This PR allows you to use the pydantic Field decorator to constrain tool arguments for tools decorated with @function_tool (more specifically, for tools using function_schema to parse tool arguments). Such constrains include, e.g. limiting integers to a certain range, but in principle, this should work for any constrains supported by the API.

Specifically, it enables the following syntax:

@function_tool
def my_tool(age: int = Field(..., gt=0)) -> str:
    ...

Previously, one had to create a nested pydantic BaseModel to achieve this functionality.
Issue #1123 explains this feature request and the previous workaround.

Example:

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: (compare to #1123)

{
  "properties": {
    "age": {
      "exclusiveMinimum": 0,
      "title": "Age",
      "type": "integer"
    }
  },
  "required": [
    "age"
  ],
  "title": "my_tool_args",
  "type": "object",
  "additionalProperties": false
}

Test plan

I added unit tests in tests/test_function_schema.py.

Issue number

Closes #1123.

Checks

  • I've added new tests (if relevant)
  • I've added/updated the relevant documentation
  • I've run make lint and make format
  • I've made sure tests pass

Note: I am happy to add documentation for this; please point me to where I should do so:)

@seratch seratch added enhancement New feature or request feature:core labels Jul 15, 2025
@georg-wolflein georg-wolflein changed the title Add support for pydantic Field annotations in tool arguments (for tools decorated with @function_schema) feat(function_schema): Add support for pydantic Field annotations in tool arguments (for tools decorated with @function_schema) Jul 15, 2025
@seratch
Copy link
Member

seratch commented Jul 15, 2025

Could you check the lint error?

uv run ruff check
tests/test_function_schema.py:1:1: I001 [*] Import block is un-sorted or un-formatted
   |
 1 | / from collections.abc import Mapping
 2 | | from enum import Enum
 3 | | from typing import Any, Literal
 4 | |
 5 | | import pytest
 6 | | from pydantic import BaseModel, ValidationError, Field
 7 | | from typing_extensions import TypedDict
 8 | |
 9 | | from agents import RunContextWrapper
10 | | from agents.exceptions import UserError
11 | | from agents.function_schema import function_schema
12 | |
13 | |
   | |__^ I001
14 |   def no_args_function():
15 |       """This function has no args."""
   |
   = help: Organize imports

Found 1 error.
[*] 1 fixable with the `--fix` option.
make: *** [Makefile:16: lint] Error 1
Error: Process completed with exit code 2.

@georg-wolflein
Copy link
Contributor Author

georg-wolflein commented Jul 15, 2025

Could you check the lint error?

Done:) @seratch

@seratch seratch requested review from seratch and rm-openai and removed request for seratch July 15, 2025 10:44
Copy link
Member

@seratch seratch left a comment

Choose a reason for hiding this comment

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

LGTM; @rm-openai can you take a look at this?

Copy link
Collaborator

@rm-openai rm-openai left a comment

Choose a reason for hiding this comment

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

This is so good, thanks! @georg-wolflein in a follow up would also love some docs for this

@rm-openai rm-openai merged commit 14154b7 into openai:main Jul 15, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature:core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support pydantic Field annotations in tool arguments
3 participants