Skip to content

Support Enum in get_json_schema, raise on custom classes - #50583

Merged
Rocketknight1 merged 3 commits into
mainfrom
json-schema-enum-support
Nov 11, 2026
Merged

Rocketknight1 merged 3 commits into
mainfrom
json-schema-enum-support

Conversation

@Rocketknight1

@Rocketknight1 Rocketknight1 commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

🤗 Transformers CI

get_json_schema() was supposed to raise on type hints it couldn't parse, like custom classes, but _get_json_schema_type() fell back to {"type": "object"} for anything it didn't recognize, so the error was unreachable. This meant hints like Enum classes, dataclasses or datetime all silently told the model to pass a dict.

This PR adds support for Enum classes, which become an enum of their member values, and makes other unknown classes raise as originally intended. This is a behaviour change, since tools with custom class hints (including return hints) used to get through, but I think a loud failure is better than a schema that's quietly wrong!

Fixes #50556

@Rocketknight1
Rocketknight1 marked this pull request as ready for review November 10, 2026 16:06
@Rocketknight1

Copy link
Copy Markdown
Member Author

cc @qgallouedec, can you do a quick review/approval if you're happy?

@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.

Pages changed in this PR:

@qgallouedec

Copy link
Copy Markdown
Member

Thanks, lgtm.

One question on return hints (edge case, not super important I think): def f(x: int) -> Result works on 5.22 and now raises in apply_chat_template, though its arguments are fine (the model never generates the return value), so leaving return out of the schema when its hint can't be converted would keep these tools working without a wrong schema.

The [`~PreTrainedTokenizerBase.apply_chat_template`] method uses the [get_json_schema](https://github.com/huggingface/transformers/blob/14561209291255e51c55260306c7d00c159381a5/src/transformers/utils/chat_template_utils.py#L205) function to convert Python callables to a JSON schema. This includes methods: `self` and `cls` are treated as implicit receiver arguments and are ignored.

Type hints can be basic types like `int`, `float`, `str` and `bool`, `Literal` or `Enum` classes (which both become an `enum` of the allowed values), and `list`, `tuple`, `dict` and `Union` types containing those. Other classes, like dataclasses or Pydantic models, raise an error because there's no way to know what JSON they expect. If your tool needs one of those, write its JSON schema by hand and pass that instead.

@qgallouedec qgallouedec Nov 10, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
The model generates the member's value, so a tool with a `color: Color` parameter receives `"red"` rather than `Color.RED`. Call `Color(color)` to get the member back.

mangeshraut712

This comment was marked as spam.

@qgallouedec

Copy link
Copy Markdown
Member

@mangeshraut712 automated contributions aren't welcome in Hugging Face repos: each of these reviews costs a maintainer time to read and answer. So please stop commenting all hf PRs.

See the AI usage policy in our contributing guide:

More context: https://huggingface.co/blog/qgallouedec/open-source-good-intentions

@Rocketknight1

Copy link
Copy Markdown
Member Author

@qgallouedec yeah, good point! Dropping return hints rather than throwing an error when we can't parse them, which makes sense since most templates ignore the return hints

@Rocketknight1
Rocketknight1 added this pull request to the merge queue Nov 11, 2026
Merged via the queue into main with commit 715b48b Nov 11, 2026
124 checks passed
@Rocketknight1
Rocketknight1 deleted the json-schema-enum-support branch November 11, 2026 12:47
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.

get_json_schema silently returns "object" for custom classes instead of raising

4 participants