Skip to main content

json_schema_to_grammar

Function json_schema_to_grammar 

Source
pub fn json_schema_to_grammar(
    schema_json: &str,
    force_gbnf: bool,
) -> Result<String, ChatError>
Expand description

Convert a JSON Schema into a GBNF grammar.

The output goes straight to LlamaSampler::grammar, which is how you implement OpenAI’s response_format: json_schema — the model then cannot emit anything the schema rejects, rather than being asked nicely.

force_gbnf mirrors upstream’s flag: leave it false and the converter may pick a more compact representation for schemas it recognises.

let gbnf = json_schema_to_grammar(r#"{"type":"integer"}"#, false).unwrap();
assert!(gbnf.contains("root"));

§Errors

Returns ChatError::BadJson if schema_json is not valid JSON, or ChatError::Failed if it is not a schema llama.cpp can convert.