Implementation:Ggml org Llama cpp Chat Parser Xml Toolcall Header
| Knowledge Sources | |
|---|---|
| Domains | Chat, Tool_Calling |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Declares the `xml_tool_call_format` configuration struct and functions for XML-style tool call grammar building and parsing.
Description
The `xml_tool_call_format` struct defines configurable XML tag delimiters: `scope_start`/`scope_end` (outer wrapper), `tool_start`/`tool_end` (per-tool invocation), `tool_sep` (separator after tool name), `key_start`/`key_val_sep`/`val_end` (parameter key-value structure). Optional fields control whether argument values are raw strings or JSON strings, whitespace handling in separators, and whether tool calls inside think blocks are allowed. Declares `build_grammar_xml_tool_call` to generate GBNF grammars from tools and format, and `make_gbnf_excluding` to create grammars that accept text excluding specific forbidden strings.
Usage
Use this header when implementing or extending support for model-specific XML tool call syntaxes (such as MiniMax, GLM 4.5, etc.). The configurable format specification allows a single grammar and parsing implementation to handle multiple XML-based tool call formats.
Code Reference
Source Location
- Repository: Ggml_org_Llama_cpp
- File: common/chat-parser-xml-toolcall.h
- Lines: 1-45
Signature
struct xml_tool_call_format {
std::string scope_start;
std::string tool_start;
std::string tool_sep;
std::string key_start;
std::string key_val_sep;
std::string val_end;
std::string tool_end;
std::string scope_end;
std::optional<std::string> key_val_sep2 = std::nullopt;
std::optional<bool> raw_argval = std::nullopt;
std::optional<std::string> last_val_end = std::nullopt;
std::optional<std::string> last_tool_end = std::nullopt;
bool trim_raw_argval = false;
bool allow_toolcall_in_think = false;
};
std::string make_gbnf_excluding(std::vector<std::string> forbids);
void build_grammar_xml_tool_call(
common_chat_params & data,
const nlohmann::ordered_json & tools,
const struct xml_tool_call_format & form);
Import
#include "chat.h"
#include <nlohmann/json.hpp>
#include <optional>
#include <string>
#include <vector>
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| data | common_chat_params& | Yes | Chat parameters to populate with grammar and parsing config |
| tools | nlohmann::ordered_json | Yes | JSON array of available tool definitions with parameter schemas |
| form | xml_tool_call_format | Yes | Format specification defining the XML tag structure |
| forbids | vector<string> | Yes (for make_gbnf_excluding) | List of forbidden strings the grammar must not match |
Outputs
| Name | Type | Description |
|---|---|---|
| data.grammar | string | Generated GBNF grammar string constraining model output to valid XML tool calls |
| GBNF string | string | Grammar accepting any text except forbidden substrings (from make_gbnf_excluding) |
Usage Examples
#include "chat-parser-xml-toolcall.h"
// Configure MiniMax-style XML tool call format
xml_tool_call_format form;
form.scope_start = "<minimax:tool_call>\n";
form.tool_start = "<invoke name=\"";
form.tool_sep = "\">\n";
form.key_start = "<parameter name=\"";
form.key_val_sep = "\">";
form.val_end = "</parameter>\n";
form.tool_end = "</invoke>\n";
form.scope_end = "</minimax:tool_call>";
// Build grammar from tools and format
common_chat_params data;
build_grammar_xml_tool_call(data, tools_json, form);