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

报错ValueError:协程对象不可迭代及vars参数异常问题咨询

问题排查与解决

错误根源

代码里的response.text是协程对象,不是直接可用的字符串:

  • 直接对协程对象调用replace()方法,会触发TypeError('协程对象不可迭代')
  • 若后续代码尝试将这个协程对象作为vars()的参数(或其他需要具备__dict__属性的场景),会触发第二个TypeError

修复后的代码

async def answer_question(QUERY, answer):
    async with aiohttp.ClientSession() as session:
        response = await session.post('http://127.0.0.1:5009/answer', json={
            "question": QUERY,
            "passage": answer
        })
        # 先await获取响应文本字符串,再处理替换
        text_content = await response.text()
        return text_content.replace("\"","")

关键说明

  • response.text()是异步方法,必须用await等待它执行完成,才能拿到实际的字符串内容
  • 原代码跳过await直接操作协程对象,是引发错误的核心原因

内容的提问来源于stack exchange,提问作者Prasun Kumar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 19:20:19