Skip to content

Support function_tool on instance methods#3458

Draft
adit24dhaya wants to merge 1 commit into
openai:mainfrom
adit24dhaya:fix-function-tool-method-binding
Draft

Support function_tool on instance methods#3458
adit24dhaya wants to merge 1 commit into
openai:mainfrom
adit24dhaya:fix-function-tool-method-binding

Conversation

@adit24dhaya
Copy link
Copy Markdown

@adit24dhaya adit24dhaya commented May 19, 2026

Summary

  • Support using @function_tool on instance methods by binding the decorated tool when accessed from an instance.
  • Omit the implicit self parameter from generated tool schemas while preserving normal function-tool behavior.
  • Add regression coverage for instance-method tools, including context after 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.py
  • git diff --check

Issue number

Closes #94

Checks

  • I've added new tests (if relevant)
  • I've added/updated the relevant documentation
  • I've run make lint and make format (Ruff format/check commands above passed locally)
  • I've made sure tests pass

@adit24dhaya adit24dhaya marked this pull request as ready for review May 19, 2026 09:07
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/agents/tool.py
Comment on lines +1842 to +1844
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"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@seratch seratch marked this pull request as draft May 19, 2026 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add method_tool Functionality

2 participants