[Bugfix] Suppress HarmonyError Unexpected token while expecting start token 200006 (#59254)
Signed-off-by: Yifan Zong <yzong@redhat.com>
yzong-rh committed
Sep 29, 2026 at 16:40 UTC
1d331f8341f09636c0182a4b48112f8e4f7d027c
2 files changed
+17
-2
tests/parser/test_harmony.py
+9
@@ -872,6 +872,15 @@ class TestProcessChunk:
872
("final", "Two"),
873
]
874
875
+ def test_malformed_token_does_not_raise(self, harmony_parser):
876
+ malformed = encode_output(
877
+ "<|channel|>analysis<|message|>think<|end|><|return|>"
878
+ "<|start|>assistant<|channel|>final<|message|>answer<|return|>"
879
+ )
880
+
881
+ result = harmony_parser.process_chunk(malformed)
882
+ assert "".join(s.delta for s in result.segments if s.delta) == "thinkanswer"
883
+
884
885
class TestCountReasoningTokens:
886
def test_matches_process_chunk(self, harmony_parser, gpt_oss_tokenizer):
vllm/parser/harmony.py
+8
-2
@@ -335,7 +335,10 @@ class HarmonyParser(DelegatingParser):
335
segments: list[Segment] = []
336
reasoning_token_count = 0
337
for token_id in token_ids:
338
- self._harmony_parser.process(token_id)
338
+ try:
339
+ self._harmony_parser.process(token_id)
340
+ except HarmonyError:
341
+ continue
342
channel = self._harmony_parser.current_channel
343
recipient = self._normalize_recipient(
344
self._harmony_parser.current_recipient
@@ -376,7 +379,10 @@ class HarmonyParser(DelegatingParser):
379
parser = get_streamable_parser_for_assistant()
380
count = 0
381
for token_id in token_ids:
379
- parser.process(token_id)
382
+ try:
383
+ parser.process(token_id)
384
+ except HarmonyError:
385
+ continue
386
recipient = self._normalize_recipient(parser.current_recipient)
387
if self._is_reasoning_token(token_id, parser.current_channel, recipient):
388
count += 1