Repository navigation
Support Enum in get_json_schema, raise on custom classes - #50583
Conversation
|
cc @qgallouedec, can you do a quick review/approval if you're happy? |
|
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: |
|
Thanks, lgtm. One question on return hints (edge case, not super important I think): |
| 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. | ||
|
|
There was a problem hiding this comment.
| 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 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 |
|
@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 |
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 likeEnumclasses, dataclasses ordatetimeall silently told the model to pass a dict.This PR adds support for
Enumclasses, which become anenumof 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