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

Agent因迭代/时间限制停止且不执行Pandas查询求助

解决LangChain CSV Agent「Agent stopped due to iteration limit or time limit」错误

问题描述

使用LangChain的CSV Agent时,触发错误「Agent stopped due to iteration limit or time limit」。Agent能够生成正确的Pandas查询语句,但无法完成该查询的执行流程,且该问题在Ollama、Groq、OpenAI等多模型提供商环境中均出现,推测与Agent执行逻辑相关。

解决方案

1. 调整迭代次数与超时阈值

当前设置的max_iterations=10可能不足以覆盖查询执行的完整步骤,尝试调高迭代次数并添加超时时间参数,避免因资源限制中断执行:

agent = create_csv_agent(
    llm,
    csv_file,
    verbose=True,
    handle_parsing_errors=True,
    allow_dangerous_code=True,
    max_iterations=20,  # 提升迭代次数
    max_execution_time=60  # 设置60秒超时上限
)

2. 显式指定Agent类型

CSV Agent推荐使用zero-shot-react-description类型,显式声明该参数可明确执行逻辑,避免默认逻辑的不确定性:

agent = create_csv_agent(
    llm,
    csv_file,
    verbose=True,
    agent_type="zero-shot-react-description",  # 启用官方推荐的Agent类型
    handle_parsing_errors=True,
    allow_dangerous_code=True,
    max_iterations=20,
    max_execution_time=60
)

3. 简化回调与错误处理

暂时移除StreamingStdOutCallbackHandler,验证是否因实时输出干扰了Agent的执行流程;同时优化错误捕获逻辑,获取更详细的执行日志:

# 初始化LLM时移除回调
llm = OllamaLLM(
    model="llama3.1",
    temperature=0
)

# 优化错误处理
def query_data(query):
    try:
        response = agent.invoke(query)
        return response
    except Exception as e:
        print(f"执行错误详情: {str(e)}")
        # 打印Agent的中间思考步骤(需开启verbose=True)
        raise

4. 独立验证查询语句有效性

利用verbose=True的日志输出捕获Agent生成的Pandas查询语句,直接在Pandas中执行,确认数据本身无异常:

# 示例:从Agent日志中提取的查询语句
result = df[df['bike_type'] == 'hybrid']['sales'].count()
print("直接执行结果:", result)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 13:14:55