[Frontend] Prevent Kimi K3 reserved markers in response text (#52889)

Signed-off-by: Bugen Zhao <i@bugenzhao.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
Bugen Zhao
2026-08-21 09:06:52 -07:00
committed by GitHub
co-authored by OpenAI Codex
parent 6d8cd88e8d
commit 08b73d07cc
4 changed files with 31 additions and 7 deletions
+9 -3
View File
@@ -376,6 +376,7 @@ _K3_ARG_CLOSE = f"{_K3_CLOSE}argument{_K3_SEP}"
# the end-of-message token. It is generated (not part of the prompt prefix), so
# the tag must permit it or the FSM would mask the model's natural terminator.
_K3_MESSAGE_CLOSE = f"{_K3_CLOSE}message{_K3_SEP}"
_K3_END_OF_MSG = "<|end_of_msg|>"
# JSON-schema type -> K3 XTML ``type=`` attribute value. Mirrors
# ``encoding_k3._xtml_type`` (integer collapses onto number).
@@ -556,12 +557,17 @@ def _k3_response_prefix() -> list[Any]:
``response`` is generated in thinking mode (prefix ends at
``<|open|>think<|sep|>``) but is part of the generation prefix in
non-thinking mode, so its open marker is optional. The body is bounded by
the response close marker.
non-thinking mode, so its open marker is optional. Excluding the reserved
marker heads commits ``<|close|>`` to the response-close branch immediately
and prevents a nested channel from starting inside response text.
"""
return [
OptionalFormat(content=ConstStringFormat(value=_K3_RESPONSE_OPEN)),
TagFormat(begin="", content=AnyTextFormat(), end=_K3_RESPONSE_CLOSE),
TagFormat(
begin="",
content=AnyTextFormat(excludes=[_K3_OPEN, _K3_CLOSE, _K3_END_OF_MSG]),
end=_K3_RESPONSE_CLOSE,
),
]