Tutorial demonstrating a minimal LangChain LCEL chain composing a prompt, ChatOpenAI LLM, and StrOutputParser via pipe operator and inspecting input and output schemas.
This tutorial walks through a minimal LCEL (LangChain Core Execution Language) chain: a prompt -> ChatOpenAI LLM -> StrOutputParser. It demonstrates composing components with the | operator, invoking the chain, and inspecting input/output schemas. This is the canonical “hello world” of LCEL: simple, composable, and directly readable.Quick links:
1 — Import components and define prompt, LLM, and output parser
First, import the required pieces and create the prompt template, the ChatOpenAI LLM instance, and the StrOutputParser that converts model output to a plain Python string.
from langchain_openai import ChatOpenAIfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.output_parsers import StrOutputParserprompt = ChatPromptTemplate.from_template("""You are a helpful assistant.Answer the following question: {question}""")llm = ChatOpenAI()output_parser = StrOutputParser()
Call the chain’s invoke method with the prompt input fields. In this prompt, the placeholder key is question:
result = chain.invoke({"question": "Tell me about The Godfather movie"})print(result)
Example console output (model wording will vary):
"The Godfather" is a classic crime film directed by Francis Ford Coppola and released in 1972. It is based on the novel of the same name by Mario Puzo and follows the story of the powerful Italian-American crime family, the Corleones. The film stars Marlon Brando as the patriarch Vito Corleone and Al Pacino as his youngest son, Michael Corleone, who becomes increasingly involved in the family's criminal activities.The Godfather is widely regarded as one of the greatest films in cinematic history, known for its iconic performances, memorable quotes, and intricate storytelling. It won multiple Academy Awards, including Best Picture, and has had a significant impact on popular culture.
StrOutputParser simply returns the LLM response as a plain Python string.
Every LCEL component exposes an input_schema and output_schema. This makes composition safe and transparent: LangChain Core maps outputs to the next component’s inputs automatically, so you can focus on composing components instead of manually transforming data.To view the chain-level schemas:
Each LCEL component defines explicit input_schema and output_schema. The | operator composes components and LangChain Core maps data between them automatically.
This pattern removes boilerplate transformation code and encourages building small, reusable components.
Next steps:
Try implementing a custom LCEL component that follows the LCEL interface so it can be piped into chains just like built-ins.
Explore RunnablePassthrough as a foundational example of a simple, composable component.