
AttributeError: ‘str’ object has no attribute ‘get’ after the task is finished
快速结论:该报错通常出现在 AutoGen 的 UserProxyAgent 配置了 llm_config 参数但实际上不需要 LLM 回复时。优先排查用户代理是否误配置了 llm_config,应将其移除。
问题场景
用户使用 AutoGen 框架时,在 UserProxyAgent 中传入了 llm_config 参数,导致任务完成后出现 AttributeError: 'str' object has no attribute 'get'。该错误在某些简单任务完成后(如生成公司名称列表)或助理生成 Python 代码时可能会触发。
报错原文
File "C:\Users\...\openai\api_requestor.py", line 428, in handle_error_response
error_code=error_data.get("code"),
^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'get'
原因分析
可能原因:UserProxyAgent 本不需要 LLM 参与回复,但因 llm_config 被传入构造函数,导致代理尝试使用 LLM 生成回复时,OpenAI API 返回了非字典格式的错误响应(可能是字符串),使得 error_data.get("code") 调用失败。
根据 Issue 讨论,开发者确认 UserProxyAgent 应当使用 default_auto_reply 而非 LLM 回复,配置 llm_config 会引发此问题。
环境排查
- Python 版本:确认 Python 版本及所使用
pyautogen库版本。 - AutoGen 版本:检查
autogen或pyautogen包是否为最新版本(Issue 关闭时可能已有修复)。 - OpenAI 客户端版本:确认
openai库版本,部分旧版本可能导致不同的错误响应格式。
解决步骤
- 检查 UserProxyAgent 初始化代码:确认在创建
UserProxyAgent时是否传入了llm_config参数。 - 移除
llm_config参数:如果不需要 UserProxyAgent 调用 LLM,请将构造函数中的llm_config删除。 - 设置
default_auto_reply:确保在 UserProxyAgent 中设置一个非空的default_auto_reply字符串,用于在没有代码执行请求时自动回复。 - 保留
code_execution_config:如需让 UserProxyAgent 执行代码,可保留code_execution_config配置,但不要与llm_config同时使用。
验证方法
修改代码后,重新运行引发错误的任务(例如之前会导致报错的简单问答)。如果不再出现 AttributeError: 'str' object has no attribute 'get',且对话流程正常(UserProxyAgent 使用 default_auto_reply 进行回应),即表示问题已解决。



