Implementation:Ggml org Llama cpp Json Partial
| Knowledge Sources | |
|---|---|
| Domains | JSON, Parsing |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Implements partial/incomplete JSON parsing with "healing" capability, allowing truncated JSON strings from streaming LLM output to be completed and parsed.
Description
This module uses nlohmann::json's SAX interface via a custom json_error_locator to find where parsing fails in an incomplete JSON string. It maintains a stack of JSON elements (objects, keys, arrays) to track nesting depth. When a parse error is found, it inserts a healing marker and closes all open brackets/braces to produce valid JSON, preserving the marker in the output so the JSON can be re-split at that point later.
Usage
Use this module when parsing streaming or partial JSON outputs from LLM models, particularly for implementing partial tool calls in the OpenAI-compatible API format where the model has not finished generating the full JSON response.
Code Reference
Source Location
- Repository: Ggml_org_Llama_cpp
- File: common/json-partial.cpp
- Lines: 1-324
Signature
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"
#include "log.h"
#include <nlohmann/json.hpp>
#include <string>
#include <regex>
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| input | std::string | Yes | The potentially incomplete JSON string to parse |
| it | std::string::const_iterator & | Yes | Iterator-based variant: current position in JSON input |
| end | std::string::const_iterator | Yes | Iterator-based variant: end of JSON input |
| healing_marker | std::string | Yes | Marker string inserted at the point where healing occurs, for later re-splitting |
Outputs
| Name | Type | Description |
|---|---|---|
| return | bool | True if parsing (with healing) succeeded, false otherwise |
| out | common_json & | Output struct containing the parsed JSON and healing metadata |
Usage Examples
#include "json-partial.h"
// Partial JSON from streaming LLM output
std::string partial = R"({"name": "get_weather", "arguments": {"location": "San Fr)";
std::string marker = "__HEAL__";
common_json result;
if (common_json_parse(partial, marker, result)) {
// result contains healed JSON: {"name": "get_weather", "arguments": {"location": "San Fr__HEAL__"}}
// The marker indicates where the original input was truncated
}