Skip to content

feat (sql-executor): 实现 SQL 自纠错循环,执行失败后自动重试生成 - #229

Open
Sully-y wants to merge 1 commit into
apconw:masterfrom
Sully-y:feat/text2sql-graph
Open

feat (sql-executor): 实现 SQL 自纠错循环,执行失败后自动重试生成#229
Sully-y wants to merge 1 commit into
apconw:masterfrom
Sully-y:feat/text2sql-graph

Conversation

@Sully-y

@Sully-y Sully-y commented Jul 29, 2026

Copy link
Copy Markdown

概述

实现 SQL 执行失败后的自动恢复机制。当 sql_executor 执行失败时,将具体错误信息回填至生成提示词,驱动 sql_generator 重新生成 SQL,并支持最多 3 次重试。解决执行异常被静默吞掉、attempts / correct_attempts 字段闲置、{error_msg} 占位符从未填充以及无条件边导致的流程错误传播等问题。

Related Issues
Closes #227

具体改动

1. 异常分类
在 execute_sql 中细化异常捕获,区分 可重试错误(如语法错误、连接超时、表不存在)与 不可重试错误(如权限不足、资源不存在)。
只有可重试错误才触发重试逻辑,不可重试错误直接终止并返回明确错误信息。

2. 激活 attempts 计数
在流程状态中启用 attempts 字段,初始值 0,每次执行失败并决定重试前自增。
重试条件:attempts < 3 且错误类型为可重试。

3. 失败时回填 error_msg
在重试分支中,将捕获的异常对象转换为描述字符串,并填入 prompt 模板的 {error_msg} 占位符。

更新相关 prompt 模板,确保 {error_msg} 在重试时有效渲染,为模型提供具体失败上下文。

4. 条件路由改造
将 sql_executor 后的无条件边替换为条件路由,逻辑如下:
成功 → 输出结果节点。
失败 & attempts < 3 → 回填 error_msg,返回 sql_generator 节点重新生成 SQL。
失败 & attempts ≥ 3(或不可重试) → 终止流程,返回最终错误信息。

关于测试

自动化测试,新增测试文件 tests/test_aimodel_service.py(随附件),覆盖核心场景,所有测试均已通过。
image

☑ 所有单元测试均已通过
☑ 代码已通过 linter 检查
☑ 新增异常分支均有对应测试

附件清单:

pytest代码,测试截图与自动测试日志。

tests.zip

@Sully-y

Sully-y commented Aug 5, 2026

Copy link
Copy Markdown
Author

你好,只是友好地提醒一下这个PR。您有空的时候能看一下吗?谢谢! @apconw

@apconw

apconw commented Aug 10, 2026

Copy link
Copy Markdown
Owner

收到 最近比较忙 后面会统一处理

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.

[Feature] 实现 SQL 自纠错循环(Self-Correction Loop):执行失败后自动重试生成

2 participants