]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
server : return HTTP 400 on invalid grammar (#24144) (#24154)
authorAnuj Attri <redacted>
Thu, 18 Jun 2026 10:49:14 +0000 (06:49 -0400)
committerGitHub <redacted>
Thu, 18 Jun 2026 10:49:14 +0000 (12:49 +0200)
Throw on grammar parse failure so the server returns HTTP 400
instead of silently dropping the constraint.
Add a regression test for the invalid-grammar response.

Fixes #24144

common/sampling.cpp
tools/server/tests/unit/test_chat_completion.py

index c537f335039cc77e090c944e8c84a0c3362870af..75a299e23eceee419cfec5f208606ec36fb5de01 100644 (file)
@@ -259,6 +259,9 @@ struct common_sampler * common_sampler_init(const struct llama_model * model, st
              }
         }
     }
+    if (!grmr && !grammar_str.empty()) {
+        throw std::runtime_error("failed to parse grammar");
+    }
 
     // Compute prefill tokens from the generation prompt
     std::vector<llama_token> prefill_tokens;
index fe55dc5ab1794cbe5c323c53fd8d347551ab6dc3..b00aac649d7ad6ea1d26aaf4f3165892b999c2f4 100644 (file)
@@ -307,6 +307,20 @@ def test_completion_with_grammar(jinja: bool, grammar: str, n_predicted: int, re
     assert match_regex(re_content, choice["message"]["content"]), choice["message"]["content"]
 
 
+def test_completion_with_invalid_grammar():
+    global server
+    server.start()
+    res = server.make_request("POST", "/chat/completions", data={
+        "max_tokens": 8,
+        "messages": [
+            {"role": "user", "content": "Does not matter what I say, does it?"},
+        ],
+        "grammar": "root ::= this is (not valid GBNF",
+    })
+    assert res.status_code == 400, res.body
+    assert "error" in res.body
+
+
 @pytest.mark.parametrize("messages", [
     None,
     "string",