LangChain's pandas DataFrame agent will not import
Your error
ImportError: cannot import name 'create_pandas_dataframe_agent' from 'langchain.agents'2 more lines it can showFix it fast
Most common cause, 1 of 2The function is imported from langchain.agents or langchain_community
Install langchain-experimental and import from it.
python -m pip install -U "langchain-experimental==0.4.2" langchain-openai pandas
import pandas as pd
from langchain_experimental.agents import create_pandas_dataframe_agent
from langchain_openai import ChatOpenAI # or another LangChain chat model
df = pd.read_csv("sales.csv")
agent = create_pandas_dataframe_agent(
ChatOpenAI(model="gpt-5.5"),
df,
agent_type="tool-calling",
allow_dangerous_code=True, # runs model-written Python: sandbox only
)
print(agent.invoke({"input": "How many rows does the table have?"}))Short answer
ImportError: cannot import name 'create_pandas_dataframe_agent' means the code asks langchain.agents or langchain_community for it, and neither has it: the function lives only in langchain_experimental.agents. If that import fails on AgentType instead, langchain-experimental is older than 0.4 and expects pre-1.0 LangChain. Install langchain-experimental 0.4.2. Applies to langchain 1.5 with langchain-core 1.8.
Which cause do you have?
List the LangChain packages installed in the interpreter that runs the script. A missing langchain-experimental points to the first cause; a 0.3.x version next to langchain 1.x points to the second.
python -m pip show langchain langchain-core langchain-experimental langchain-classicThen find the line you see in this list and open its fix:
- 1The function is imported from langchain.agents or langchain_community
The ImportError names langchain.agents or langchain_community.agent_toolkits as the module, and langchain-experimental is missing from the pip show output - 2langchain-experimental 0.3.x is installed next to LangChain 1.x
pip show lists langchain-experimental 0.3.x with langchain 1.x, and the traceback ends in langchain_experimental/agents/agent_toolkits/pandas/base.py with cannot import name 'AgentType' from 'langchain.agents'
What does the error look like?
ImportError: cannot import name 'create_pandas_dataframe_agent' from 'langchain.agents'ImportError: cannot import name 'create_pandas_dataframe_agent' from 'langchain_community.agent_toolkits'ImportError: cannot import name 'AgentType' from 'langchain.agents'
Cause 1 of 2
The function is imported from langchain.agents or langchain_community
You seeThe ImportError names langchain.agents or langchain_community.agent_toolkits as the module, and langchain-experimental is missing from the pip show output
Check
In LangChain 1.x, langchain.agents exports only create_agent and AgentState, and the agent_toolkits module of langchain-community has no entry for the pandas agent, so both imports fail. create_pandas_dataframe_agent is exported by langchain_experimental.agents, a separate package that pip install langchain does not bring in. Old tutorials that import it from the main package no longer work.
grep -rn "create_pandas_dataframe_agent" --include=*.py --include=*.ipynb .Fix
Install langchain-experimental and import from it. The agent then refuses to start unless you pass allow_dangerous_code=True, because its Python tool executes code the model writes, so build it only where that code cannot harm anything.
python -m pip install -U "langchain-experimental==0.4.2" langchain-openai pandas
import pandas as pd
from langchain_experimental.agents import create_pandas_dataframe_agent
from langchain_openai import ChatOpenAI # or another LangChain chat model
df = pd.read_csv("sales.csv")
agent = create_pandas_dataframe_agent(
ChatOpenAI(model="gpt-5.5"),
df,
agent_type="tool-calling",
allow_dangerous_code=True, # runs model-written Python: sandbox only
)
print(agent.invoke({"input": "How many rows does the table have?"}))Verify
The import line runs without error, and the agent returns an answer about the DataFrame.
python -c "from langchain_experimental.agents import create_pandas_dataframe_agent; print('ok')"Confirmed by raw.githubusercontent.com
Cause 2 of 2
langchain-experimental 0.3.x is installed next to LangChain 1.x
You seepip show lists langchain-experimental 0.3.x with langchain 1.x, and the traceback ends in langchain_experimental/agents/agent_toolkits/pandas/base.py with cannot import name 'AgentType' from 'langchain.agents'
Check
The 0.3 line of the pandas agent imports AgentType, AgentExecutor and the older agent constructors from langchain.agents, which LangChain 1.0 moved out of the main package. Release 0.4.2 imports the same classes from langchain_classic and depends on langchain-community 0.4.2, which in turn requires langchain-classic, so upgrading brings the right modules along.
python -m pip show langchain-experimental | grep -i ^versionFix
Upgrade langchain-experimental to 0.4.2 in the same interpreter. It needs Python 3.10 or newer and langchain-core 1.4 or newer, and pip resolves those for you.
python -m pip install -U "langchain-experimental==0.4.2"Verify
pip show now reports 0.4.2 and lists langchain-classic as installed, and the import from langchain_experimental.agents succeeds.
python -m pip show langchain-experimental langchain-classic | grep -i -E "^(name|version)"
python -c "from langchain_experimental.agents import create_pandas_dataframe_agent"Confirmed by raw.githubusercontent.com
How do I stop it coming back?
- Pin langchain-experimental==0.4.2 together with langchain and langchain-core in requirements.txt; the project was sunset in May 2026 and its repository is read-only, so no later release will fix a new break.
- Plan a replacement for the pandas agent: LangChain's sunset notice says its Python REPL tool predates the safer interpreter and sandbox environments it now recommends.
- Run any agent built with allow_dangerous_code=True in an isolated container with no secrets and no network access it does not need.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.