Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Langchain ai Langchain Langchain Classic Init

From Leeroopedia
Knowledge Sources
Domains LLM Framework, Python Packaging, Deprecation Management
Last Updated 2026-02-11 00:00 GMT

Overview

The langchain_classic/__init__.py module serves as the main entrypoint for the legacy langchain-classic package, providing backward-compatible lazy imports with deprecation warnings for classes that have been relocated to other packages.

Description

This file is the package initialization module for langchain_classic, the legacy LangChain package that is no longer receiving new features. Its primary role is to act as a compatibility shim: when users import classes from the top-level langchain_classic namespace (e.g., from langchain_classic import OpenAI), the module intercepts these attribute accesses via __getattr__, imports the class from its current canonical location (such as langchain_community or langchain_core), issues a deprecation warning, and returns the class. This allows existing codebases to continue working while encouraging migration to the new package structure.

Usage

This module is imported implicitly whenever import langchain_classic or from langchain_classic import ClassName is used. Users should migrate to the recommended replacement packages rather than relying on these deprecated re-exports.

Code Reference

Source Location

Signature

def _warn_on_import(name: str, replacement: str | None = None) -> None:
    """Warn on import of deprecated module."""
    ...

def __getattr__(name: str) -> Any:
    """Lazy-load deprecated classes with warnings."""
    ...

Import

import langchain_classic
# or
from langchain_classic import OpenAI  # triggers deprecation warning

I/O Contract

Inputs

Name Type Required Description
name str Yes The attribute name being accessed on the langchain_classic module (handled by __getattr__)

Outputs

Name Type Description
class/object Any The requested class imported from its canonical location (e.g., langchain_community.llms.OpenAI), along with a deprecation warning emitted to stderr

Key Mechanisms

Lazy Import with Deprecation Warnings

The __getattr__ function handles over 35 named imports, mapping each to its new canonical location. For each name:

  1. The class is imported from its new package (e.g., langchain_community, langchain_core)
  2. A deprecation warning is issued via _warn_on_import
  3. The class object is returned

Interactive Environment Detection

The _warn_on_import function checks if the code is running in an interactive environment (e.g., Jupyter, IPython). If so, warnings are suppressed to avoid polluting auto-complete output.

Relocated Classes

The module maps classes to their new homes across several packages:

Category Examples New Package
Agents MRKLChain, ReActChain, SelfAskWithSearchChain langchain_classic.agents
Chains LLMChain, ConversationChain, VectorDBQA langchain_classic.chains
LLMs OpenAI, Anthropic, Cohere, HuggingFaceHub langchain_community.llms
Prompts PromptTemplate, FewShotPromptTemplate langchain_core.prompts
Utilities WikipediaAPIWrapper, SQLDatabase, SerpAPIWrapper langchain_community.utilities
Vector Stores FAISS, ElasticVectorSearch langchain_community.vectorstores
Docstores InMemoryDocstore, Wikipedia langchain_community.docstore

Special Cases

  • LLMBashChain: Raises an ImportError directing users to install langchain-experimental
  • Prompt: Aliases to PromptTemplate for backward compatibility
  • SerpAPIChain/SerpAPIWrapper: Both map to SerpAPIWrapper

Usage Examples

Basic Usage

# This will work but emit a deprecation warning
from langchain_classic import PromptTemplate

# Recommended: import directly from the canonical location
from langchain_core.prompts import PromptTemplate

Checking Available Exports

import langchain_classic

# View all re-exported names (35+ classes)
print(langchain_classic.__all__)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment