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: support unconstrained array item schemas by @tuanzirwar (#2378)

## [0.3.36]

- feat: update llama.cpp to ggml-org/llama.cpp@0c1e57098
Expand Down
9 changes: 7 additions & 2 deletions llama_cpp/llama_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,9 @@ def add_component(comp_schema, is_required):
elif schema_type in (None, "array") and (
"items" in schema or "prefixItems" in schema
):
items = schema.get("items") or schema["prefixItems"]
items = schema.get("items") or schema.get("prefixItems")
if items is None:
items = schema["items"]
if isinstance(items, list):
return self._add_rule(
rule_name,
Expand Down Expand Up @@ -830,7 +832,10 @@ def add_component(comp_schema, is_required):
+ r' "\"" space',
)

elif (schema_type == "object") or (len(schema) == 0):
elif len(schema) == 0:
return self._add_primitive(rule_name, PRIMITIVE_RULES["value"])

elif schema_type == "object":
return self._add_rule(
rule_name, self._add_primitive("object", PRIMITIVE_RULES["object"])
)
Expand Down
53 changes: 53 additions & 0 deletions tests/test_llama_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,56 @@ def test_grammar_anyof():
grammar = llama_cpp.LlamaGrammar.from_json_schema(json.dumps(sch))

# assert grammar.grammar is not None


def test_grammar_unconstrained_array_items():
schema = {"type": "array", "items": {}}
grammar = llama_cpp.LlamaGrammar.from_json_schema(json.dumps(schema))
rules = dict(line.split(" ::= ", 1) for line in grammar._grammar.splitlines())
assert rules["item"] == "object | array | string | number | boolean | null"
assert "item" in rules["root"]


def test_grammar_unconstrained_tuple_item():
schema = {"type": "array", "prefixItems": [{}, {"type": "integer"}]}
grammar = llama_cpp.LlamaGrammar.from_json_schema(json.dumps(schema))
rules = dict(line.split(" ::= ", 1) for line in grammar._grammar.splitlines())
assert rules["tuple-0"] == "object | array | string | number | boolean | null"
assert "integer" in rules["root"]


def test_grammar_empty_schema_allows_any_json_value():
grammar = llama_cpp.LlamaGrammar.from_json_schema("{}")
rules = dict(line.split(" ::= ", 1) for line in grammar._grammar.splitlines())
assert rules["root"] == "object | array | string | number | boolean | null"


def test_grammar_typed_array_preserves_item_constraints():
grammar = llama_cpp.LlamaGrammar.from_json_schema(
json.dumps(
{
"type": "array",
"items": {"type": "integer"},
"minItems": 1,
"maxItems": 2,
}
)
)
assert (
"integer"
in dict(line.split(" ::= ", 1) for line in grammar._grammar.splitlines())[
"root"
]
)


def test_grammar_closed_tuple_preserves_prefix_items():
schema = {
"type": "array",
"prefixItems": [{"type": "integer"}, {"type": "string"}],
"items": False,
}
grammar = llama_cpp.LlamaGrammar.from_json_schema(json.dumps(schema))
rules = dict(line.split(" ::= ", 1) for line in grammar._grammar.splitlines())
assert "integer" in rules["root"]
assert "string" in rules["root"]