From d1822fed6b706f38bd1ff0de4dec5baaa3cf84fa Mon Sep 17 00:00:00 2001 From: Andrei Betlen Date: Tue, 13 Feb 2024 02:44:00 -0500 Subject: [PATCH] fix: Don't change order of json schema object properties unless prop_order is passed, Closes #1180 --- llama_cpp/llama_grammar.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) 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):