Topics in this subject
LangChain 3 min read Updated 11 Aug 2026

Prompts and Output Parsers

Using PromptTemplates to inject variables and Output Parsers to guarantee JSON output.

🧑‍🏫 Sabse pehle — simple mein samjho#

Agar tum backend mein AI use kar rahe ho, toh tum nahi chahte ki AI har baar lambi kahani likhe ("Sure, here is the translation..."). Tum chahte ho ki wo ek exact format mein data de (jaise JSON), taaki tumhara code use samajh kar database mein save kar sake. Iske liye hum PromptTemplates use karte hain (jisme variable data inject kiya jata hai) aur Output Parsers use karte hain, jo AI ko force karte hain ki wo sirf aur sirf JSON return kare.

1. Prompt Templates#

Hardcoding strings in Python (using f-strings) works for simple scripts, but in production, prompts get massive. PromptTemplates allow you to create reusable prompt structures with variables.

Using ChatPromptTemplate#

For Chat Models, we use ChatPromptTemplate to structure the exact sequence of System and Human messages, leaving placeholders (using {}) for dynamic data.

from langchain_core.prompts import ChatPromptTemplate

# Create a reusable template
prompt_template = ChatPromptTemplate.from_messages([
    ("system", "You are an expert translator. Translate the text into {language}."),
    ("human", "{text}")
])

# Inject the variables to create the final formatted prompt
formatted_prompt = prompt_template.format_messages(
    language="French", 
    text="Hello, how are you today?"
)

# You would then pass this formatted_prompt to your chat_model
# response = chat_model.invoke(formatted_prompt)

2. Output Parsers (Forcing JSON Structure)#

If you ask an LLM to generate a recipe, it might output a massive block of text. If you want to render that recipe on your frontend, you need the title, ingredients, and steps separated into a JSON object.

LangChain provides several Output Parsers (like CSV, XML, etc.), but the most powerful is the PydanticOutputParser (if you remember Pydantic from FastAPI, it's the exact same library!).

Step 1: Define the desired output structure#

from pydantic import BaseModel, Field

class Recipe(BaseModel):
    title: str = Field(description="The name of the dish")
    ingredients: list[str] = Field(description="List of required ingredients")
    prep_time: int = Field(description="Preparation time in minutes")

Step 2: Initialize the Parser#

from langchain.output_parsers import PydanticOutputParser

parser = PydanticOutputParser(pydantic_object=Recipe)

# The parser automatically generates a block of text telling the LLM exactly how to format the JSON!
format_instructions = parser.get_format_instructions()

Step 3: Inject the instructions into the prompt#

prompt_template = ChatPromptTemplate.from_messages([
    ("system", "You are a chef. {format_instructions}"),
    ("human", "Give me a recipe for {dish}.")
])

# Inject both the user's dish AND the parser's complex JSON instructions
final_prompt = prompt_template.format_messages(
    dish="Spaghetti Carbonara",
    format_instructions=format_instructions
)

# Step 4: Invoke the model
response = chat_model.invoke(final_prompt)

# Step 5: Parse the raw string back into a Python object!
# 'recipe' is now a fully typed Pydantic object, NOT a string!
recipe = parser.parse(response.content)

print(recipe.title)       # Spaghetti Carbonara
print(recipe.ingredients) # ['Pasta', 'Eggs', 'Pancetta', 'Cheese']

In the next section (LCEL), we will see how to compress all 5 of these steps into a single, elegant line of code!