Implementation:Ggml org Llama cpp Chat Parser Xml Toolcall
| Knowledge Sources | |
|---|---|
| Domains | Chat, Tool_Calling |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Implements XML-style tool call grammar generation and parsing for models that use XML-based function calling syntax.
Description
This file is part of the llama.cpp common chat library and provides support for models that use XML-based tool calling formats (such as MiniMax M2, GLM 4.5, and others). It generates GBNF grammars from tool definitions using the xml_tool_call_format configuration, which specifies XML tag delimiters for scope, tool invocation, parameter names, and values. The parsing side (try_consume_xml_tool_calls) walks through XML-structured output, extracting tool names and key-value argument pairs into common_chat_tool_call objects. It includes UTF-8 safe truncation utilities for handling partial streaming output.
Usage
Used automatically by the chat message parser when the active chat format requires XML-based tool calling. Not typically invoked directly by application code.
Code Reference
Source Location
- Repository: Ggml_org_Llama_cpp
- File: common/chat-parser-xml-toolcall.cpp
- Lines: 1-879
Signature
class xml_toolcall_syntax_exception : public std::runtime_error {
public:
xml_toolcall_syntax_exception(const std::string & message);
};
// Helper utilities
template<typename T> inline void sort_uniq(std::vector<T> & vec);
template<typename T> inline bool all_space(const T & str);
static size_t utf8_truncate_safe(const std::string_view s);
inline void utf8_truncate_safe_resize(std::string & s);
inline std::string_view utf8_truncate_safe_view(const std::string_view s);
// Grammar generation (builds GBNF grammars for XML tool calls)
// Parsing (extracts tool calls from XML-formatted model output)
Import
#include "chat-parser.h"
#include "chat.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| builder | common_chat_msg_parser & | Yes | Parser state tracking cursor position over the model output string |
| format | xml_tool_call_format | Yes | Configuration specifying XML tag delimiters for tool call elements |
| tools | vector<common_chat_tool> | Yes | Available tool definitions used to generate matching grammars |
Outputs
| Name | Type | Description |
|---|---|---|
| tool_calls | vector<common_chat_tool_call> | Extracted tool calls with function names and argument key-value pairs |
| grammar | string | Generated GBNF grammar string constraining model output to valid XML tool calls |
Usage Examples
// XML tool call parsing is invoked internally by the chat parser.
// When a model uses XML-style function calling, the parser delegates to this module:
// The format configuration specifies XML tags like:
// <tool_call>
// <name>get_weather</name>
// <arguments>
// <location>Paris</location>
// </arguments>
// </tool_call>
// The parser extracts:
// tool_call.name = "get_weather"
// tool_call.arguments = {"location": "Paris"}