LangChain Classic
LangChain Classic is a legacy Python package providing the original API for building applications with Large Language Models (LLMs) through composability. It represents the state of the `langchain` library prior to its major architectural refactor (pre-0.1.0/0.2.0, before v1.0), which split the project into `langchain-core`, `langchain-community`, and partner packages. This package is in maintenance mode for backward compatibility and does not receive new features; it is not recommended for new projects.
Warnings
- breaking LangChain Classic's API is fundamentally incompatible with the modern LangChain (v1.0+) ecosystem, including `langchain-core`, `langchain-community`, and partner packages. Direct migration requires significant code changes.
- deprecated The `langchain-classic` package is explicitly for backward compatibility with pre-v1.0 LangChain and is not actively developed for new features.
- gotcha Attempting to mix `langchain-classic` with newer `langchain` packages (e.g., `langchain-core`, `langchain-community`, `langchain-openai`) will likely lead to dependency conflicts, import errors, and runtime issues due to incompatible APIs and overlapping namespaces.
- gotcha LangChain Classic will not receive new features, performance optimizations, or regular bug fixes available in the ongoing development of the main `langchain` library. This includes updates for new LLM capabilities or provider integrations.
Install
-
pip install langchain-classic
Imports
- LLMChain
from langchain.chains import LLMChain
- OpenAI
from langchain.llms import OpenAI
- PromptTemplate
from langchain.prompts import PromptTemplate
Quickstart
import os
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
# Set your OpenAI API key as an environment variable (e.g., export OPENAI_API_KEY="sk-...")
# For this example, we use os.environ.get to prevent errors if the key is not set.
openai_api_key = os.environ.get('OPENAI_API_KEY', '')
if not openai_api_key:
print("Warning: OPENAI_API_KEY environment variable not set. Please set it to run the example.")
print("Skipping LLM interaction.")
else:
llm = OpenAI(openai_api_key=openai_api_key, temperature=0.7)
prompt = PromptTemplate(
input_variables=["topic"],
template="Tell me a short, funny joke about {topic}."
)
chain = LLMChain(llm=llm, prompt=prompt)
try:
response = chain.run("developers")
print(f"Joke about developers:\n{response}")
except Exception as e:
print(f"An error occurred during LLM interaction: {e}")
print("Please ensure your OPENAI_API_KEY is valid and has access to the OpenAI API.")