Skip to content

fix(utils): ignore braces inside strings when extracting nested json (#1401) - #1408

Closed
ExtremeNina wants to merge 1 commit into
dataease:mainfrom
ExtremeNina:fix/issue-1401
Closed

ExtremeNina wants to merge 1 commit into
dataease:mainfrom
ExtremeNina:fix/issue-1401

Conversation

@ExtremeNina

Copy link
Copy Markdown
Contributor

…(#1401)

背景与问题

当大模型生成的 SQL 字符串内部包含 { 或 }(如 LIKE '%{vip}%'、SQL 中的 JSON 模板或正则)时,原有的 extract_nested_json 因仅使用简单的括号计数栈、未识别字符串内部状态,导致栈计数失衡并返回 None,上层误报 "SQL answer is not a valid json object"。

修改内容

  1. 重构 extract_nested_json(backend/common/utils/utils.py):
    • 使用 Python 标准库 json.JSONDecoder(strict=False).raw_decode 流式解析 JSON;
    • 严格遵循 JSON 规范,天然正确处理字符串字面量、转义字符(\"、\\ 等)及多行格式;
    • 保留 orjson.loads 二次校验,确保下游解析兼容性;
    • 增加空值和非字符串类型的防御性检查。
  2. 新增单元测试(backend/tests/test_extract_nested_json.py):
    • 覆盖单侧 {、单侧 }、成对 {} 模糊匹配、转义引号、反斜杠路径、Markdown 代码块及前置干扰项等 17 项测试用例。

验证情况

  • 运行针对性测试:uv run pytest tests/test_extract_nested_json.py -v(17 passed);
  • 运行既有测试:uv run pytest tests/test_data_format.py tests/test_codex_mcp_runtime.py tests/test_connection_pool_manager.py -v(23 passed),无回归。

@ulleo

ulleo commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

此修复已由 #1415 接续并合并到 main,关闭本 PR 以避免重复合并。

原作者 ExtremeNina 的提交作者信息、提交说明及独立提交记录已保留。由于仓库禁用 merge commit,使用 rebase merge 后原提交 438a4ab7 对应 main 中的 a72ef89c;补充修复为 8bd2d6e7。

补充修复避免将非法或截断 JSON 的内部子对象/数组当作完整回答,保留说明文字、Markdown 包装、字符串内括号及转义字符的提取支持。与最新主分支组合验证的相关测试共 57 项通过,合并后的完整文件树与已验证版本一致。

@ulleo ulleo closed this Oct 26, 2026
@ExtremeNina
ExtremeNina deleted the fix/issue-1401 branch October 27, 2026 01:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants