Skip to content

Conversation

habema
Copy link
Contributor

@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
Contributor Author

habema commented Jul 8, 2025

Linked Issue: #992

@seratch seratch added enhancement New feature or request feature:core labels Jul 10, 2025
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

@seratch seratch requested a review from rm-openai July 10, 2025 07:41
@rm-openai rm-openai merged commit de8accc into openai:main Jul 18, 2025
5 checks passed
@salah9003
Copy link

من هنا إلى الأعلى 👍🏿 👍🏿

@3marwanmoraly3
Copy link

3marwanmoraly3 commented Jul 26, 2025

From here to the top 👍🏿 👍🏿 👍🏿

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.

5 participants