Zero Labs
Industry News / AI

LangChain Series #3: Prompts Explained — Prompt Templates, Chat Prompts, Dynamic Prompting…

Source · Towards AI By Atul Kumar

From the publisher’s feed, collected in the ZeroLabs newsroom.

🚀 LangChain Series #3: Prompts Explained — Prompt Templates, Chat Prompts, Dynamic Prompting, Structured Outputs, and Output ParsersPrompts are the foundation of every LLM application.No matter how powerful a model is, the quality of its output depends heavily on the instructions you provide.In LangChain, prompts are much more than simple strings. They allow developers to dynamically generate instructions, manage conversations, inject chat history, and even enforce structured outputs.What are Prompts?A prompt is an instruction, question, or input given to a model to guide its response.For example:Explain what a Transformer model is. orSummarize the Attention Is All You Need paper.The model uses this prompt as context to generate an output.Static vs Dynamic Prompts:One of the first concepts to understand is the difference between static and dynamic prompting.Static Prompts:Static prompts use hardcoded instructions. Only the user input changes while the prompt structure remains the same.Example:user_input = "Attention Is All You Need"prompt = f"Summarize this topic: {user_input}"Generated prompt:Summarize this topic: Attention Is All You NeedWhy is it called Static?Because the structure never changes.Summarize this topic:will always remain the same.Limitations of Static Prompts: While simple, static prompts have several drawbacks:Difficult to reuseNo validationHarder to maintainDoesn’t integrate well with LangChain workflowsThis is where Prompt Templates become useful.Dynamic Prompts:Dynamic prompts generate instructions at runtime. Instead of hardcoding values, placeholders are used and filled dynamically.Example:Explain the research paper {paper}in a {style} stylewith a {length} explanation.Generated prompt:Explain the research paper Attention Is All You Needin a Beginner-Friendly stylewith a Long explanation.The prompt changes based on user selections.Prompt TemplatesLangChain provides PromptTemplate to create reusable dynamic prompts.from langchain_core.prompts import PromptTemplatetemplate = PromptTemplate( input_variables=[ "paper", "style", "length", "style_instruction" ], validate_template=True, template="""Please summarize the research paper titled "{paper}"Explanation Style: {style}Explanation Length: {length}Style Guidelines:{style_instruction}""")Why Use PromptTemplate Instead of f-Strings?1. ReusabilityCreate once and use many times.2. Variable ValidationPromptTemplate checks missing variables.validate_template=Truehelps catch mistakes early.3. Better LangChain IntegrationPrompt Templates work seamlessly with:ChainsAgentsMemoryLCEL4. Easier MaintenanceUpdating a single template updates your entire application.Generating Prompts DynamicallyThe template can be populated using:final_prompt = template.invoke({ "paper": paper_input, "style": style_input, "length": length_input, "style_instruction": style_instructions[style_input]})This creates a customized prompt based on user selections.Chat Models and MessagesTraditional LLMs accept plain text:Input ↓String ↓OutputChat Models work differently.They accept a sequence of messages.System MessageHuman MessageAI Messageand return an AI response.Message Types in LangChainLangChain provides three primary message types.from langchain_core.messages import ( SystemMessage, HumanMessage, AIMessage)System Message:Defines the model’s behavior.SystemMessage( content="You are a concise AI assistant.")Human Message:Represents user input.HumanMessage( content="Tell me about LangChain.")AI Message:Stores previous responses.AIMessage( content=response.content)ChatPromptTemplateChatPromptTemplate helps create dynamic prompts for chat models.Example from our project:from langchain_core.prompts import ChatPromptTemplatechat_template = ChatPromptTemplate([ ('system', 'You are a helpful {domain} expert'), ('human', 'Explain in simple terms what is {topic}')])prompt = chat_template.invoke({ 'domain': 'machine learning', 'topic': 'diffusion models'})Why Use ChatPromptTemplate?Instead of managing message objects manually, we can generate them dynamically.Benefits:Cleaner codeBetter readabilityEasy variable substitutionMessage PlaceholdersReal chatbots need conversation history.LangChain solves this using: MessagesPlaceholderExample:from langchain_core.prompts import ChatPromptTemplate,MessagesPlaceholderchat_template = ChatPromptTemplate([ ('system', 'You are a helpful customer support agent'), MessagesPlaceholder( variable_name='chat_history' ), ('human', '{query}')])Why Message Placeholders?They allow dynamic insertion of:Chat historyPrevious interactionsContext memorywithout manually rebuilding prompts every time.This becomes extremely useful in: Chatbots, Agents, Customer Support SystemsStructured Outputs and Output Parsers in LangChainSo far, our models have returned plain text responses.Example:The sentiment of the review is positive.While this is easy for humans to read, it is difficult for applications to process programmatically.Imagine building:A Review AnalyzerA Resume ParserA Customer Support BotA Data Extraction SystemInstead of plain text, we often need structured data like:{ "sentiment": "positive", "summary": "The product received favorable feedback."}This is where Structured Outputs and Output Parsers become useful.Structured Outputs:Structured Outputs allow LLMs to return data in predefined formats.Instead of generating free-form text, we define a schema that the model should follow. User Input ↓ LLM ↓Structured ResponseLangChain supports multiple ways to define these structures.Which Schema Type Should You Use?TypedDict: Use TypedDict when:-You only need type hints.You don’t need validation.You trust the model output.Example:from typing import TypedDictclass Person(TypedDict): name: str age: intWhy TypedDict Has No ValidationConsider:person = { "name": "Bob", "age": "25"}Even though age should be an integer, TypedDict will not raise an error.This is because TypedDict provides type hints but does not validate data.Pydantic Models: Use Pydantic when:-Validation is required.Default values are needed.Automatic type conversion is useful.Example:from pydantic import BaseModelclass Student(BaseModel): name: str age: int = 18Why Use Pydantic?Strict Schema Enforcement: Ensures the response follows a predefined structure.Type Safety: Automatically converts values into correct Python types.Validation: Detects missing or invalid fields.LangChain Integration: Works seamlessly with structured outputs and output parsers.JSON Schema: Another popular approach is JSON Schema.Example:{ "title": "student", "type": "object", "properties": { "name": "string", "age": "integer" }, "required": ["name"]}When Should You Use JSON Schema?You don’t want Python-specific dependencies.You need a standard schema format.You want interoperability across systems.Output Parsers:What if the model cannot generate structured output natively?This is where Output Parsers come in.Output Parsers convert raw LLM responses into structured formats.LLM Output ↓Output Parser ↓Structured DataLangChain provides multiple output parsers.String Output Parser: The simplest parser. It converts model responses into plain strings.Example:from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpointfrom dotenv import load_dotenvfrom langchain_core.prompts import PromptTemplateload_dotenv()llm = HuggingFaceEndpoint( repo_id="google/gemma-2-2b-it", task="text-generation")model = ChatHuggingFace(llm=llm)# 1st prompt -> detailed reporttemplate1 = PromptTemplate( template='Write a detailed report on {topic}', input_variables=['topic'])# 2nd prompt -> summarytemplate2 = PromptTemplate( template='Write a 5 line summary on the following text. /n {text}', input_variables=['text'])prompt1 = template1.invoke({'topic':'black hole'})result = model.invoke(prompt1)prompt2 = template2.invoke({'text':result.content})result1 = model.invoke(prompt2)print(result1.content)This parser is commonly used inside chains.JSON Output Parser: JSON Output Parser converts responses into JSON objects.Example:from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpointfrom dotenv import load_dotenvfrom langchain_core.prompts import PromptTemplatefrom langchain_core.output_parsers import JsonOutputParserload_dotenv()llm = HuggingFaceEndpoint( repo_id="Qwen/Qwen2.5-7B-Instruct", task="text-generation")model = ChatHuggingFace(llm=llm)json_parser = JsonOutputParser()template = PromptTemplate( template='Give me 5 facts about {topic} \n {format_instruction}', input_variables=['topic'], partial_variables={'format_instruction': json_parser.get_format_instructions()})# prompt = template.format()# model_output = model.invoke(prompt)# final_result = json_parser.parse(model_output.content)chain = template | model | json_parserchain_result = chain.invoke({'topic': 'Origami'})print(chain_result)The parser automatically injects formatting instructions into the prompt.Why Use JsonOutputParser?Converts output into JSON.Easier programmatic access.Works with models lacking native structured output support.Structured Output Parser: StructuredOutputParser allows us to define required fields explicitly.Example:from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpointfrom dotenv import load_dotenvfrom langchain_core.prompts import PromptTemplatefrom langchain_classic.output_parsers.structured import StructuredOutputParser, ResponseSchemaload_dotenv()llm = HuggingFaceEndpoint( repo_id="Qwen/Qwen2.5-7B-Instruct", task="text-generation")model = ChatHuggingFace(llm=llm)response_schema = [ ResponseSchema(name="fact1", description="The first fact about the topic"), ResponseSchema(name="fact2", description="The second fact about the topic"), ResponseSchema(name="fact3", description="The third fact about the topic"), ResponseSchema(name="fact4", description="The fourth fact about the topic"), ResponseSchema(name="fact5", description="The fifth fact about the topic")]parser = StructuredOutputParser.from_response_schemas(response_schema)template = PromptTemplate( template='Give me 5 facts about {topic} \n {format_instruction}', input_variables=['topic'], partial_variables={'format_instruction': parser.get_format_instructions()})chain = template | model | parserchain_result = chain.invoke({'topic': 'Origami'})print(chain_result)Pydantic Output Parser: PydanticOutputParser combines structure and validation.Example:from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpointfrom dotenv import load_dotenvfrom langchain_core.prompts import PromptTemplatefrom langchain_core.output_parsers import PydanticOutputParserfrom pydantic import BaseModel, Fieldload_dotenv()llm = HuggingFaceEndpoint( repo_id="Qwen/Qwen2.5-7B-Instruct", task="text-generation")model = ChatHuggingFace(llm=llm)class Person(BaseModel): name: str = Field(description='Name of the person') age: int = Field(gt=18, description='Age of the person') city: str = Field(description='Name of the city the person belongs to')parser = PydanticOutputParser(pydantic_object=Person)template = PromptTemplate( template='Generate the name, age and city of a fictional {place} person \n {format_instruction}', input_variables=['place'], partial_variables={'format_instruction':parser.get_format_instructions()})chain = template | model | parserfinal_result = chain.invoke({'place':'sri lankan'})print(final_result)ConclusionStructured outputs make LLM responses predictable and easier to integrate into applications.LangChain provides multiple approaches:TypedDict for lightweight schemasPydantic for validation and type safetyJSON Schema for standard schema definitionsOutput Parsers for models that cannot generate structured outputs nativelyUnderstanding these tools is essential when building reliable AI applications that need structured, machine-readable data rather than free-form text.In the next article, we’ll dive into Chains in LangChain and learn how to combine prompts, models, and output parsers into powerful end-to-end AI pipelines using LCEL.Code Repository: https://github.com/Atul245/LangChain-for-developersIf you found this article helpful, consider:⭐ Starring the repository🍴 Forking the repository👤 Following me for more content on LangChain, Generative AI, and AI EngineeringConnect with me on LinkedIn: https://www.linkedin.com/in/atulkumar8/🚀 LangChain Series #3: Prompts Explained — Prompt Templates, Chat Prompts, Dynamic Prompting… was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.