[Bugfix][Frontend][gpt-oss] Return raw output when Harmony parser ends non-terminal (#47062)

Signed-off-by: Achyuthan Sivasankar <achyuthan.sivasankar@gmail.com>
This commit is contained in:
achyuthan.s
2026-07-01 01:46:01 +00:00
committed by GitHub
parent a264e41975
commit 3406e8f83d
2 changed files with 69 additions and 17 deletions
+42 -5
View File
@@ -7,6 +7,7 @@ from collections.abc import Sequence
import pytest
from openai_harmony import (
Conversation,
HarmonyError,
Message,
RenderConversationConfig,
Role,
@@ -81,9 +82,8 @@ def get_model_output_tokens(
Role.ASSISTANT,
config=config,
)
full_ids = enc.render_conversation_for_completion(
full_ids = enc.render_conversation(
Conversation.from_messages([*prompt_messages, *response_messages]),
Role.ASSISTANT,
config=config,
)
assert full_ids[: len(prompt_ids)] == prompt_ids
@@ -147,12 +147,12 @@ class TestFlush:
assert get_text(flushed.completed_message) == "Think"
assert harmony_parser._parser is None
def test_flush_resets_after_eos_error(self, harmony_parser):
def test_flush_raises_and_resets_on_non_terminal_eos(self, harmony_parser):
harmony_parser.process_chunk(encode_output("<|channel|>analysis"))
flushed = harmony_parser.flush()
with pytest.raises(HarmonyError):
harmony_parser.flush()
assert flushed is None
assert harmony_parser._parser is None
@@ -396,6 +396,23 @@ class TestParse:
assert tool_calls is None
assert harmony_parser._parser is None
def test_malformed_final_recovers_raw_content(self, harmony_parser, chat_request):
raw_output = (
"<|channel|>analysis<|message|>thinking<|end|>"
'<|start|>assistant<|channel|>final {"answer": "hi"}<|return|>'
)
reasoning, content, tool_calls = harmony_parser.parse(
raw_output,
chat_request,
model_output_token_ids=encode_output(raw_output),
)
assert content == raw_output
assert reasoning is None
assert tool_calls is None
assert harmony_parser._parser is None
@pytest.mark.parametrize(
("harmony_str", "expected_content"),
[
@@ -489,6 +506,26 @@ class TestParseDelta:
assert delta.reasoning is None
assert not delta.tool_calls
def test_malformed_final_recovers_raw_content(
self, gpt_oss_tokenizer, chat_request
):
parser = HarmonyParser(gpt_oss_tokenizer)
delta = parser.parse_delta(
delta_text='final {"answer": "hi"}',
delta_token_ids=encode_output(
'<|channel|>final {"answer": "hi"}<|return|>'
),
request=chat_request,
finished=True,
)
assert delta is not None
assert delta.content == 'final {"answer": "hi"}'
assert delta.reasoning is None
assert not delta.tool_calls
assert parser._parser is None
@pytest.mark.parametrize("tool_channel", ["commentary", "analysis"])
def test_tool_call_split_across_deltas(
self, gpt_oss_tokenizer, chat_request, tool_channel