Skip to content

[Chat Parsing] Support $ref, allOf, const, and enum in _schema_types - #51162

Closed
xgbtxy wants to merge 1 commit into
huggingface:mainfrom
xgbtxy:fix/schema-types-ref-allof-const
Closed

xgbtxy wants to merge 1 commit into
huggingface:mainfrom
xgbtxy:fix/schema-types-ref-allof-const

Conversation

@xgbtxy

@xgbtxy xgbtxy commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

🤗 Transformers CI

What does this PR do?

Follow-up to #51069: _schema_types in src/transformers/utils/chat_parsing/response_parser.py previously only checked type, anyOf, oneOf, and nullable, returning () for properties defined via local $ref (#/$defs/..., #/definitions/...), allOf, const, or enum.

As a result:

  • Untyped tool-call parsers (xml-inline, kv-lines) skipped coercion for parameters declared with $ref, allOf, const, or enum when tools= was passed.
  • Typed parsers (json, python-call-list) could not see "string" inside string-valued $ref/allOf/const/enum schemas.

Changes

  • Add _resolve_local_ref(ref, root) with RFC 6901 pointer unescaping (~1 -> /, ~0 -> ~) and cycle protection via _seen.
  • Add _json_value_type(value) (checking bool before int) for const and enum schemas.
  • Extend _schema_types(schema, root=...) to traverse $ref, allOf (narrowing compatible branches such as ["number"] + ["integer"] -> ("integer",) and falling back cleanly when branches are disjoint), const, and enum, preserving insertion order without duplicates.
  • Store self._tool_roots[name] = parameters in ResponseParser.__init__ and pass root from _coerce_tool_calls.
  • Add ToolArgCoercionTest.test_schema_types_and_coerce_handle_ref_allof_const_enum covering nested/escaped/cyclic $ref, allOf, const (bool/int/str), enum, untyped coercion, and typed-parser string preservation.

Verification

  • Targeted and suite tests on tests/utils/test_chat_parsing.py (ToolArgCoercionTest: 25 non-AutoTokenizer tests passing; test_chat_parsing.py: 83 passed, 12 skipped, only pre-existing missing fixtures/chat_parsing/python_call_list.json in sparse checkout) and tests/utils/test_chat_template_utils.py (46 passed, 11 subtests passed).
  • ruff check and ruff format --check clean on both modified files.

Disclosure: Assisted by AI tooling during root-cause analysis and test/patch drafting; all code and tests were verified locally.

Follow up on huggingface#51069 by teaching `_schema_types` to resolve local JSON Pointer
`$ref` targets (`#/$defs/...`, `#/definitions/...`), traverse `allOf` branches,
and infer literal types from `const` and `enum`, passing each tool's root
`parameters` schema from `ResponseParser` for `$ref` lookup.
Copilot AI balanced review requested due to automatic review settings November 24, 2026 07:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@Rocketknight1

Copy link
Copy Markdown
Member

Hmm, not sure about this one; I'm not sure we actually want to support the whole range of JSON schema, considering that I'm unaware of any chat templates that can parse them. So this seems like a lot of code complexity with no clear purpose

@xgbtxy

xgbtxy commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Fair point — get_json_schema() itself already emits type alongside enum and does not generate $ref/allOf/const, so this only affects external Pydantic/MCP tool schemas passed directly as dicts. If you'd rather not carry JSON Schema resolution complexity in response_parser.py, I'm happy to either trim this down to a minimal enum-only fallback or close the PR — whichever you prefer.

@Rocketknight1

Copy link
Copy Markdown
Member

Yes, I think we can close this for now!

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