server: workaround new chat parser regression#20729
Open
jpohhhh wants to merge 1 commit intoggml-org:masterfrom
Open
server: workaround new chat parser regression#20729jpohhhh wants to merge 1 commit intoggml-org:masterfrom
jpohhhh wants to merge 1 commit intoggml-org:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The new chat parser (#18675,
566059a26) added astd::runtime_error(chat.cpp L1740) when parsing fails on the final call (is_partial=false). During streaming, parse failures fall back to partial results and everything works. On the final parse (server-task.h L381) of the exact same text, it throws instead.Server users get HTTP 500 and no final SSE event, even though they already got all the output, and no further would be emitted if this didn't throw. API users calling
common_chat_parsedirectly get astd::runtime_errorthat didn't exist before the new parser. Both cases: inference completes, output is generated, then discarded on the final parse. Same requests return 200 withfinish_reasonand timings on34df42f7b(commit before #18675).This PR catches the throw on the final parse and falls back to the last successful streaming result.
I use the server API and would prefer the throw on final parse be removed in
common_chat_parseitself. (#20708) This is the key regression. But the parser maintainers haven't been amenable to that, so this PR works around it at the server level instead.Repros with stock models. Does not repro on
34df42f7b(commit before #18675):Why these fail
The parser and grammar are both generated from the chat template. The grammar constrains generation, the parser extracts structure afterward. When they're aligned, everything works. Three ways they become misaligned:
Grammar is lazy with
tool_choice=auto(chat-auto-parser-generator.cpp L64): grammar only activates after a trigger pattern. If the model never hits the trigger (decides not to call tools), output is unconstrained but the parser still expects tool call structure.Grammar not generated for some request combos. GPT-OSS with
json_schemabuilds a parser (chat.cpp L858) but skips grammar (chat.cpp L904). Parser validates, grammar doesn't constrain.Model ignores template tags. Cohere expects
<|START_RESPONSE|>wrapping. Model skips them*, parser fails at pos 0. *: not sure why, this is a very interesting case10 of 48 bundled templates crash
--chat-template-fileoverrides the model's built-in template. Any small model can test all 48 bundled templates. The model generates output that doesn't match the foreign template, which is the kind of unexpected input the parser needs to handle without crashing.Affected: Llama 3.2, Llama 3.3, Cohere Command-R, Cohere Command-R7B, GLM-4.7, Nemotron Nano v2, Functionary v3.2, Mistral Small 3.2, GPT-OSS, Solar.