OpenAI Agents SDK · Chapter 3 of 24

Typed output

By the end of this chapter you can give an agent an output type, say what the SDK sends for it (a strict JSON schema in the request's text.format) and what it gets back (JSON text it turns into your object), limit a field to a few values, describe a field for the model, let a field be None, read the output as an object, and say why a typed output beats parsing a reply. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Your first agents
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of OpenAI Agents SDK are free. This one comes with the all labs plan.

Why you need this

Bhanu's WhatsApp line is mostly requests: a doubt session for a student, a hostel bed, a refund. Before anything can be booked, someone has to read the message and pick out the facts: which student, which subject, which day, the girls' hostel or the boys', AC or not. A reply like "Sure, I can help with your chemistry doubt!" is pleasant to read and useless to a booking system.

So far an agent's final output has been text. This chapter makes it an object with fields, filled from the message by the model, checked by Pydantic, and ready for your code.

What you build: 4 tasks

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

  1. Task 1

    doubt_request.py: a doubt request as an object

    Write a Pydantic model DoubtRequest with three fields: student_id, a str; subject, which can only be physics, chemistry, maths or biology; and day, a str with a Field description that says it is a weekday, tomorrow or today.

    Make an agent for lab-chat whose output type is DoubtRequest and run it on the student's message exactly:

    organic chemistry ka doubt hai, monday ko sir se milna hai. S2044

    Print one line from the output's fields: the student_id, subject and day, separated by a comma and a space (for example: S2030, physics, tomorrow).

  2. Task 2

    refund_request.py: a refund request, None for what is missing

    Write a Pydantic model RefundRequest with three fields, each with a Field description: student_id, a str or None when the message gives none; refund_id, a str like RF5003 or None when not given; and reason, a str, the parent's message.

    Make an agent for lab-chat whose output type is RefundRequest and run it on these two messages, in this order:

    my refund RF5003 still not received, S2041

    refund kab aayega? bahut din ho gaye

    Read each output with the result's final_output_as(RefundRequest) and print it as JSON with Pydantic's own JSON method, one line per message.

  3. Task 3

    classify.py: label 32 messages and score them

    Bhanu labelled 32 real WhatsApp messages in /workspace/data/messages.jsonl (one JSON object per line, with id, from, text and label). Write a Pydantic model Label with one field, intent, that can only be one of the eleven labels: admission, fees, refund, schedule, doubt, results, hostel, complaint, praise, off_limits, counsellor.

    Make one agent for lab-chat with Label as its output type and run it on every message's text, in the file's order. For each message whose intent is not its label, print: the id, a colon and a space, the intent, a comma, then labelled and the label (for example: M031: fees, labelled refund). Then print the score as: Score: right/total.

  4. Task 4 · interview question

    Interview: typed_or_text.py, typed or text

    Run the parent's message beta ghar wapas aa raha hai, fees refund kitna milega? S2061 through two agents for lab-chat. The first has no output type and the instructions: Reply with only the student ID. The second has an output type StudentRef, a Pydantic model with one str field, student_id.

    Print two lines: Text: and the first agent's final output, then Typed: and the second output's student_id.

    The interviewer will ask why a typed output beats asking for text and parsing it.

What you will learn

  • output_type: a Pydantic model as the agent's answer.
  • What the SDK sends: text.format, a strict JSON schema.
  • Literal, Field and None: limits, descriptions and missing values.
  • final_output as an object, and final_output_as.
  • Why typed beats asking for text.

More in Your first agents

Build "Typed output" yourself

Shikhar Classes, a Kota coaching institute, answers parents and students on WhatsApp all day. You build its helpdesk 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 OpenAI Agents SDK are free. This one comes with the all labs plan.