From c5a5535e6ebc2e74ab0e3f1b249d7d989aba26a2 Mon Sep 17 00:00:00 2001 From: Sergey Sklyarov Date: Thu, 3 Sep 2026 22:37:50 +0200 Subject: [PATCH] common/json-schema : fix GBNF grammar generation for empty object schemas (#28279) --- common/json-schema-to-grammar.cpp | 4 ++++ examples/json_schema_to_grammar.py | 3 +++ tests/test-json-schema-to-grammar.cpp | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/common/json-schema-to-grammar.cpp b/common/json-schema-to-grammar.cpp index 0aee51b26..a7a18857d 100644 --- a/common/json-schema-to-grammar.cpp +++ b/common/json-schema-to-grammar.cpp @@ -748,6 +748,10 @@ private: optional_props.push_back("*"); } + if (required_props.empty() && optional_props.empty()) { + return "\"{\" space \"}\""; + } + std::string rule = "\"{\" space "; for (size_t i = 0; i < required_props.size(); i++) { if (i > 0) { diff --git a/examples/json_schema_to_grammar.py b/examples/json_schema_to_grammar.py index 83abd259d..02b7ef15a 100755 --- a/examples/json_schema_to_grammar.py +++ b/examples/json_schema_to_grammar.py @@ -734,6 +734,9 @@ class SchemaConverter: ) optional_props.append("*") + if not required_props and not optional_props: + return '"{" space "}"' + rule = '"{" space ' rule += ' "," space '.join(prop_kv_rule_names[k] for k in required_props) diff --git a/tests/test-json-schema-to-grammar.cpp b/tests/test-json-schema-to-grammar.cpp index 214dbe199..2a1b6348c 100755 --- a/tests/test-json-schema-to-grammar.cpp +++ b/tests/test-json-schema-to-grammar.cpp @@ -994,7 +994,7 @@ static void test_all(const std::string & lang, std::function