You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Aider Coder AI添加超3个文件?遇AttributeError报错求助

如何在Aider Coder中添加超过3个文件?

问题重现

当使用Aider Coder时,若传入的fnames列表长度超过3,会触发以下错误:

from aider.coder import Coder

fnames=["file2.py","file2.py","file3.py","file4.py"]
coder = Coder.create(main_model=model, fnames=fnames)

coder.run("some prompt")

报错信息:

File "path\my_venv\Lib\site-packages\aider\coders\base_coder.py", line 1035, in get_rel_fname
    return os.path.relpath(fname, self.root)
                                  ^^^^^^^^^
AttributeError: 'EditBlockCoder' object has no attribute 'root'

当fnames长度≤3时可正常运行。

解决方案

方案1:显式指定root参数

创建Coder实例时手动传入root参数,指定项目根目录(比如当前工作目录"./"),确保EditBlockCoder能正确获取路径处理所需的root属性:

from aider.coder import Coder

fnames=["file2.py","file2.py","file3.py","file4.py"]
# 指定root为当前目录
coder = Coder.create(main_model=model, fnames=fnames, root="./")

coder.run("some prompt")

方案2:强制使用全文件模式

Aider默认在文件数超过3时自动切换到EditBlockCoder,你可以通过coder_type="full"强制使用FullMessageCoder,绕过路径处理的问题:

from aider.coder import Coder

fnames=["file2.py","file2.py","file3.py","file4.py"]
coder = Coder.create(main_model=model, fnames=fnames, coder_type="full")

coder.run("some prompt")

原因说明

Aider的默认逻辑是:当文件数≤3时使用FullMessageCoder,直接将全部文件内容传入模型;当文件数超过3时,自动切换为EditBlockCoder,该模式需要依赖root属性来处理文件相对路径,但自动初始化时可能未正确设置该属性,导致报错。通过手动指定root或强制使用全文件模式,即可解决该问题。

内容的提问来源于stack exchange,提问作者koushtubh vikramaditya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 07:43:16