Implementation:Ggml org Llama cpp Json Partial Header
| Knowledge Sources | |
|---|---|
| Domains | JSON, Parsing |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Declares the interface for partial JSON parsing with healing support, enabling incomplete JSON from streaming model outputs to be completed and parsed.
Description
This header defines `common_healing_marker` containing a raw marker and a `json_dump_marker` for locating the healing point in serialized output, and `common_json` wrapping an `nlohmann::ordered_json` plus the healing marker. It exposes two overloads of `common_json_parse`: one taking a string and one taking iterators, both accepting a `healing_marker` string to control how partial JSON is completed. When a healing marker is provided, the parser completes the partial JSON by adding the marker and whatever is needed to close the JSON structure.
Usage
Use this header when handling streaming model outputs that produce incomplete JSON. It is used throughout the server and chat infrastructure to parse partial JSON from tool calls and structured outputs in OAI-compatible format.
Code Reference
Source Location
- Repository: Ggml_org_Llama_cpp
- File: common/json-partial.h
- Lines: 1-39
Signature
struct common_healing_marker {
std::string marker;
std::string json_dump_marker;
};
struct common_json {
nlohmann::ordered_json json;
common_healing_marker healing_marker;
};
bool common_json_parse(
const std::string & input,
const std::string & healing_marker,
common_json & out);
bool common_json_parse(
std::string::const_iterator & it,
const std::string::const_iterator & end,
const std::string & healing_marker,
common_json & out);
Import
#include "json-partial.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| input | const std::string & | Yes | JSON string to parse (may be incomplete) |
| healing_marker | const std::string & | Yes | Marker string for healing partial JSON; empty string disables healing |
| it | std::string::const_iterator & | Yes | Iterator to current position in the JSON string (advanced on success) |
| end | const std::string::const_iterator & | Yes | End iterator for the JSON string |
Outputs
| Name | Type | Description |
|---|---|---|
| return value | bool | True if parsing (with optional healing) succeeded |
| out | common_json & | Parsed JSON object with healing marker information |
| out.json | nlohmann::ordered_json | The parsed (and possibly healed) JSON value |
| out.healing_marker | common_healing_marker | Contains the marker and json_dump_marker for locating the healing point |
Usage Examples
#include "json-partial.h"
// Parse a complete JSON string (no healing)
common_json result;
bool ok = common_json_parse("{\"key\": \"value\"}", "", result);
// ok == true, result.json == {"key": "value"}
// Parse a partial JSON string with healing
common_json partial_result;
bool healed = common_json_parse("{\"name\": \"Al", "foo", partial_result);
// healed == true, partial_result.json is a complete JSON object
// partial_result.healing_marker.json_dump_marker can locate the healing point