Build a Chatbot with LangChain and OpenAI API in Python

As generative AI applications mature beyond simple single-turn API queries, developers frequently need to manage multi-turn conversation history, dynamic prompt templates, external tool integrations, and stateful memory. Writing raw HTTP calls to OpenAI for complex conversational workflows introduces significant boilerplate code.

**LangChain** is the leading open-source framework designed to simplify the creation of applications powered by Large Language Models (LLMs). This comprehensive guide walks through building a production-grade conversational chatbot in **Python** using LangChain and the OpenAI API, covering chat models, prompt templates, and conversation memory.

1. Environment Setup and Package Installation

Before writing code, install the required LangChain and OpenAI dependency packages via pip and configure your OpenAI API key securely as an environment variable.

C
# Terminal installation command
pip install langchain langchain-openai python-dotenv
C
# Python environment initialization (.env file or script)
import os
from dotenv import load_dotenv

load_dotenv()
# Ensure OPENAI_API_KEY is set in your environment
if "OPENAI_API_KEY" not in os.environ:
    os.environ["OPENAI_API_KEY"] = "your-api-key-here"

2. Initializing Chat Models and Prompt Templates

LangChain abstracts interactions with OpenAI's chat models (`ChatOpenAI`) and enables dynamic prompt construction using `ChatPromptTemplate` and message placeholders.

C
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

# Initialize the OpenAI chat model
llm = ChatOpenAI(model="gpt-4o", temperature=0.7)

# Create a structured chat prompt template with system instructions and memory placeholder
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful, expert AI software engineering assistant."),
    MessagesPlaceholder(variable_name="chat_history"),
    ("human", "{input}")
])

# Combine prompt and model into a run chain
chain = prompt | llm

3. Managing Conversational Memory

Because LLMs are inherently stateless, building a functional chatbot requires maintaining conversation history across multiple user turns. LangChain provides memory utilities and message history buffers to inject past interactions automatically.

C
from langchain_core.chat_history import InMemoryChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory

# Store session histories in-memory (production apps use Redis or Postgres)
store = {}

def get_session_history(session_id: str):
    if session_id not in store:
        store[session_id] = InMemoryChatMessageHistory()
    return store[session_id]

# Wrap the core chain with message history management
conversational_chain = RunnableWithMessageHistory(
    chain,
    get_session_history,
    input_messages_key="input",
    history_messages_key="chat_history"
)

4. Executing Conversational Turns

Now you can invoke the conversational chain by passing a unique session ID and user input. The model will retain context across multiple sequential prompts.

C
# Config specifying the active user session
config = {"configurable": {"session_id": "user_session_001"}}

# Turn 1
response_1 = conversational_chain.invoke(
    {"input": "Hi! Can you explain what dependency injection is in C#?"},
    config=config
)
print("AI:", response_1.content)

# Turn 2 (Testing conversational memory)
response_2 = conversational_chain.invoke(
    {"input": "Can you provide a short code example for that?"},
    config=config
)
print("AI:", response_2.content)

Conclusion

Building chatbots with LangChain and the OpenAI API in Python streamlines prompt orchestration and stateful memory management. By leveraging `ChatOpenAI`, structured prompt templates, and `RunnableWithMessageHistory`, developers can quickly scale sophisticated, context-aware AI assistants into production applications.