JSON Output Parser

What it is A JSON Output Parser converts the LLM’s text output into a Python dictionary (or list/dict combination) by expecting the output to be valid JSON. It makes unstructured text usable in code.

Why it exists LLMs often return plain text that’s hard to process. JSON parsers enforce a structured format so your program can directly work with the data, avoiding manual string manipulation.

Real-world analogy Think of it like receiving a survey response in a standardized form rather than handwritten notes. You can immediately read fields like {"name": "Alice", "age": 30} instead of trying to parse messy text.

Minimal beginner example

import os
from dotenv import load_dotenv
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.prompts import PromptTemplate

load_dotenv()
api_key = os.getenv("GEMINI_API_KEY")

llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", api_key=api_key)

# Step 1: Prepare parser
parser = JsonOutputParser()

# Step 2: Prepare prompt
prompt = PromptTemplate(
    template="Provide details of Elon Musk in JSON format with keys: name, age, company.",
    input_variables=[]
)

# Step 3: Run LLM and parse output
response = llm.invoke(prompt.format())
parsed_output = parser.parse(response.content)

print(parsed_output)
# Example: {'name': 'Elon Musk', 'age': 51, 'company': 'SpaceX'}

Small LangChain workflow

  1. Prepare a prompt requesting JSON-formatted output.

  2. Send it to the LLM.

  3. Parse the raw text with JsonOutputParser.

  4. Use the resulting dictionary in your chains, APIs, or databases.

Common beginner mistakes

  • LLM output is not valid JSON → parser fails.

  • Forgetting to specify keys clearly in the prompt.

  • Using JSON parser for non-JSON outputs (e.g., plain text list).

When to use this vs alternatives

  • Use JSON Parser when you need structured key-value output.

  • Use Comma-Separated Parser for simple flat lists.

  • Use Pydantic Parser when you need strict validation of fields and types.

Last updated