[Bugfix][GPT-OSS] Fix strict tool-call grammar to accept Harmony renders (#52222)

Signed-off-by: Yifan Zong <yzong@redhat.com>
Co-authored-by: Ben Browning <56071+bbrowning@users.noreply.github.com>
This commit is contained in:
yzong-rh
2026-08-26 18:05:09 -07:00
committed by GitHub
co-authored by Ben Browning
parent d1e5e66ee3
commit 51d1b45182
2 changed files with 111 additions and 81 deletions
+24 -20
View File
@@ -387,6 +387,7 @@ class HarmonyParser(DelegatingParser):
return recipient[:constrain_index].rstrip() or None
_JSON_CONSTRAINS = [" json", " <|constrain|>json"]
# Harmomy's stop tokens are <|return|>, <|call|>, <|endoftext|>
# They are represented as "" since xgrammar disallows stop tokens while
# under constraints, leading to bad or infinite generations.
@@ -398,10 +399,8 @@ _TOOL_CALL_CHANNELS = [
"<|channel|>final",
]
_FUNCTION_CALL_BEGINS = [
"to=functions.{name} {channel} json<|message|>",
"to=functions.{name} {channel} <|constrain|>json<|message|>",
"{channel} to=functions.{name} json<|message|>",
"{channel} to=functions.{name} <|constrain|>json<|message|>",
" to=functions.{name}{channel}{constrain}<|message|>",
"{channel} to=functions.{name}{constrain}<|message|>",
]
_JSON_CONTENT = JSONSchemaFormat(json_schema={"type": "object"})
_ANY_CONTENT = AnyTextFormat()
@@ -476,7 +475,9 @@ def get_harmony_structural_tag(
else:
tags = [
TagFormat(
begin=pattern.format(name=tool.function.name, channel=channel),
begin=pattern.format(
name=tool.function.name, channel=channel, constrain=constrain
),
content=JSONSchemaFormat(
json_schema=get_function_parameters(tool.function)
),
@@ -484,23 +485,18 @@ def get_harmony_structural_tag(
)
for tool in tools
for pattern in _FUNCTION_CALL_BEGINS
for constrain in _JSON_CONSTRAINS
for channel in _TOOL_CALL_CHANNELS
]
if tool_choice == "auto":
tags.append(
tags.extend(
TagFormat(
begin=_FINAL_BEGIN.format(constrain=" <|constrain|>json"),
content=_ANY_CONTENT,
end=_END_TAG,
)
)
tags.append(
TagFormat(
begin=_FINAL_BEGIN.format(constrain=""),
begin=_FINAL_BEGIN.format(constrain=constrain),
content=_ANY_CONTENT,
end=_END_TAG,
)
for constrain in _JSON_CONSTRAINS + [""]
)
return _assemble_tag(
@@ -558,14 +554,22 @@ def _adjust_output_format(
return request
if isinstance(final_content, JSONSchemaFormat):
begin = _FINAL_BEGIN.format(constrain=" <|constrain|>json")
content = OrFormat(
elements=[
TagFormat(
begin=_FINAL_BEGIN.format(constrain=constrain),
content=final_content,
end=_END_TAG,
)
for constrain in _JSON_CONSTRAINS
]
)
else:
begin = _FINAL_BEGIN.format(constrain="")
content = TagFormat(
begin=_FINAL_BEGIN.format(constrain=""), content=final_content, end=_END_TAG
)
structural_tag = _assemble_tag(
allow_analysis=True,
allow_commentary=False,
content=TagFormat(begin=begin, content=final_content, end=_END_TAG),
allow_analysis=True, allow_commentary=False, content=content
)
request.structured_outputs = replace(