Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- fix(grammar): preserve type-specific constraints in schema unions by @remote-controlled-man in #2449

- fix: load cached pretrained models in offline mode by @emme1t in #2396
- fix(ci): install matching NVML headers for CUDA wheel builds

Expand Down
4 changes: 3 additions & 1 deletion llama_cpp/llama_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,9 @@ def visit(self, schema, name):
elif isinstance(schema_type, list):
return self._add_rule(
rule_name,
self._generate_union_rule(name, [{"type": t} for t in schema_type]),
self._generate_union_rule(
name, [{**schema, "type": t} for t in schema_type]
),
)

elif "const" in schema:
Expand Down
134 changes: 134 additions & 0 deletions tests/test_llama_grammar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import llama_cpp
import json
import ctypes
from pathlib import Path

import pytest

from llama_cpp.llama_grammar import json_schema_to_gbnf

tree = """
leaf ::= "."
Expand Down Expand Up @@ -76,3 +82,131 @@ def test_grammar_anyof():
grammar = llama_cpp.LlamaGrammar.from_json_schema(json.dumps(sch))

# assert grammar.grammar is not None


@pytest.fixture(scope="module")
def grammar_accepts():
llama_cpp.llama_backend_init()
params = llama_cpp.llama_model_default_params()
params.vocab_only = True
path = (
Path(__file__).resolve().parents[1]
/ "vendor/llama.cpp/models/ggml-vocab-llama-spm.gguf"
)
model = llama_cpp.llama_model_load_from_file(str(path).encode(), params)
assert model
try:
vocab = llama_cpp.llama_model_get_vocab(model)
pieces = {}
buffer = ctypes.create_string_buffer(32)
for token in range(llama_cpp.llama_vocab_n_tokens(vocab)):
size = llama_cpp.llama_token_to_piece(
vocab, token, buffer, len(buffer), 0, False
)
if size == 1:
pieces[buffer.raw[:size]] = token

def accepts(schema, text):
grammar = json_schema_to_gbnf(json.dumps(schema))
sampler = llama_cpp.llama_sampler_init_grammar(
vocab, grammar.encode(), b"root"
)
assert sampler
try:
tokens = [pieces[bytes([byte])] for byte in text.encode()]
tokens.append(llama_cpp.llama_vocab_eos(vocab))
for token in tokens:
candidate = (llama_cpp.llama_token_data * 1)(
llama_cpp.llama_token_data(token, 0.0, 0.0)
)
candidates = llama_cpp.llama_token_data_array(
candidate, 1, -1, False
)
llama_cpp.llama_sampler_apply(sampler, ctypes.byref(candidates))
if candidate[0].logit == float("-inf"):
return False
llama_cpp.llama_sampler_accept(sampler, token)
return True
finally:
llama_cpp.llama_sampler_free(sampler)

yield accepts
finally:
llama_cpp.llama_model_free(model)


@pytest.mark.parametrize("type_form", ["single", "nullable", "null-first"])
@pytest.mark.parametrize(
("schema", "valid", "invalid"),
[
(
{"type": "string", "minLength": 2, "maxLength": 3},
['"ab"', '"abc"'],
['"a"', '"abcd"'],
),
(
{"type": "string", "pattern": "^[ab]+$"},
['"a"', '"abba"'],
['""', '"c"'],
),
(
{
"type": "array",
"items": {"type": "integer"},
"minItems": 1,
"maxItems": 2,
},
["[1]", "[1,2]"],
["[]", "[1,2,3]", '["a"]'],
),
(
{
"type": "object",
"properties": {"value": {"type": "integer"}},
"required": ["value"],
"additionalProperties": False,
},
['{"value":1}'],
["{}", '{"value":"a"}', '{"value":1,"extra":2}'],
),
(
{"type": "string", "format": "date"},
['"2026-11-25"'],
['"2026-13-25"', '"abc"'],
),
(
{"type": "array", "prefixItems": [{"type": "integer"}]},
["[1]"],
["[]", '["a"]', "[1,2]"],
),
],
)
def test_type_union_preserves_constraints(
grammar_accepts, type_form, schema, valid, invalid
):
if type_form != "single":
types = [schema["type"], "null"]
schema = {**schema, "type": types if type_form == "nullable" else types[::-1]}
for text in valid:
assert grammar_accepts(schema, text), text
for text in invalid:
assert not grammar_accepts(schema, text), text
assert grammar_accepts(schema, "null") == (type_form != "single")


def test_type_union_applies_constraints_only_to_matching_type(grammar_accepts):
schema = {"type": ["string", "integer"], "minLength": 2, "maxLength": 3}
assert grammar_accepts(schema, '"ab"')
assert grammar_accepts(schema, "1")
assert not grammar_accepts(schema, '"a"')
assert not grammar_accepts(schema, "null")


def test_nested_type_union_preserves_constraints(grammar_accepts):
schema = {
"$defs": {"name": {"type": ["string", "null"], "pattern": "^[ab]+$"}},
"type": "array",
"items": {"$ref": "#/$defs/name"},
}
assert grammar_accepts(schema, '[null,"abba"]')
assert not grammar_accepts(schema, '["c"]')
Loading