Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Learn/LangChain Mastery/Part 3 · 2. LCEL & Prompt Architecture
Lesson 17 of 68

Structured Output with JSON Mode

Taming the chatbot to speak machine code. Why text is not enough.

Derpx0610 Jan · 1 min read
LangChain Mastery
Your First Chatbot: Talking to an LLMLCEL: Why LangChain Is a Composition EngineThe Runnable Protocol (invoke, batch, stream)Pipe Operator & Data FlowRunnableLambda & Custom LogicRunnableParallel & BranchingPromptTemplate vs ChatPromptTemplateSystem, Human, AI MessagesPrompt Architecture & ReusabilityStructured Output with JSON ModePydanticOutputParserOutputFixingParserStreaming Tokens & Async Chains
Contents
1. The Problem: "Here is the JSON you asked for"2. Using jsonmode3. Parsing to Dict (JsonOutputParser)4. Summary
0% read · 1 min leftBack to top ↑
Course outline & contents0%
LangChain Mastery
Your First Chatbot: Talking to an LLMLCEL: Why LangChain Is a Composition EngineThe Runnable Protocol (invoke, batch, stream)Pipe Operator & Data FlowRunnableLambda & Custom LogicRunnableParallel & BranchingPromptTemplate vs ChatPromptTemplateSystem, Human, AI MessagesPrompt Architecture & ReusabilityStructured Output with JSON ModePydanticOutputParserOutputFixingParserStreaming Tokens & Async Chains
Contents
1. The Problem: "Here is the JSON you asked for"2. Using jsonmode3. Parsing to Dict (JsonOutputParser)4. Summary

1. The Problem: "Here is the JSON you asked for"

You ask the model for JSON. It replies: "Sure! Here is the JSON file: json* *{ "answer": 42 }* * Hope that helps!"

This breaks your app. Your JSON parser crashes because of the text "Sure! Here is..." Text is for humans. JSON is for systems.

2. Using json_mode

OpeAI (and many others) support a response_format parameter. It forces the model to output only valid JSON.

json_mode.py
model = ChatOpenAI(model="gpt-4o", model_kwargs={"response_format": {"type": "json_object"}})

chain = prompt | model

# IMPORTANT: You MUST mention "json" in the prompt for this to work
prompt = ChatPromptTemplate.from_template(
  "Output a JSON object with a field 'joke' about {topic}"
)

res = chain.invoke({"topic": "bears"})
print(res.content)
# Output: { "joke": "Why did the bear..." } (No extra text)

3. Parsing to Dict (JsonOutputParser)

Getting a string that looks like JSON is good. Getting a Python dictionary is better.

json_parser.py
from langchain_core.output_parsers import JsonOutputParser

chain = prompt | model | JsonOutputParser()

res = chain.invoke({"topic": "bears"})
print(type(res)) 
# Output: <class 'dict'> -> {"joke": "..."}

4. Summary

If your code needs to read the answer, use JSON mode. Never rely on "prompt begging" (e.g. "Please don't say anything else"). Enforce it at the API level.

Key Intuition: "If software consumes it, structure it."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonPrompt Architecture & ReusabilityNext lesson →PydanticOutputParser