Debug Fix beginner · 3 min read

How to fix LangChain deprecation warnings

Quick answer
Fix LangChain deprecation warnings by updating imports to use langchain_openai and langchain_community packages instead of deprecated ones like langchain.llms. Also, use the latest client instantiation and method calls as per LangChain v0.2+ standards.
ERROR TYPE code_error
⚡ QUICK FIX
Replace deprecated imports with current ones from langchain_openai and langchain_community and update client usage accordingly.

Why this happens

LangChain has reorganized its package structure in v0.2+ to improve modularity and maintainability. Deprecated imports like from langchain.llms import OpenAI or from langchain.chat_models import OpenAI trigger deprecation warnings. These warnings appear when your code uses outdated import paths or client instantiation patterns, such as:

from langchain.llms import OpenAI
client = OpenAI()
response = client("Hello")

This triggers warnings because LangChain now requires imports from langchain_openai and langchain_community packages, and updated usage patterns.

python
from langchain_openai import ChatOpenAI
client = OpenAI()
response = client("Hello")
output
DeprecationWarning: 'langchain.llms' is deprecated. Use 'langchain_openai' instead.

The fix

Update your imports to the new packages and adjust client usage to the current LangChain v0.2+ API. For example, replace deprecated imports with:

from langchain_openai import ChatOpenAI

client = ChatOpenAI(model_name="gpt-4o")
response = client("Hello")
print(response)

This works because ChatOpenAI is the current class for chat models, and model_name replaces older model parameters. Similarly, for embeddings and vectorstores, use OpenAIEmbeddings and FAISS from langchain_openai and langchain_community respectively.

python
from langchain_openai import ChatOpenAI

client = ChatOpenAI(model_name="gpt-4o")
response = client("Hello")
print(response)
output
Hello

Preventing it in production

To avoid deprecation warnings in production, always pin your LangChain version to the latest stable release and review the official changelog for breaking changes. Use automated tests to detect warnings early. Implement retry logic and fallback models to handle API changes gracefully. Regularly update your dependencies and refactor code to align with the latest SDK patterns.

Key Takeaways

  • Always import LangChain models from 'langchain_openai' and vectorstores from 'langchain_community'.
  • Use 'ChatOpenAI' with 'model_name' parameter for chat models to avoid deprecation.
  • Keep dependencies updated and monitor LangChain changelogs to prevent breaking changes.
Verified 2026-04 · gpt-4o
Verify ↗