Skip to content

Tool

FunctionTool dataclass

Declaration of a function the model can ask the application to call.

Source code in src/openai_agents/codex/tool.py
@dataclass(kw_only=True)
class FunctionTool:
    """Declaration of a function the model can ask the application to call."""

    name: str
    description: str
    parameters: dict[str, Any]
    """JSON Schema describing the function's arguments."""

    handler: Callable[[dict[str, Any]], str | Awaitable[str]] | None = None
    """Application callback that receives the decoded arguments and returns text."""

    needs_approval: bool = False
    """Ask the agent's approval handler before each invocation."""

    type: Literal["function"] = field(default="function", init=False)

parameters instance-attribute

parameters: dict[str, Any]

JSON Schema describing the function's arguments.

handler class-attribute instance-attribute

handler: (
    Callable[[dict[str, Any]], str | Awaitable[str]] | None
) = None

Application callback that receives the decoded arguments and returns text.

needs_approval class-attribute instance-attribute

needs_approval: bool = False

Ask the agent's approval handler before each invocation.

WebSearchTool dataclass

Declaration of the built-in web search tool.

Source code in src/openai_agents/codex/tool.py
@dataclass(kw_only=True)
class WebSearchTool:
    """Declaration of the built-in web search tool."""

    type: Literal["web_search"] = field(default="web_search", init=False)

function_tool

function_tool(
    func: _Function,
    *,
    name_override: str | None = None,
    description_override: str | None = None,
    needs_approval: bool = False,
) -> FunctionTool
function_tool(
    *,
    name_override: str | None = None,
    description_override: str | None = None,
    needs_approval: bool = False,
) -> Callable[[_Function], FunctionTool]
function_tool(
    func: _Function | None = None,
    *,
    name_override: str | None = None,
    description_override: str | None = None,
    needs_approval: bool = False,
) -> FunctionTool | Callable[[_Function], FunctionTool]

Turn a sync or async function into a tool using its signature and docstring.

Source code in src/openai_agents/codex/tool.py
def function_tool(
    func: _Function | None = None,
    *,
    name_override: str | None = None,
    description_override: str | None = None,
    needs_approval: bool = False,
) -> FunctionTool | Callable[[_Function], FunctionTool]:
    """Turn a sync or async function into a tool using its signature and docstring."""

    def decorate(fn: _Function) -> FunctionTool:
        name = fn.__name__ if name_override is None else name_override
        doc = docstring_parser.parse(inspect.getdoc(fn) or "")
        description = description_override
        if description is None:
            separator = "\n\n" if doc.blank_after_short_description else "\n"
            description = separator.join(
                filter(None, (doc.short_description, doc.long_description))
            )

        descriptions = {p.arg_name.lstrip("*"): p.description for p in doc.params}
        signature = inspect.signature(fn)
        adapter, kwargs_name = _tool_arguments(fn, signature, descriptions)

        async def handler(arguments: dict[str, Any]) -> str:
            bound = inspect.BoundArguments(
                signature, OrderedDict(adapter.validate_python(arguments))
            )
            # Expand **kwargs separately so Python catches duplicate keywords.
            extra = bound.arguments.pop(kwargs_name) if kwargs_name else {}
            if inspect.iscoroutinefunction(fn):
                result = fn(*bound.args, **bound.kwargs, **extra)
            else:
                result = await run_sync(lambda: fn(*bound.args, **bound.kwargs, **extra))
            if inspect.isawaitable(result):
                result = await result
            if isinstance(result, str):
                return result
            return _result_adapter.dump_json(result).decode()

        return FunctionTool(
            name=name,
            description=description,
            parameters=adapter.json_schema(),
            handler=handler,
            needs_approval=needs_approval,
        )

    return decorate if func is None else decorate(func)