| name | langchain-common-errors |
| description | Diagnose and fix common LangChain errors and exceptions.
Use when encountering LangChain errors, debugging failures,
or troubleshooting integration issues.
Trigger with phrases like "langchain error", "langchain exception",
"debug langchain", "langchain not working", "langchain troubleshoot".
|
| allowed-tools | Read, Write, Edit, Grep |
| version | 1.0.0 |
| license | MIT |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
LangChain Common Errors
Overview
Quick reference for diagnosing and resolving the most common LangChain errors.
Prerequisites
- LangChain installed and configured
- Access to application logs
- Understanding of your LangChain implementation
Error Reference
Authentication Errors
openai.AuthenticationError: Incorrect API key provided
import os
os.environ["OPENAI_API_KEY"] = "sk-..."
from langchain_openai import ChatOpenAI
llm = ChatOpenAI()
anthropic.AuthenticationError: Invalid x-api-key
os.environ["ANTHROPIC_API_KEY"] = "sk-ant-..."
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(api_key="sk-ant-...")
Import Errors
ModuleNotFoundError: No module named 'langchain_openai'
pip install langchain-openai
pip install langchain-anthropic
pip install langchain-google-genai
pip install langchain-community
ImportError: cannot import name 'ChatOpenAI' from 'langchain'
from langchain.chat_models import ChatOpenAI
from langchain_openai import ChatOpenAI
Rate Limiting
openai.RateLimitError: Rate limit reached
from langchain_openai import ChatOpenAI
from tenacity import retry, wait_exponential, stop_after_attempt
@retry(wait=wait_exponential(min=1, max=60), stop=stop_after_attempt(5))
def call_with_retry(llm, prompt):
return llm.invoke(prompt)
llm = ChatOpenAI(max_retries=3)
Output Parsing Errors
OutputParserException: Failed to parse output
from langchain.output_parsers import RetryOutputParser
parser = RetryOutputParser.from_llm(parser=your_parser, llm=llm)
from pydantic import BaseModel
class Output(BaseModel):
answer: str
llm_with_structure = llm.with_structured_output(Output)
ValidationError: field required
from pydantic import BaseModel, Field
from typing import Optional
class Output(BaseModel):
answer: str
confidence: Optional[float] = Field(default=None)
Chain Errors
ValueError: Missing required input keys
prompt = ChatPromptTemplate.from_template("Hello {name}, you are {age}")
print(prompt.input_variables)
chain.invoke({"name": "Alice", "age": 30})
TypeError: Expected mapping type as input
chain.invoke("hello")
chain.invoke({"input": "hello"})
Agent Errors
AgentExecutor: max iterations reached
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
max_iterations=20,
early_stopping_method="force"
)
ToolException: Tool execution failed
@tool
def my_tool(input: str) -> str:
"""Tool description."""
try:
return result
except Exception as e:
return f"Tool error: {str(e)}"
Memory Errors
KeyError: 'chat_history'
prompt = ChatPromptTemplate.from_messages([
MessagesPlaceholder(variable_name="chat_history"),
("human", "{input}")
])
chain.invoke({
"input": "hello",
"chat_history": []
})
Debugging Tips
Enable Verbose Mode
import langchain
langchain.debug = True
agent_executor = AgentExecutor(verbose=True)
Trace with LangSmith
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_API_KEY"] = "your-langsmith-key"
os.environ["LANGCHAIN_PROJECT"] = "my-project"
Check Version Compatibility
pip show langchain langchain-core langchain-openai
Resources
Next Steps
For complex debugging, use langchain-debug-bundle to collect evidence.