Taming the chatbot to speak machine code. Why text is not enough.
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.
json_modeOpeAI (and many others) support a response_format parameter.
It forces the model to output only valid JSON.
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)
Getting a string that looks like JSON is good. Getting a Python dictionary is better.
from langchain_core.output_parsers import JsonOutputParser
chain = prompt | model | JsonOutputParser()
res = chain.invoke({"topic": "bears"})
print(type(res))
# Output: <class 'dict'> -> {"joke": "..."}
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."