Debug Fix beginner · 3 min read

How to fix LangChain deprecated import langchain.agents

Quick answer
The langchain.agents module import is deprecated in LangChain v0.2+. Replace it by importing agents from langchain.agents.agent_toolkits or other specific submodules as per the new structure. Update your code to use the new import paths to avoid import errors.
ERROR TYPE code_error
⚡ QUICK FIX
Replace from langchain.agents import ... with imports from langchain.agents.agent_toolkits or other updated submodules.

Why this happens

LangChain reorganized its package structure in v0.2+ to improve modularity and clarity. The langchain.agents module was deprecated and split into more specific submodules. Code that still uses from langchain.agents import AgentExecutor or similar will raise import errors or warnings.

Example deprecated import:

python
from langchain.agents import AgentExecutor, Tool

agent = AgentExecutor(...)
output
ImportError: cannot import name 'AgentExecutor' from 'langchain.agents'

The fix

Update your imports to the new LangChain v0.2+ structure. For example, import AgentExecutor from langchain.agents.agent or import tools from langchain.agents.agent_toolkits. This aligns with the new modular design and resolves import errors.

Corrected example:

python
from langchain.agents.agent import AgentExecutor
from langchain.agents.agent_toolkits import Tool

agent = AgentExecutor(...)
output
No import errors; agent initialized successfully

Preventing it in production

Pin your LangChain version in requirements.txt or pyproject.toml to a stable release and monitor LangChain's changelog for breaking changes. Use automated tests to catch import errors early. Consider adding retry or fallback logic if your app dynamically loads modules.

Key Takeaways

  • LangChain v0.2+ deprecated the langchain.agents import; use new submodules instead.
  • Update imports to langchain.agents.agent and langchain.agents.agent_toolkits to fix errors.
  • Pin LangChain versions and monitor changelogs to avoid breaking changes in production.
Verified 2026-04
Verify ↗