Langchain dataframe agent fails to execute pandas operations

I’m having trouble with langchain’s dataframe agent in Python. It keeps getting stuck in an endless loop because it can’t properly execute pandas operations. The agent seems to understand what it needs to do but fails to use the correct tools.

Here’s my setup:

import os
os.environ['OPENAI_API_KEY'] = 'your-key-here'
from langchain.agents import create_pandas_dataframe_agent
from langchain.chat_models import ChatOpenAI
import pandas as pd

# Load data
data = pd.read_csv('customer_data.csv')
model = ChatOpenAI(temperature=0.0, model_name='gpt-3.5-turbo')
agent = create_pandas_dataframe_agent(model, data, verbose=True)
result = agent.run("calculate average revenue")

The agent output shows:

Thought: I need to compute the average revenue from the data
Action: Perform revenue calculation with pandas
Action Input: data['Revenue'].mean()
Observation: Perform revenue calculation with pandas is not a valid tool, try another one

It keeps trying different approaches but never actually executes the code using python_repl_ast tool. I tried modifying the tool description but that didn’t work either. The dataframe should be available in the tool’s scope. What am I missing here?

Had this exact problem last month. The dataframe agent tool selection is completely broken - keeps making up fake tools instead of using real ones.

Tried everything: downgrading versions, different imports, manual tool passing. None of it worked consistently. Agent would handle basic stuff fine, then completely fail on anything complex.

Found a way better solution though. Ditched langchain agents entirely and moved to Latenode workflows for pandas operations.

With Latenode, I just upload my CSV once and build a workflow with the exact pandas code I need. No tool confusion, no fake function names. Just reliable processing that actually works.

Built one workflow that handles average revenue, median, percentiles - whatever metrics I want. Runs perfectly every time without getting stuck in loops or inventing nonexistent tools.

So much better than debugging agent tool mappings.

Had this exact headache 6 months ago during a project migration. Langchain’s agent gets confused about which tools are available and starts making up tool names.

This fixed it for me - explicitly pass the tools parameter when creating the agent:

from langchain.agents.agent_toolkits import create_python_agent
from langchain.tools import PythonREPLTool

python_repl = PythonREPLTool()
tools = [python_repl]

agent = create_python_agent(
    llm=model,
    tool=python_repl,
    verbose=True,
    agent_type=AgentType.OPENAI_FUNCTIONS
)

Then inject your dataframe into the execution context:

result = agent.run(f"""import pandas as pd
data = {data.to_dict()}
df = pd.DataFrame(data)
print(df['Revenue'].mean())""")

Basically skip the dataframe agent entirely and use the python agent directly. Less magic, more control over which tools get used.

Or just ditch the agent approach and call pandas directly. Sometimes the simple solution beats fighting with tool selection logic.

Hit this nightmare multiple times. The dataframe agent has a bug where it makes up tool names instead of using python_repl_ast. Your verbose output shows the classic signs - it gets what you want but invents fake tools.

What fixed it for me: downgrade to langchain 0.0.240 for now. Newer versions broke the tool mapping. Also make sure you’re importing from langchain_experimental.agents.agent_toolkits not the regular agents import.

Set max_iterations=3 to stop infinite loops while you debug. Agent gives up after 3 tries instead of running forever. If you’re still stuck, add handle_parsing_errors=True when creating the agent. Sometimes tool selection fails because it can’t parse the dataframe schema properly.

same thing happened to me last week! gpt-3.5-turbo keeps making up tool names instead of using python_repl_ast. try switching to gpt-4 or add return_intermediate_steps=True to see what’s going on. also make sure ur running the latest dataframe agent - older versions had messed up tool mappings.

I’ve hit this exact problem - it’s super frustrating with langchain’s dataframe agent. The agent gets confused about available tools and keeps trying to use ones that don’t exist.

Langchain’s tool selection just isn’t reliable. Even with python_repl_ast available, the agent makes up tool names like “Perform revenue calculation with pandas” instead of using what’s actually there.

I used to waste hours debugging these loops until I found a better way. Instead of fighting langchain’s wonky agent behavior, I switched to Latenode for automating data analysis tasks.

Latenode lets you build workflows that read CSVs, run pandas operations reliably, and return results without the tool confusion. You get consistent execution because you’re not relying on an LLM to guess which tools to use.

I built a customer revenue analysis automation that runs pandas operations on schedule and never gets stuck. Way more predictable than wrestling with langchain agents.

I’ve been fighting this same issue for months in prod. The problem is langchain’s tool registration - the dataframe agent doesn’t properly register python_repl_ast, so it just makes up tool names.

Here’s what fixed it for me: import PythonAstREPLTool directly and pass it through the available_tools parameter when you create the agent. Also set agent_executor_kwargs={‘handle_parsing_errors’: True, ‘max_execution_time’: 60} to stop those infinite loops.

One more thing - check your pandas version. Langchain breaks with newer pandas versions because of deprecated methods. I’m using pandas 1.5.3 with langchain 0.0.354 and it’s solid now. The dataframe agent depends on specific pandas internals that changed recently, so even correct syntax fails.