Skip to content

Enhance JSON schema generation to support instance, static, and class methods#43968

Merged
qgallouedec merged 5 commits into
mainfrom
json-schema-support-methods
Feb 13, 2026
Merged

Enhance JSON schema generation to support instance, static, and class methods#43968
qgallouedec merged 5 commits into
mainfrom
json-schema-support-methods

Conversation

@qgallouedec

@qgallouedec qgallouedec commented Feb 13, 2026

Copy link
Copy Markdown
Member

What does this PR do?

Enhances get_json_schema() and render_jinja_template() to support instance methods, class methods, and static methods, not just plain functions.

Previously, get_json_schema() only worked with standalone functions. Passing a method (bound or unbound) would either fail or incorrectly include self/cls as a required parameter in the generated schema. Similarly, render_jinja_template() would reject bound methods with a ValueError because inspect.isfunction() returns False for them.

This is needed for stateful tool use, where tools are naturally defined as methods on a class instance. For example, an agent may need tools that share state (e.g., a database connection, a session, or accumulated context), and the idiomatic way to do this in Python is to group them as methods on an object:

class IncrementEnv:
    def __init__(self):
        self._counter = 0

    def increment(self, a: int) -> int:
        """
        Increment the internal counter by the input value.

        Args:
            a: The amount to increment the counter by

        Returns:
            The new value of the counter after incrementing
        """
        self._counter += a
        return self._counter

my_env = IncrementEnv()
schema = get_json_schema(my_env.increment)  # Now works correctly
{'type': 'function', 'function': {'name': 'increment', 'description': 'Increment the internal counter by the input value.', 'parameters': {'type': 'object', 'properties': {'a': {'type': 'integer', 'description': 'The amount to increment the counter by'}}, 'required': ['a']}, 'return': {'type': 'integer', 'description': 'The new value of the counter after incrementing'}}}

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Comment thread src/transformers/utils/chat_template_utils.py Outdated
@qgallouedec qgallouedec merged commit 153894c into main Feb 13, 2026
26 checks passed
@qgallouedec qgallouedec deleted the json-schema-support-methods branch February 13, 2026 18:01
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.

3 participants