google-research / google-research/agentic-visualization

TypeError: write() argument must be str, not dict — DebugAgent 在写入生成/修复代码到文件时崩溃

Open
#1 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
34
Forks
4
PR merge metrics
No merged PRs in 30d

Description

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

## 概要

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

```
Failed to write code to file: write() argument must be str, not dict
```

该错误导致生成/执行阶段中断,未能生成图片或保存修复后的代码。

## 复现步骤

1. 运行项目中的工作流或基准(示例命令):

```bash
python run_benchmark.py --start 57 --end 57 --model-name openai/gpt-4o --search-model-name openai/gpt-4o
```

2. 查看对应输出目录下的 `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,尝试提取常见字符串字段;只有在確認為字符串时才写文件。

这是一种保守的防护性改动,目的是避免类型错误导致的崩溃,并提供更明确的错误日志以便进一步排查。

[debug_agent.py](https://github.com/user-attachments/files/27522213/debug_agent.py)

[orchestrator.py](https://github.com/user-attachments/files/27522218/orchestrator.py)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.