An OpenAI Agents SDK agent can return its final answer as a validated object instead of free text: set the output type to a Pydantic model in Python, or a schema in TypeScript, and the SDK parses and validates the result. That is what makes the agent’s answer safe to hand to another program. Define the shape before you build the agent, keep it small, and test with an input that does not fit neatly, because that is the input that breaks schemas.
What you need
- Python with the openai-agents package installed and an API key
- A Pydantic model for the answer shape
- One test input with a known correct answer
Step 1: Define the output model
Write a Pydantic model with the fields the answer must carry, each with a short description. Keep the list short, with only the fields the next step needs.
Step 2: Set the output type on the agent
Set the output type on the Agent to your model, then run the agent on the test input and read the validated result object, not the raw text.
Step 3: Test with an awkward input
Run the agent on an input that does not fit the shape neatly, such as a vague request, and confirm it still returns a valid object or fails clearly. If it returns a half-filled object, tighten the model or the instructions.
Worked example: synthetic extraction
The table below is a synthetic example. It shows the expected result of structured output; no agent was run.
| Field | Expected | Returned? |
|---|---|---|
| event_name | Short name | Yes |
| event_date | A date | Yes |
| attendees | A list | Yes |
| Object validates | Yes | Pass |
Expected result (UNTESTED). the agent returns a validated object with all three fields correctly filled. This check was not run live; the table shows the expected result for the synthetic example.
Limits
- The output type only fixes the shape, not the content; a wrong value that fits the schema still passes.
- Realtime voice agents do not support structured outputs, so do not plan a voice flow around one.
- Keep the model small, because every extra field is another chance for the model to guess.
