OpenAI Functions and Tools


LocalAI supports running the OpenAI functions and tools API across multiple backends. The OpenAI request shape is the same regardless of which backend runs your model - LocalAI is responsible for extracting structured tool calls from the model’s output before returning the response.
To learn more about OpenAI functions, see also the OpenAI API blog post.
LocalAI also supports JSON mode out of the box on llama.cpp-compatible models.
💡 Check out LocalAGI for an example on how to use LocalAI functions.
Supported backends
| Backend | How tool calls are extracted |
|---|---|
llama.cpp | C++ incremental parser; any ggml/gguf model works out of the box, no configuration needed |
vllm | vLLM’s native ToolParserManager - select a parser with tool_parser:<name> in the model options. Auto-set by the gallery importer for known families |
vllm-omni | Same as vLLM |
mlx | mlx_lm.tool_parsers - auto-detected from the chat template, no configuration needed |
mlx-vlm | mlx_vlm.tool_parsers (with fallback to mlx-lm parsers) - auto-detected from the chat template, no configuration needed |
Reasoning content (<think>...</think> blocks from DeepSeek R1, Qwen3, Gemma 4, etc.) is returned in the OpenAI reasoning_content field on the same backends. When a model both reasons and calls a tool in the same turn, see Interleaved Thinking with Tool Calls for how the reasoning survives the tool-result round trip.
Setup
llama.cpp
No configuration required - the autoparser detects the tool call format for any ggml/gguf model that was trained with tool support.
Migrating older Ministral 3 configurations
The mistralai_ministral-3-14b-reasoning-2512-multimodal gallery entry uses the model’s embedded template and llama.cpp’s native tool parser.
Older installations inherit the Mistral 0.3 prompt and JSON parser, which can return tool calls such as pick_tool{...} as text.
For an existing installation, replace the model YAML’s template and function sections with:
Set use_jinja:true in the existing options list:
Remove the inherited stopwords list so llama.cpp controls the model’s end-of-turn markers.
Keep your model path, mmproj, sampling settings, and MCP configuration. Reload the model after saving the YAML.
Gallery changes do not rewrite installed model configurations.
vLLM / vLLM Omni
The parser must be specified explicitly because vLLM itself doesn’t auto-detect one. Pass it via the model options:
When you import a vLLM model through the LocalAI gallery, the importer looks up the model family and pre-fills tool_parser: and reasoning_parser: for you - you only need to override them for non-standard model names.
Available tool parsers include hermes, llama3_json, llama4_pythonic, mistral, qwen3_xml, deepseek_v3, granite4, kimi_k2, glm45, and more. Available reasoning parsers include deepseek_r1, qwen3, mistral, gemma4, granite. See the upstream vLLM documentation for the full list.
With backend: vllm, vLLM 0.30 or later, and a configured reasoning parser,
streamed responses separate delta.reasoning_content, delta.content, and tool
calls as tokens arrive. The final chunk does not repeat the generated text.
If parsing fails during a stream, the backend buffers the remaining output and
sends any matching, unsent suffixes in the final chunk. Older vLLM versions keep
the previous streaming behavior and can include reasoning in delta.content.
MLX / MLX-VLM
MLX backends auto-detect the right tool parser by inspecting the model’s chat template - you don’t need to set anything. Just load an MLX-quantized model that was trained with tool support:
The gallery importer will still append tool_parser: and reasoning_parser: entries to the YAML for visibility and consistency with the other backends, but those are informational - the runtime auto-detection in the MLX backend ignores them and uses the parser matched to the chat template.
Supported parser families: hermes/json_tools, mistral, gemma4, glm47, kimi_k2, longcat, minimax_m2, pythonic, qwen3_coder, function_gemma.
When streaming with strict: true and LocalAI builds the tool grammar (no no_grammar), LocalAI buffers candidate content until tool-call parsing completes. Recognized tool calls are returned in delta.tool_calls, with no raw tool JSON in delta.content. Reasoning is delivered separately; genuine non-tool text is returned as content after classification.
Usage example
You can configure a model manually with a YAML config file in the models directory, for example:
To use the functions with the OpenAI client in python:
For example, with curl:
Return data:
Advanced
Use functions without grammars
The functions calls maps automatically to grammars which are currently supported only by llama.cpp, however, it is possible to turn off the use of grammars, and extract tool arguments from the LLM responses, by specifying in the YAML file no_grammar and a regex to map the response from the LLM:
no_grammar also applies to tools that set "strict": true. LocalAI does not build its own tool grammar for them. On llama.cpp with use_tokenizer_template, the tools go to the chat template, and the backend uses the grammar that the template generates.
The response regex have to be a regex with named parameters to allow to scan the function name and the arguments. For instance, consider:
(?P<function>\w+)\s*\((?P<arguments>.*)\)will catch
function_name({ "foo": "bar"})Tool call validation
A tool’s schema does not always constrain the arguments the model writes:
- llama.cpp’s autoparser checks the tool name against the request’s tools,
but it parses the arguments as any JSON. Only a grammar built from the
schema constrains them, and with
tool_choice: autollama.cpp builds that grammar only when the template’s tool format has a trigger marker. - When the backend’s parser returns no tool call, LocalAI parses the model’s text for tool calls, with no grammar at all.
So unless LocalAI sends its own grammar, it checks every tool call against the request’s tools, in the chat completions, Anthropic messages and Responses endpoints, streaming and non-streaming. A call is dropped when:
- the tool is not in the request,
- an argument is not in the tool’s schema (unless the schema sets
additionalPropertiestotrueor to a schema), or - a required argument is missing.
When every call is dropped, the response answers with the model’s text as
normal content (a text block for Anthropic, an output_text message for
Responses). A call returned by llama.cpp’s autoparser is not in the model’s
text, so it is written out as {"name": ..., "arguments": ...}. The server log
names the tool and the unknown or missing arguments.
Calls to tools that LocalAI runs itself (MCP servers and add-on tools) are not dropped. LocalAI runs them, and when the arguments are wrong the tool’s error goes back to the model, which can call the tool again.
To keep every call, as older versions did:
Parallel tools calls
This feature is experimental and has to be configured in the YAML of the model by enabling function.parallel_calls:
Use functions with grammar
It is possible to also specify the full function signature (for debugging, or to use with other clients).
The chat endpoint accepts the grammar_json_functions additional parameter which takes a JSON schema object.
For example, with curl:
On the llama.cpp backend, a grammar that LocalAI builds from a JSON schema (tool calls, response_format and grammar_json_functions) uses llguidance when the backend build has it. With grammar_engine:llguidance, the tool-call grammars that llama.cpp builds from the chat template use llguidance too. Set grammar_engine:gbnf in the options of the model to keep the GBNF engine for every grammar. See Grammar engine.
On the llama.cpp backend with use_jinja:true, a request that sets response_format (json_object or json_schema) and sends no tools hands response_format to llama.cpp instead of the grammar that LocalAI builds. llama.cpp then applies the JSON constraint after the thinking block, so a thinking model returns the JSON in content. A request that also sends tools keeps the grammar that LocalAI builds.
Grammars and function tools can be used as well in conjunction with vision APIs:
💡 Examples
A full e2e example with docker-compose is available here.