Implementation:Ollama Ollama Llama Json Schema To Grammar
| Knowledge Sources | |
|---|---|
| Domains | Grammar, Structured Output |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Converts JSON Schema definitions into GBNF (GGML BNF) grammar rules that constrain LLM token generation to produce valid JSON matching the schema.
Description
Implements common_schema_converter which recursively walks a JSON schema (using nlohmann::ordered_json), handling types (object, array, string, number, boolean, null), combinators (oneOf, anyOf, allOf), $ref resolution, enum/const constraints, pattern regexes, and additionalProperties. Uses BuiltinRule definitions for primitive types and a TrieNode-based optimization for string literal alternatives. Builds up named GBNF rules with repetition operators and produces a complete grammar string. The build_repetition helper generates quantified rule expressions.
Usage
Use this to enable structured output and function calling by constraining LLM generation to produce valid JSON conforming to a user-specified schema.
Code Reference
Source Location
- Repository: Ollama
- File: llama/llama.cpp/common/json-schema-to-grammar.cpp
- Lines: 1-1153
Signature
static std::string build_repetition(
const std::string & item_rule,
int min_items, int max_items,
const std::string & separator_rule = "");
static void _build_min_max_int(
int64_t min_value, int64_t max_value,
std::stringstream & out, int decimals_left = 16,
bool top_level = true);
class common_schema_converter {
// Recursively converts JSON schema to GBNF grammar rules
// Handles object, array, string, number, boolean, null types
// Supports $ref, enum, const, oneOf, anyOf, allOf
};
Import
#include "json-schema-to-grammar.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| schema | nlohmann::ordered_json | Yes | JSON Schema definition to convert |
| force_gbnf | bool | No | Force GBNF output format (default: false) |
Outputs
| Name | Type | Description |
|---|---|---|
| grammar | std::string | Complete GBNF grammar string that constrains generation to match the schema |
Usage Examples
#include "json-schema-to-grammar.h"
#include <nlohmann/json.hpp>
using json = nlohmann::ordered_json;
// Define a JSON schema
json schema = {
{"type", "object"},
{"properties", {
{"name", {{"type", "string"}}},
{"age", {{"type", "integer"}, {"minimum", 0}}}
}},
{"required", {"name", "age"}}
};
// Convert to GBNF grammar
std::string grammar = json_schema_to_grammar(schema);
// Result: GBNF rules that constrain output to valid JSON matching the schema