CLI Tools2026年5月13日·1 分钟阅读

json_repair — Fix Malformed LLM JSON in Python

json_repair repairs broken JSON from LLMs, logs, and APIs (missing quotes, commas, brackets) and can replace json.loads() with a safe fallback.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 94/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Cli
安装
Pip|Pipx
信任
信任等级:Established
入口
pip install json-repair
通用 CLI 安装命令
npx tokrepo install 5c8b9d40-3b3d-5043-87e4-a60e552b6127

简介

json_repair 用来修复 LLM 常见的 JSON 破损问题(多余逗号、缺引号、夹杂解释文字等),可以作为 json.loads() 的安全兜底。仓库 GitHub 已验证 4,853★,并提供快速 Python 示例。

最适合: 依赖 JSON 输出的 LLM 应用(结构化抽取/工具调用/ETL)并需要稳定兜底解析的团队

适配: Python 3.10+;pip/pipx 安装;可作为 json.load(s) 的替代

配置时间: 2–5 分钟

关键事实(已验证)

  • README 说明可作为 json.loads()/json.load() 的兜底替代。
  • README 标注 Python 3.10+,并给出 pip install json-repair
  • README 示例展示修复 tru 等破损字面量并返回可用对象。
  • GitHub:4,853 stars · 195 forks;最近更新 2026-05-12(GitHub API 验证)。

正文

把 json_repair 当作结构化流程的“安全网”:

  • 正常情况下仍然走 json.loads(),但在模型输出路径上改用 json_repair.loads() 更稳。
  • 建议同时记录原始输入与修复后的输出,方便排查与写回归测试。
  • 如果你做 schema 校验,通常先 repair,再校验,让校验器面对真正的 JSON 对象。

当你的下游是数据库写入、配置生成或 API 调用时,这种兜底能显著减少因为格式问题导致的失败。

README 原文节选(verbatim)

PyPI Python version PyPI downloads PyPI Downloads Github Sponsors GitHub Repo stars

English | 中文

json_repair

Repair malformed JSON from LLMs, APIs, logs, and user input in Python.

  • Fix missing quotes, commas, brackets, comments, stray prose, and truncated values.
  • Use it as a drop-in fallback for json.loads() or as a schema-guided repair step.
  • Install with pip install json-repair or try the live demo.

banner


Quick example

import json_repair

bad_json = '{"users":[{"name":"Ada","role":"admin",}],"ok":tru'
decoded_object = json_repair.loads(bad_json)

# {'users': [{'name': 'Ada', 'role': 'admin'}], 'ok': True}

If json_repair saves you time, star the repository so more people can find it.


Demo

If you are unsure whether this library will fix your specific problem, or simply want your JSON validated online, try one of these:

Premium sponsors


Think about sponsoring this library!

This library is free for everyone and is maintained as a side project, so if it helps your work, consider becoming a sponsor: https://github.com/sponsors/mangiucugna


Motivation

Some LLMs are a bit iffy when it comes to returning well formed JSON data, sometimes they skip a parentheses and sometimes they add some words in it, because that's what an LLM does. Luckily, the mistakes LLMs make are simple enough to be fixed without destroying the content.

FAQ

能直接替代 json.loads() 吗? 答:可以:README 说明它可作为 json.loads()/json.load() 的兜底或替代。

适合修哪些输入? 答:适合模型生成的 JSON:可能带多余逗号、缺引号、注释或夹杂自然语言。

如何安装? 答:按 README:pip install json-repair(也可用 pipx install json-repair)。

🙏

来源与感谢

Source: https://github.com/mangiucugna/json_repair > License: MIT > GitHub stars: 4,853 · forks: 195

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产