Skip to content

Fix serve tool parsing for legacy response schemas#47033

Open
Sunt-ing wants to merge 1 commit into
huggingface:mainfrom
Sunt-ing:8
Open

Fix serve tool parsing for legacy response schemas#47033
Sunt-ing wants to merge 1 commit into
huggingface:mainfrom
Sunt-ing:8

Conversation

@Sunt-ing

@Sunt-ing Sunt-ing commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

CI

What does this PR do?

#46980 made new-style response templates require prefix=, but legacy response_schema rejects prefix. parse_tool_calls currently passes prefix="" unconditionally, so the serving tool-call path crashes for legacy schemas and the Qwen3.5 fallback schema before parsing any tool call.

This only passes prefix="" for new-style schemas (version or fields) and keeps legacy schemas on the no-prefix path. The existing new-style fallback remains covered, and this adds a regression guard for the legacy fallback.

Repro and output
from transformers.cli.serving.utils import _TOOL_CALL_FALLBACKS, parse_tool_calls
from transformers.tokenization_utils_base import PreTrainedTokenizerBase


class Processor:
    parse_response = PreTrainedTokenizerBase.parse_response


processor = Processor()
schema = next(v["schema"] for k, v in _TOOL_CALL_FALLBACKS.items() if "qwen3_5" in k)
text = "<function=get_weather><parameter=city>Paris</parameter></function>"

print("schema keys", sorted(schema.keys()))
print("text", text)
print(parse_tool_calls(processor, text, schema))

Before this PR:

schema keys ['items', 'type', 'x-regex-iterator']
text <function=get_weather><parameter=city>Paris</parameter></function>
ValueError: `prefix=` is only supported with new-style `response_template` specs, not legacy `response_schema`.

After this PR:

schema keys ['items', 'type', 'x-regex-iterator']
text <function=get_weather><parameter=city>Paris</parameter></function>
[{'name': 'get_weather', 'arguments': '{"city": "Paris"}'}]
Tests
python -m pytest -q tests/cli/test_serve.py::TestToolCallUnit::test_parse_tool_calls_from_text tests/cli/test_serve.py::TestToolCallUnit::test_parse_multiple_tool_calls_from_text tests/cli/test_serve.py::TestToolCallUnit::test_parse_tool_calls_with_legacy_schema_does_not_pass_prefix

tests/cli/test_serve.py::TestToolCallUnit::test_parse_tool_calls_from_text PASSED [ 33%]
tests/cli/test_serve.py::TestToolCallUnit::test_parse_multiple_tool_calls_from_text PASSED [ 66%]
tests/cli/test_serve.py::TestToolCallUnit::test_parse_tool_calls_with_legacy_schema_does_not_pass_prefix PASSED [100%]

============================== 3 passed in 0.31s ===============================

python -m ruff check src/transformers/cli/serving/utils.py tests/cli/test_serve.py
All checks passed!

git diff --check

Code Agent Policy

  • I confirm that this is not a pure code agent PR.

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 and the
    Pull Request checks?
  • 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 according to the guidelines?
  • Did you write any new necessary tests?

Who can review?

@Rocketknight1 @SunMarc

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 28625420848:2
Result: success | Jobs: 2 | Tests: 7 | Failures: 0 | Duration: 25s

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.

1 participant