public Optional<String> maybeVisit(JsonNode schema, String baseName, String name) {
            var typeText = getTypeText(schema);

            if ((typeText == null || typeText.equals("array"))
                    && (schema.has("items") || schema.has("prefixItems"))) {
                var items = schema.has("items") ? schema.get("items") : schema.get("prefixItems");

                if (items.isArray()) {
                    var content = new StringBuilder();
                    content.append("\"[\" space ");

                    for (var i = 0; i < items.size(); i++) {
                        if (i > 0) {
                            content.append(" \",\" space ");
                        }

                        var item = items.get(i);
                        var itemRuleName = (baseName == null ? "" : baseName + "-") + "tuple-" + i;
                        var itemRule = visit(item, itemRuleName);
                        content.append(itemRule);
                    }

                    content.append(" \"]\" space");
                    var rule = addRule(name, content.toString());
                    return Optional.of(rule);
                } else {
                    var itemRuleName = (baseName == null ? "" : baseName + "-") + "item";
                    var visitedItemRuleName = visit(items, itemRuleName);

                    int minItems = schema.has("minItems") ? schema.get("minItems").asInt() : 0;
                    Integer maxItems = schema.has("maxItems") ? schema.get("maxItems").asInt() : null;

                    var content = "\"[\" space " +
                            generateRepetition(visitedItemRuleName, minItems, maxItems, "\",\" space") +
                            " \"]\" space";
                    var rule = addRule(name, content);
                    return Optional.of(rule);
                }
            }

            return Optional.empty();
        }
Converts a JSON Schema to a GBNF grammar to be used for structured output with llama.cpp.
It is a port of Python implementation from llama.cpp repository.
Current implementation doesn't support regex patterns, resolving $ref or fetching schemas from URLs.
The plan is to replace this with C++ implementation from llama.cpp though JNI.
model-integration/src/main/java/ai/vespa/llm/clients/JsonSchemaToGrammar.java