diff --git a/llama_cpp/llama_grammar.py b/llama_cpp/llama_grammar.py index d8ef563..3eb3b96 100644 --- a/llama_cpp/llama_grammar.py +++ b/llama_cpp/llama_grammar.py @@ -1471,12 +1471,15 @@ class SchemaConverter: if schema_type == "object" and "properties" in schema: # TODO: `required` keyword - prop_order = self._prop_order - prop_pairs = sorted( - schema["properties"].items(), - # sort by position in prop_order (if specified) then by key - key=lambda kv: (prop_order.get(kv[0], len(prop_order)), kv[0]), - ) + if self._prop_order: + prop_order = self._prop_order + prop_pairs = sorted( + schema["properties"].items(), + # sort by position in prop_order (if specified) then by key + key=lambda kv: (prop_order.get(kv[0], len(prop_order)), kv[0]), + ) + else: + prop_pairs = schema["properties"].items() rule = '"{" space' for i, (prop_name, prop_schema) in enumerate(prop_pairs):