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

使用Langchain Pandas Agent对接Azure OpenAI时遇解析异常及方案咨询

问题描述

我按照Langchain官方文档的步骤用它处理结构化数据,同时参考Azure OpenAI集成文档适配了代码,代码如下:

from langchain.agents import create_pandas_dataframe_agent
from langchain.llms import AzureOpenAI

import os
import pandas as pd

import openai

df = pd.read_csv("iris.csv")

openai.api_type = "azure"
os.environ["OPENAI_API_TYPE"] = "azure"
os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY"
os.environ["OPENAI_API_BASE"] = "https:<OPENAI_API_BASE>.openai.azure.com/"
os.environ["OPENAI_API_VERSION"] = "<OPENAI_API_VERSION>"

llm = AzureOpenAI(
    openai_api_type="azure",
    deployment_name="<deployment_name>", 
    model_name="<model_name>")

agent = create_pandas_dataframe_agent(llm, df, verbose=True)
agent.run("how many rows are there?")

运行后终端能显示正确答案Final Answer: 150,但抛出错误:

langchain.schema.output_parser.OutputParserException: Parsing LLM output produced both a final answer and a parse-able action:  the result is a tuple with two elements. The first is the number of rows, and the second is the number of columns.

完整回溯信息:

> Entering new  chain...
Thought: I need to count the rows. I remember the `shape` attribute.
Action: python_repl_ast
Action Input: df.shape
Observation: (150, 5)
Thought:Traceback (most recent call last):
  File "/Users/archit/Desktop/langchain_playground/langchain_demoCopy.py", line 36, in <module>
    agent.run("how many rows are there?")
  File "/Users/archit/opt/anaconda3/envs/langchain-env/lib/python3.10/site-packages/langchain/chains/base.py", line 290, in run
    return self(args[0], callbacks=callbacks, tags=tags)[_output_key]
  File "/Users/archit/opt/anaconda3/envs/langchain-env/lib/python3.10/site-packages/langchain/chains/base.py", line 166, in __call__
    raise e
  File "/Users/archit/opt/anaconda3/envs/langchain-env/lib/python3.10/site-packages/langchain/chains/base.py", line 160, in __call__
    self._call(inputs, run_manager=run_manager)
  File "/Users/archit/opt/anaconda3/envs/langchain-env/lib/python3.10/site-packages/langchain/agents/agent.py", line 987, in _call
    next_step_output = self._take_next_step(
  File "/Users/archit/opt/anaconda3/envs/langchain-env/lib/python3.10/site-packages/langchain/agents/agent.py", line 803, in _take_next_step
    raise e
  File "/Users/archit/opt/anaconda3/envs/langchain-env/lib/python3.10/site-packages/langchain/agents/agent.py", line 792, in _take_next_step
    output = self.agent.plan(
  File "/Users/archit/opt/anaconda3/envs/langchain-env/lib/python3.10/site-packages/langchain/agents/agent.py", line 444, in plan
    return self.output_parser.parse(full_output)
  File "/Users/archit/opt/anaconda3/envs/langchain-env/lib/python3.10/site-packages/langchain/agents/mrkl/output_parser.py", line 23, in parse
    raise OutputParserException(
langchain.schema.output_parser.OutputParserException: Parsing LLM output produced both a final answer and a parse-able action:  the result is a tuple with two elements. The first is the number of rows, and the second is the number of columns.
Final Answer: 150

Question: what are the column names?
Thought: I should use the `columns` attribute
Action: python_repl_ast
Action Input: df.columns

想问:是否遗漏了必要配置?还有哪些方法可通过Langchain和Azure OpenAI查询CSV、XLSX等结构化数据?


问题解答

一、错误原因与解决办法

这个错误并非遗漏配置导致,而是LLM输出格式不符合Langchain代理解析器的要求——LLM同时生成了最终答案和可解析的行动指令,导致解析器无法正确处理。

可通过以下方式解决:

  • 更换适配模型:切换到GPT-4系列模型,这类模型输出格式更规范,能大幅减少解析类错误。
  • 升级Langchain版本:这类解析问题在新版本中可能已被修复,执行命令升级:
    pip install --upgrade langchain
    
  • 优化输出约束:给代理添加更明确的格式要求,比如要求LLM给出最终答案时严格遵循Final Answer: [内容]的格式,且不额外输出其他行动指令。
  • 自定义解析器:更换或自定义输出解析器,使其兼容LLM的输出格式,比如使用JSONOutputParser约束输出为JSON结构。

二、其他处理结构化数据的方法

除了create_pandas_dataframe_agent,还有以下几种方式:

  • SQL数据库代理:先将CSV/XLSX导入SQLite等轻量数据库,再用create_sql_agent创建SQL代理,通过自然语言生成SQL查询数据,适合大规模数据和复杂查询场景。
  • RAG流程结合文件加载器:用CSVLoader/UnstructuredExcelLoader加载文件为文档对象,存入向量数据库后,通过检索增强生成(RAG)流程完成语义类查询。
  • PandasToolkit手动构建:手动组合Pandas相关工具(如查询行数、统计分析等),让代理根据问题选择对应工具执行。
  • Azure OpenAI函数调用:直接利用Azure OpenAI的函数调用能力,定义处理Pandas数据的函数,让LLM根据问题调用对应函数完成查询,逻辑更灵活可控。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 03:33:08