Support function_tool on instance methods#3458
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 04770327d8
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| def _is_instance_method_tool(the_func: ToolFunction[...]) -> bool: | ||
| parameters = tuple(inspect.signature(the_func).parameters.values()) | ||
| return bool(parameters) and parameters[0].name == "self" |
There was a problem hiding this comment.
Detect methods without relying on
self
For decorated instance methods whose first parameter uses any valid name other than the convention self (for example def lookup(this, account_id: str)), this check does not install the descriptor factory, so tools.lookup returns the unbound FunctionTool, leaves the instance parameter in the JSON schema, and later invokes the original function without the instance. Conversely, a non-method tool whose first argument happens to be named self is now misclassified and becomes unusable unless accessed through an instance.
Useful? React with 👍 / 👎.
Summary
@function_toolon instance methods by binding the decorated tool when accessed from an instance.selfparameter from generated tool schemas while preserving normal function-tool behavior.self, and document the usage pattern.Test plan
.venv/bin/python -m pytest tests/test_function_tool.py.venv/bin/ruff format --check src/agents/function_schema.py src/agents/tool.py tests/test_function_tool.py.venv/bin/ruff check src/agents/function_schema.py src/agents/tool.py tests/test_function_tool.py.venv/bin/pyright src/agents/function_schema.py src/agents/tool.py tests/test_function_tool.pygit diff --checkIssue number
Closes #94
Checks
make lintandmake format(Ruff format/check commands above passed locally)