Skip to content

FunctionTool ignores strict_json_schema parameter #992

Open
@habema

Description

@habema

Describe the bug

The documentation for creating custom FunctionTool objects directly (without using the @function_tool decorator) shows an example that fails with OpenAI's Responses API due to missing additionalProperties: false in the JSON schema.

The example from the docs:

tool = FunctionTool(
    name="process_user",
    description="Processes extracted user data",
    params_json_schema=FunctionArgs.model_json_schema(),  # This line is the problem
    on_invoke_tool=run_function,
)

This results in the error:

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'}}

Debug information

  • Agents SDK version: 0.1.0
  • Python version: 3.10.16

Repro steps

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 fails with OpenAI 400 error
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]
)

# This will fail when the agent tries to use the tool
result = Runner.run_sync(agent, "Process user data for John who is 30 years old")

Working fix (not documented):

from agents.strict_schema import ensure_strict_json_schema

tool = FunctionTool(
    name="process_user", 
    description="Processes extracted user data",
    params_json_schema=ensure_strict_json_schema(FunctionArgs.model_json_schema()),  # Fix
    on_invoke_tool=run_function,
)

Expected behavior

Either:

  1. The documentation example should work as-is, OR
  2. The documentation should clearly show that ensure_strict_json_schema() must be called on Pydantic schemas when creating FunctionTool objects directly, OR
  3. The FunctionTool constructor should automatically apply strict schema validation when strict_json_schema=True (which is the default)

The current behavior is inconsistent - the @function_tool decorator automatically applies strict schema validation, but the FunctionTool class does not, despite both having strict_json_schema=True by default.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions