AI Hints
Home›LangChain Tutorial›Structured Output
LangChain · Lesson 28 of 120

Why Structured Output

Text vs data.

⏱ 2 min read▶ Video at 30:01🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

Models write text. But programs need data, with fixed fields, like a rating that's a number, or a list of genres. Asking for J SON in the prompt sometimes works, and sometimes doesn't. Structured output makes it reliable.

Why Structured Output
From the video: Why Structured Output
  • Models write text
  • Programs need data
  • Make it reliable

Text vs structured

With free text, you get a friendly paragraph, and must dig the facts out yourself. The format can change from call to call. With structured output, you describe the exact fields you want, and get back a Python object, with those fields, every time. Your code can use it straight away.

Free text

  • A paragraphFormat variesYou parse it

Structured

  • Exact fieldsSame shape every timeA Python object

Where you need it

You need structured output whenever code, not a person, uses the answer. Extracting order numbers from emails. Rating reviews. Tagging books by genre. Filling in a form. Or deciding which route to take in a chain.

keyvalue
Extractionorder numbers from emails
Scoringrate reviews 1–5
Tagginggenres, topics
Routingwhich path next

Summary

Structured output gives you data with fixed fields, instead of free text. It's essential whenever code uses the answer. Next, describing that data with Pydantic.

Key takeaways
  • Code needs data
  • Fixed fields
  • Same shape every time
Next up: Pydantic Models — describing data