LangChain and LangGraph · Chapter 4 of 25

Structured output

By the end of this chapter you can describe the data you want as a Pydantic class, get it back from a model as an object with with_structured_output, use Literal for a fixed set of choices and Optional for what a message may not say, read the schema LangChain sends, and say what happens when a model's answer does not fit. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: LangChain basics
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.

Why you need this

So far the model has answered in sentences, which is right for a tenant and useless for a program. Mrunal wants a repair message to become a ticket: the room, the kind of issue, the description, each in its own field, so RoomRaja's system can file it. Faisal wants every message labelled so he can count them.

A program needs data, not prose: a room id it can look up, an issue that is exactly one of five words. This chapter gets that data out of the model reliably.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    ticket.py: a tenant's message as an object

    Mrunal wants repair messages turned into tickets. Write a Pydantic model Ticket with three fields: room_id (a string), issue (one of wifi, plumbing, electrical, furniture, housekeeping, written with Literal) and description (a string).

    Give lab-chat the schema with with_structured_output, invoke it with the tenant's message: The shower in KHR-202 is leaking since yesterday

    and print three lines from the Ticket you get back: Room: and its room_id, Issue: and its issue, Description: and its description.

  2. Task 2

    optional.py: what the message does not say

    Not every message has every detail. Write a Pydantic model Lookup with four fields, each Optional (it may be None): tenant_id (a string), room_id (a string), ticket_id (a string) and issue (one of wifi, plumbing, electrical, furniture, housekeeping).

    Extract it with lab-chat from each of these three messages, in order, and print each result's model_dump(), one per line:

    My Wi-Fi is down, I'm T1047 in HIN-101

    geyser band hai since morning

    Any update on TK5012?

  3. Task 3

    classify_all.py: 31 messages, scored

    Faisal asks how often the assistant gets the topic right. Write a Pydantic model Intent with one field, intent: one of rent, deposit, notice, maintenance, guest, food, room_change, complaint, praise.

    Classify every message in /workspace/data/messages.jsonl (one JSON object per line, with id, text and label) with lab-chat and with_structured_output, in the file's order. For each message the model gets wrong, print its id, a colon and a space, then expected, the true label, a comma and a space, got and the model's intent, for example: M099: expected rent, got food

    Then print the score last: Correct: N of 31

  4. Task 4 · interview question

    Interview: schema_vs_prompt.py, why a schema beats asking for JSON

    Ask lab-chat for the same thing twice, for the message: Hi, the tap in HIN-205 keeps leaking

    First with only a prompt: the system message Extract the room and the problem from the tenant's message as JSON. and the message as the human message. Parse the reply's text with json.loads.

    Then with a schema: a Pydantic model Ticket with room_id (a string) and issue (one of wifi, plumbing, electrical, furniture, housekeeping), through with_structured_output, with the message alone.

    Print two lines: Prompt only: and the list of keys of the parsed JSON, then Schema: and the list of the Ticket's field names as they came back (the keys of its model_dump()).

    The interviewer will ask why a schema beats "reply in JSON".

What you will learn

  • Asking for JSON in the prompt, and why it is fragile.
  • A Pydantic schema and with_structured_output: an object back, not text.
  • What LangChain sends: the schema in the request's response_format.
  • Literal and Optional: fixed choices, and fields that may be missing.
  • When the answer does not fit: the validation error.

More in LangChain basics

Build "Structured output" yourself

RoomRaja, a Pune co-living company, answers tenants on WhatsApp by hand. You build its assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.