Skip to content

Commit de8accc

Browse files
authored
fix: Apply strict JSON schema validation in FunctionTool constructor (#1041)
## Summary Fixes an issue where directly created `FunctionTool` objects fail with OpenAI's Responses API due to missing `additionalProperties: false` in the JSON schema, while the `@function_tool` decorator works correctly. ## Problem The documentation example for creating `FunctionTool` objects directly fails with: ``` Error code: 400 - {'error': {'message': "Invalid schema for function 'process_user': In context=(), 'additionalProperties' is required to be supplied and to be false.", 'type': 'invalid_request_error', 'param': 'tools[0].parameters', 'code': 'invalid_function_parameters'}} ``` This creates an inconsistency between `FunctionTool` and `@function_tool` behavior, both of which have `strict_json_schema=True` by default. ## Solution - Added `__post_init__` method to `FunctionTool` dataclass - Automatically applies `ensure_strict_json_schema()` when `strict_json_schema=True` - Makes behavior consistent with `@function_tool` decorator - Maintains backward compatibility ## Testing The fix can be verified by running the reproduction case from the issue: ```python from typing import Any from pydantic import BaseModel from agents import RunContextWrapper, FunctionTool, Agent, Runner class FunctionArgs(BaseModel): username: str age: int async def run_function(ctx: RunContextWrapper[Any], args: str) -> str: parsed = FunctionArgs.model_validate_json(args) return f"{parsed.username} is {parsed.age} years old" # This now works without manual ensure_strict_json_schema() call tool = FunctionTool( name="process_user", description="Processes extracted user data", params_json_schema=FunctionArgs.model_json_schema(), on_invoke_tool=run_function, ) agent = Agent( name="Test Agent", instructions="You are a test agent", tools=[tool] ) result = Runner.run_sync(agent, "Process user data for John who is 30 years old") ```
1 parent 4a31bb6 commit de8accc

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/agents/tool.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from .items import RunItem
2525
from .logger import logger
2626
from .run_context import RunContextWrapper
27+
from .strict_schema import ensure_strict_json_schema
2728
from .tool_context import ToolContext
2829
from .tracing import SpanError
2930
from .util import _error_tracing
@@ -92,6 +93,10 @@ class FunctionTool:
9293
and returns whether the tool is enabled. You can use this to dynamically enable/disable a tool
9394
based on your context/state."""
9495

96+
def __post_init__(self):
97+
if self.strict_json_schema:
98+
self.params_json_schema = ensure_strict_json_schema(self.params_json_schema)
99+
95100

96101
@dataclass
97102
class FileSearchTool:

0 commit comments

Comments
 (0)