Skip to content

fix: Apply strict JSON schema validation in FunctionTool constructor #1041

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

habema
Copy link

@habema habema commented Jul 8, 2025

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:

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")

@habema
Copy link
Author

habema commented Jul 8, 2025

Linked Issue: #992

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant