📘 Introduction

If you are starting with LangChain, one of the most useful ideas to learn early is LCEL, short for LangChain Expression Language. LCEL lets you connect prompts, models, and output parsers into one simple pipeline.

In this tutorial, you will build a small text generation app with LangChain, Python, ChatOpenAI, and StrOutputParser. By the end, you will know how the prompt | model | parser pattern works and why it is so common in modern LangChain code.

🎯
We will build a beginner-friendly study-note generator that turns a topic into a short explanation you can reuse in your own AI apps.

💡 What are we implementing?

We are creating a small LangChain workflow that takes a topic, formats a prompt, sends it to a chat model, and returns plain text output. This is a great first LCEL project because the structure is simple, useful, and easy to expand later.

Topic input -> ChatPromptTemplate -> ChatOpenAI -> StrOutputParser -> Final text

Once you understand this flow, you can use the same pattern for summarizers, chatbots, classifiers, and RAG pipelines.

✅ Prerequisites

☑️ Python 3.10 or newer installed
☑️ Basic Python knowledge
☑️ An OpenAI API key
☑️ A terminal or command prompt

⚙️1️⃣ Create a project folder

Create a new project folder and move into it:

mkdir langchain-lcel-python
cd langchain-lcel-python

🐍2️⃣ Create a virtual environment

Create and activate a virtual environment so the tutorial dependencies stay isolated:

python -m venv .venv
source .venv/bin/activate

On Windows PowerShell, activate it with:

.venv\Scripts\Activate.ps1

📦3️⃣ Install libraries

Install the LangChain core package and the OpenAI integration package:

pip install -U langchain-core langchain-openai
This package split matches the current LangChain docs: core LCEL pieces such as prompts and parsers live in langchain-core, while ChatOpenAI lives in langchain-openai.

🔐4️⃣ Set your API key

Store your OpenAI API key as an environment variable. Replace the placeholder with your own key and never hardcode real secrets in your Python file:

export OPENAI_API_KEY="your_api_key_here"

On Windows PowerShell, use:

$env:OPENAI_API_KEY="your_api_key_here"

🧪5️⃣ Preview the prompt before calling the model

Before we build the full LCEL chain, let’s inspect the prompt that LangChain will prepare. Create a file named preview_lcel.py and add this code:

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
    ("system", "You explain AI topics to beginners in simple language."),
    ("human", "Explain {topic} in 3 short bullet points."),
])

formatted_prompt = prompt.invoke({"topic": "vector databases"})

for message in formatted_prompt.messages:
    print(f"{message.type.upper()}: {message.content}")

preview_lcel.py

Run the script:

python preview_lcel.py

You should see the system message and the human message with the topic inserted. This is useful because you can debug the prompt before paying for a model call.

SYSTEM: You explain AI topics to beginners in simple language.
HUMAN: Explain vector databases in 3 short bullet points.
🎓
Want the full working LCEL app? In the Academy section, we will connect this prompt to ChatOpenAI, parse the response into plain text, and turn it into a reusable Python helper.

You can view this post with the tier: Academy Membership

Join academy now to read the post and get access to the full library of premium posts for academy members only.

Join Academy Already have an account? Sign In