server : fix json_schema response_format ignored by some chat templates - #21537
server : fix json_schema response_format ignored by some chat templates#21537wiktoraleksanderkaczor wants to merge 1 commit into
Conversation
|
Hi @wiktoraleksanderkaczor, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
|
Tagging y'all directly as the auto-assigned team review doesn't seem to have gone through (I think) given missing page when clicking it via https://github.com/orgs/ggml-org/teams/llama-server PR fixes |
It has been assigned correctly, GitHub is just being coy about it. :) |
pwilkin
left a comment
There was a problem hiding this comment.
Seems OK, just maybe do an extra check here to be safe.
|
|
||
| // json_schema fallback: if the chat template did not produce a grammar, pass json_schema | ||
| // through to server-task.cpp and skip template grammar artifacts to avoid conflicts. | ||
| bool json_schema_passthrough = !json_schema.is_null() && chat_params.grammar.empty(); |
There was a problem hiding this comment.
Might want to check if the json schema isn't empty and not just null, just in case.
|
@aldehir need a second look here, especially on whether bypassing the parser with response schema like this will be OK. |
|
I think this has the potential to make things worse for certain models, especially reasoning ones, as they will be constrained to not reason and it will break the parsing underneath. It's simple to implement the response format support to the models that don't currently support it. |
|
LFM2 issue with structured output was fixed in #24377 |
Overview
response_formatwithtype: json_schemais silently ignored when the active chat template's specialized handler does not process thejson_schemafield. The model generates unconstrained text instead of schema-valid JSON.Affected handlers include at least the LFM2 and LFM2.5 ones. Possibly more... basically any that do not explicitly handle
json_schemaand write it back intollama_paramsso it doesn't get lost.When
json_schemais present but the chat template produced an empty grammar for it, passjson_schemathrough toserver-task.cpp's existingjson_schema_to_grammarpath. Also skip the template's grammar triggers, preserved tokens, generation prompt, and chat parser to avoid conflicts.Handlers that already handle
json_schema(GPT-OSS, Gemma4, Ministral, autoparser, legacy) produce a non-empty grammar, so the fallback never activates for them.Additional information
You can reproduce the issue and verify the fix via below command:
Requirements