# `LlamaCppEx.Grammar`
[🔗](https://github.com/nyo16/llama_cpp_ex/blob/main/lib/llama_cpp_ex/grammar.ex#L1)

Converts JSON Schema to GBNF grammar for constrained generation.

Uses llama.cpp's built-in `json_schema_to_grammar()` to convert a JSON Schema
into a GBNF grammar string that can be used with the `:grammar` option.

In most cases you don't need to call this module directly — pass `:json_schema`
to `LlamaCppEx.generate/3`, `LlamaCppEx.chat/3`, or any other generate function
and the conversion happens automatically.

## Examples

    schema = %{
      "type" => "object",
      "properties" => %{
        "name" => %{"type" => "string"},
        "age" => %{"type" => "integer"}
      },
      "required" => ["name", "age"],
      "additionalProperties" => false
    }

    {:ok, gbnf} = LlamaCppEx.Grammar.from_json_schema(schema)
    # Use with the :grammar option
    {:ok, sampler} = LlamaCppEx.Sampler.create(model, grammar: gbnf, temp: 0.0)

Supports all JSON Schema types: `object`, `array`, `string`, `number`,
`integer`, `boolean`, `null`, `enum`, `oneOf`, `anyOf`, `allOf`, `$ref`, etc.

# `from_json_schema`

```elixir
@spec from_json_schema(map()) :: {:ok, String.t()} | {:error, String.t()}
```

Converts a JSON Schema map to a GBNF grammar string.

Returns `{:ok, gbnf_string}` on success or `{:error, reason}` on failure.

# `from_json_schema!`

```elixir
@spec from_json_schema!(map()) :: String.t()
```

Converts a JSON Schema map to a GBNF grammar string.

Returns the GBNF string on success or raises on failure.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
