| name | langchain-core-workflow-a |
| description | Build LangChain LCEL chains with prompts, parsers, and composition. Use when this capability is needed. |
| metadata | {"author":"flight505"} |
LangChain Core Workflow A: Chains & Prompts
Overview
Build production chains using LCEL (LangChain Expression Language). Covers prompt templates, output parsers, RunnableSequence, RunnableParallel, RunnableBranch, RunnablePassthrough, and chain composition patterns.
Prerequisites
langchain-install-auth completed
@langchain/core and at least one provider installed
Prompt Templates
ChatPromptTemplate
import { ChatPromptTemplate, MessagesPlaceholder } from "@langchain/core/prompts";
const simple = ChatPromptTemplate.fromTemplate(
"Translate '{text}' to {language}"
);
const chat = ChatPromptTemplate.fromMessages([
["system", "You are a {role}. Respond in {style} style."],
new MessagesPlaceholder("history"),
["human", "{input}"],
]);
console.log(chat.inputVariables);
Partial Templates
const partial = await chat.partial({
role: "senior engineer",
style: "concise",
});
const result = await partial.invoke({
history: [],
input: "Explain LCEL",
});
Output Parsers
import { StringOutputParser } from "@langchain/core/output_parsers";
import { JsonOutputParser } from "@langchain/core/output_parsers";
import { StructuredOutputParser } from "@langchain/core/output_parsers";
import { z } from "zod";
const strParser = new StringOutputParser();
const jsonParser = StructuredOutputParser.fromZodSchema(
z.object({
answer: z.string(),
confidence: z.number(),
sources: z.array(z.string()),
})
);
const instructions = jsonParser.getFormatInstructions();
Chain Composition Patterns
Sequential Chain (RunnableSequence)
import { ChatOpenAI } from "@langchain/openai";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { RunnableSequence } from "@langchain/core/runnables";
const model = new ChatOpenAI({ model: "gpt-4o-mini" });
const extractPrompt = ChatPromptTemplate.fromTemplate(
"Extract 3 key points from:\n{text}"
);
const summarizePrompt = ChatPromptTemplate.fromTemplate(
"Summarize these points in one sentence:\n{points}"
);
const chain = RunnableSequence.from([
{
points: extractPrompt
.pipe(model)
.pipe(new StringOutputParser()),
},
summarizePrompt,
model,
new StringOutputParser(),
]);
const summary = await chain.invoke({
text: "Long article text here...",
});
Parallel Execution (RunnableParallel)
import { RunnableParallel } from "@langchain/core/runnables";
const analysis = RunnableParallel.from({
summary: ChatPromptTemplate.fromTemplate("Summarize: {text}")
.pipe(model)
.pipe(new StringOutputParser()),
keywords: ChatPromptTemplate.fromTemplate("Extract 5 keywords from: {text}")
.pipe(model)
.pipe(new StringOutputParser()),
sentiment: ChatPromptTemplate.fromTemplate("Sentiment of: {text}")
.pipe(model)
.pipe(new StringOutputParser()),
});
const results = await analysis.invoke({ text: "Your input text" });
Conditional Branching (RunnableBranch)
import { RunnableBranch } from "@langchain/core/runnables";
const technicalChain = ChatPromptTemplate.fromTemplate(
"Give a technical explanation: {input}"
).pipe(model).pipe(new StringOutputParser());
const simpleChain = ChatPromptTemplate.fromTemplate(
"Explain like I'm 5: {input}"
).pipe(model).pipe(new StringOutputParser());
const router = RunnableBranch.from([
[
(input: { input: string; level: string }) => input.level === "expert",
technicalChain,
],
simpleChain,
]);
const answer = await router.invoke({ input: "What is LCEL?", level: "expert" });
Context Injection (RunnablePassthrough)
import { RunnablePassthrough } from "@langchain/core/runnables";
const chain = RunnablePassthrough.assign({
wordCount: (input: { text: string }) => input.text.split(" ").length,
uppercase: (input: { text: string }) => input.text.toUpperCase(),
}).pipe(
ChatPromptTemplate.fromTemplate(
"The text has {wordCount} words. Summarize: {text}"
)
).pipe(model).pipe(new StringOutputParser());
Python Equivalent
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnableParallel, RunnablePassthrough
llm = ChatOpenAI(model="gpt-4o-mini")
chain = ChatPromptTemplate.from_template("Summarize: {text}") | llm | StrOutputParser()
analysis = RunnableParallel(
summary=ChatPromptTemplate.from_template("Summarize: {text}") | llm | StrOutputParser(),
keywords=ChatPromptTemplate.from_template("Keywords: {text}") | llm | StrOutputParser(),
)
chain = (
RunnablePassthrough.assign(context=lambda x: fetch_context(x["query"]))
| prompt | llm | StrOutputParser()
)
Error Handling
| Error | Cause | Fix |
|---|
Missing value for input | Template variable not provided | Check inputVariables on your prompt |
Expected mapping type | Passing string instead of object | Use { input: "text" } not "text" |
OutputParserException | LLM output doesn't match schema | Use .withStructuredOutput() instead of manual parsing |
| Parallel timeout | One branch hangs | Add timeout to model config |
Resources
Next Steps
Proceed to langchain-core-workflow-b for agents and tool calling.
Source: flight505/skill-forge — distributed by TomeVault.