An open API service indexing awesome lists of open source software.

https://github.com/qdxiao/llmjsonrepair

llmjsonrepair 是一个专门修复大语言模型(LLM)输出 JSON 格式错误的 Go 语言库。当 ChatGPT、Claude 等 AI 模型生 成的 JSON 因输出截断、缺少引号、括号未闭合等问题无法正常解析时,该库能智能修复这些错误。支持深度嵌套结构修复 、多 JSON 对象流处理、上下文感知解析等功能。提供 Repair() 和 Loads() 两个简洁 API,一行代码即可修复残缺 JSON 并返回格式化结果或数据结构
https://github.com/qdxiao/llmjsonrepair

go json jsonreader llm repair

Last synced: 3 months ago
JSON representation

llmjsonrepair 是一个专门修复大语言模型(LLM)输出 JSON 格式错误的 Go 语言库。当 ChatGPT、Claude 等 AI 模型生 成的 JSON 因输出截断、缺少引号、括号未闭合等问题无法正常解析时,该库能智能修复这些错误。支持深度嵌套结构修复 、多 JSON 对象流处理、上下文感知解析等功能。提供 Repair() 和 Loads() 两个简洁 API,一行代码即可修复残缺 JSON 并返回格式化结果或数据结构

Awesome Lists containing this project

README

          

# llmjsonrepair

[English](#english) | [中文](#中文)

---

## 中文

专门用于修复大模型输出 JSON 格式错误的 Go 语言库

### 简介

`llmjsonrepair` 是一个强大的 Go 语言库,专门用于修复大语言模型(LLM)输出的不完整或格式错误的 JSON 数据。当 LLM 生成的 JSON 因为各种原因(如输出截断、格式错误、缺少引号等)无法正常解析时,这个库可以智能地修复这些问题。

### 特性

- 🔧 **智能修复**: 自动修复各种 JSON 格式错误
- 🎯 **上下文感知**: 基于解析上下文智能推断缺失的结构
- 🚀 **高容错性**: 处理深度嵌套、混合格式等复杂情况
- 📦 **简单易用**: 提供简洁的 API 接口
- 🔄 **多对象支持**: 支持处理连续的 JSON 对象流
- 🧠 **LLM 友好**: 专门针对大模型输出特点设计

### 支持修复的问题类型

- ✅ 缺少结尾括号/方括号
- ✅ 对象键缺少引号
- ✅ 字符串未正确闭合
- ✅ 深度嵌套结构错误
- ✅ 混乱的引号和转义字符
- ✅ 多 JSON 对象流被截断
- ✅ 缺少分隔符(逗号、冒号)
- ✅ LLM 思考过程文本残留

### 安装

```bash
go get github.com/qdxiao/llmjsonrepair
```

### 快速开始

```go
package main

import (
"fmt"
"log"
"github.com/qdxiao/llmjsonrepair/pkg"
)

func main() {
// 示例:修复缺少结尾括号的 JSON
malformedJSON := `{"name": "John", "age": 30, "skills": ["Go", "Python"`

// 方法1: 修复并返回格式化的 JSON 字符串
repairedJSON, err := pkg.Repair(malformedJSON)
if err != nil {
log.Fatal(err)
}
fmt.Println("修复后的 JSON:")
fmt.Println(repairedJSON)

// 方法2: 修复并返回解析后的数据结构
data, err := pkg.Loads(malformedJSON)
if err != nil {
log.Fatal(err)
}
fmt.Printf("解析后的数据: %+v\n", data)
}
```

### API 文档

#### `Repair(jsonStr string) (string, error)`

修复 JSON 字符串并返回格式化的 JSON 字符串。

**参数:**
- `jsonStr`: 需要修复的 JSON 字符串

**返回:**
- `string`: 修复并格式化后的 JSON 字符串
- `error`: 错误信息(如果修复失败)

#### `Loads(jsonStr string) (interface{}, error)`

修复 JSON 字符串并返回解析后的数据结构。

**参数:**
- `jsonStr`: 需要修复的 JSON 字符串

**返回:**
- `interface{}`: 解析后的数据结构(`map[string]interface{}` 或 `[]interface{}`)
- `error`: 错误信息(如果修复失败)

### 使用示例

查看 `example/main.go` 文件获取更多详细的使用示例,包括各种错误类型的修复演示。

### 贡献

欢迎提交 Issue 和 Pull Request!

### 许可证

MIT License

---

## English

A Go library specifically designed to repair malformed JSON output from Large Language Models (LLMs)

### Introduction

`llmjsonrepair` is a powerful Go library designed to fix incomplete or malformed JSON data output by Large Language Models (LLMs). When JSON generated by LLMs cannot be parsed normally due to various reasons (such as output truncation, format errors, missing quotes, etc.), this library can intelligently repair these issues.

### Features

- 🔧 **Smart Repair**: Automatically fixes various JSON format errors
- 🎯 **Context Aware**: Intelligently infers missing structures based on parsing context
- 🚀 **High Fault Tolerance**: Handles complex situations like deep nesting and mixed formats
- 📦 **Easy to Use**: Provides clean API interfaces
- 🔄 **Multi-Object Support**: Supports processing continuous JSON object streams
- 🧠 **LLM Friendly**: Specifically designed for LLM output characteristics

### Supported Error Types

- ✅ Missing closing brackets/braces
- ✅ Object keys without quotes
- ✅ Unclosed strings
- ✅ Deep nested structure errors
- ✅ Messy quotes and escape characters
- ✅ Truncated multi-JSON object streams
- ✅ Missing separators (commas, colons)
- ✅ Residual LLM reasoning text

### Installation

```bash
go get github.com/qdxiao/llmjsonrepair
```

### Quick Start

```go
package main

import (
"fmt"
"log"
"github.com/qdxiao/llmjsonrepair/pkg"
)

func main() {
// Example: Fix JSON with missing closing bracket
malformedJSON := `{"name": "John", "age": 30, "skills": ["Go", "Python"`

// Method 1: Repair and return formatted JSON string
repairedJSON, err := pkg.Repair(malformedJSON)
if err != nil {
log.Fatal(err)
}
fmt.Println("Repaired JSON:")
fmt.Println(repairedJSON)

// Method 2: Repair and return parsed data structure
data, err := pkg.Loads(malformedJSON)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Parsed data: %+v\n", data)
}
```

### API Documentation

#### `Repair(jsonStr string) (string, error)`

Repairs a JSON string and returns a formatted JSON string.

**Parameters:**
- `jsonStr`: The JSON string to be repaired

**Returns:**
- `string`: The repaired and formatted JSON string
- `error`: Error information (if repair fails)

#### `Loads(jsonStr string) (interface{}, error)`

Repairs a JSON string and returns the parsed data structure.

**Parameters:**
- `jsonStr`: The JSON string to be repaired

**Returns:**
- `interface{}`: The parsed data structure (`map[string]interface{}` or `[]interface{}`)
- `error`: Error information (if repair fails)

### Examples

See the `example/main.go` file for more detailed usage examples, including demonstrations of repairing various error types.

### Contributing

Issues and Pull Requests are welcome!

### License

MIT License