Skip to content

Fix TypeError when writing generated/fixed code to file (DebugAgent)#2

Open
token2everything wants to merge 1 commit into
google-research:mainfrom
token2everything:main
Open

Fix TypeError when writing generated/fixed code to file (DebugAgent)#2
token2everything wants to merge 1 commit into
google-research:mainfrom
token2everything:main

Conversation

@token2everything

Copy link
Copy Markdown

概要
在运行工作流时,发现大量失败 case 包含相同错误:

Failed to write code to file: write() argument must be str, not dict
该错误导致生成/执行阶段中断,未能生成图片或保存修复后的代码。

复现步骤
运行项目中的工作流或基准(示例命令):
python run_benchmark.py --start 57 --end 57 --model-name openai/gpt-4o --search-model-name openai/gpt-4o
查看对应输出目录下的 workflow_result.json,在 iterations 的 debug_result.stderr 中会出现上述 TypeError。
或在本地触发 debug 流程,使 debug_and_fix 返回 dict 并被上层直接写入文件时也会复现。

根因分析
debug_and_fix(或其他 agent)在某些路径返回 dict(例如包含 fixed_code 與 execution_result),而调用方假定返回的是字符串代码,並直接执行 f.write(...)。
当传入 dict 给 f.write() 时,会抛出 TypeError: write() argument must be str, not dict,导致整个迭代失败并中断后续处理。
我已提交的最小修复(用于快速消除崩溃)
修改点:

coda/coda/agents/debug_agent.py

在 debug_code_if_needed 中:兼容 debug_and_fix 返回 dict,优先取 execution_result(若已有则直接返回),否则提取 fixed_code 并仅在它為 str 時才重新执行。
在 run_python_code 写文件前:若 code_text 不是字符串,则尝试从常见键(fixed_code, generated_code, code, code_text) 提取;若仍然不是 str,則返回明确的 ExecutionResult(stderr 指示类型错误),避免抛异常。
coda/coda/workflow/orchestrator.py

在保存迭代结果 _save_iteration_result 时,写入 fixed_code.py 之前加入类型检查:若 iteration.debug_result.fixed_code 为 dict,尝试提取常见字符串字段;只有在確認為字符串时才写文件。
这是一种保守的防护性改动,目的是避免类型错误导致的崩溃,并提供更明确的错误日志以便进一步排查。

@google-cla

google-cla Bot commented Jun 12, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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.

1 participant