diff --git a/common/json-schema-to-grammar.cpp b/common/json-schema-to-grammar.cpp index 6f3ecc992..b6ea61fc3 100644 --- a/common/json-schema-to-grammar.cpp +++ b/common/json-schema-to-grammar.cpp @@ -860,6 +860,7 @@ public: std::unordered_set required; std::vector> properties; std::map enum_values; + std::vector leaves; std::function add_component = [&](const common_chat_schema & comp, bool is_required, int depth) { if (depth >= COMMON_RECURSIVE_LIMIT) { throw std::invalid_argument("JSON schema is nested more than " + std::to_string(COMMON_RECURSIVE_LIMIT) + " levels deep"); @@ -879,6 +880,10 @@ public: for (const auto & v : as(comp).values) { enum_values[_generate_constant_rule(v)] += 1; } + } else if (is_required && comp.kind() != common_chat_schema::KIND_ANY) { + // Direct non-structural children are used for merging. anyOf alternatives are not merged. + // Empty schemas are skipped. + leaves.push_back(&comp); } }; for (const auto & child : schema.children) { @@ -901,6 +906,38 @@ public: return _add_rule(rule_name, "(" + string_join(enum_intersection, " | ") + ")"); } } + + if (properties.empty() && !leaves.empty()) { + if (leaves.size() == 1) { + return visit(*leaves[0], name); + } + bool all_numeric = true; + bool any_integer = false; + int64_t lo = std::numeric_limits::min(); + int64_t hi = std::numeric_limits::max(); + for (const auto * leaf : leaves) { + if (leaf->kind() == common_chat_schema::KIND_INTEGER) { + const auto & i = as(*leaf); + any_integer = true; + lo = std::max(lo, i.minimum); + hi = std::min(hi, i.maximum); + } else if (leaf->kind() != common_chat_schema::KIND_NUMBER) { + all_numeric = false; + } + } + if (all_numeric) { + if (lo > hi) { + throw std::invalid_argument("allOf has an empty numeric range"); + } + if (any_integer) { + common_chat_schema_integer merged; + merged.minimum = lo; + merged.maximum = hi; + return visit(merged, name); + } + return visit(*leaves[0], name); + } + } return _add_rule(rule_name, _build_object_rule(properties, required, name, nullptr)); } diff --git a/tests/test-json-schema-to-grammar.cpp b/tests/test-json-schema-to-grammar.cpp index c7d971b29..ea10ad7e7 100755 --- a/tests/test-json-schema-to-grammar.cpp +++ b/tests/test-json-schema-to-grammar.cpp @@ -1325,6 +1325,66 @@ static void test_all(const std::string & title, std::function