Welcome back! In the last lesson, you learned how to inspect the result object after running an OpenAI agent in JavaScript. You explored how to access the agent's final output, review the original input, see which agent produced the answer, and analyze the step-by-step reasoning and raw responses. This knowledge is essential for understanding and debugging your agent's behavior.
As you continue building more advanced agent applications, you will often need the agent's output to follow a specific structure. For example, you might want the agent to always return an object with certain fields or to produce output that can be easily parsed and used in other parts of your program. Relying on free-form text can make downstream processing difficult and error-prone.
In this lesson, you will learn how to use the outputType parameter to shape and type-check your agent's outputs. This will help you ensure that the agent's responses are always consistent and easy to work with, making your applications more reliable and easier to maintain.
The outputType parameter allows you to specify the exact format you want the agent's output to follow. While the OpenAI Agents SDK accepts both JSON Schema-compatible objects and Zod schemas, Zod is the recommended approach for JavaScript/TypeScript projects.
Zod provides several advantages:
- Full type inference and runtime validation
- Detailed error messages
- A more ergonomic API for JavaScript/TypeScript developers
- Automatic type generation for TypeScript users
When you set the outputType parameter with a Zod schema, the agent will try to produce responses that match the structure you define. The SDK will then validate the agent's output against this schema, ensuring that the data is well-formed.
This is especially useful when you want to automate tasks or integrate the agent's output into other systems. For example, if you want your agent to always return a travel recommendation with a destination, a reason, and a topTip, you can define this structure as a Zod schema and use it as the outputType. The SDK will then guarantee that the agent's output matches this structure, making it much easier to process and debug.
By using outputType, you reduce the risk of unexpected output formats, which can cause errors in your application. It also makes it easier to catch mistakes early, since the SDK will throw an error if the output does not match the expected type.
Zod is a JavaScript/TypeScript-first schema declaration and validation library that integrates seamlessly with the OpenAI Agents SDK. To use Zod in your project, you'll first need to install it:
Once installed, you can create schemas that define the exact shape of data you expect. Here's how to define a schema for a travel recommendation:
Each field is defined using Zod's fluent API. The z.object() method creates an object schema, and within it, you define each property using methods like z.string(), z.number(), z.boolean(), etc. The .describe() method adds descriptions to each field, which helps both for documentation and can provide additional context to the AI model.
Now that you have a custom data model using Zod, you can configure your agent to use it as the outputType. This tells the agent to always return its output in the format defined by your Zod schema.
Here's how you can set up the agent in JavaScript:
By assigning outputType: TravelRecommendation, you are instructing the agent to produce outputs that match the fields and types defined in your Zod schema. The agent will understand that it needs to structure its response as an object with destination, reason, and topTip fields, all containing string values.
This approach makes your agent's output predictable and easy to use in other parts of your code, such as saving to a database, displaying in a user interface, or sending it to another service.
With your agent configured, you can now run it and see how the output is shaped according to your custom data model. In JavaScript, you can use the run utility function to execute the agent and receive a structured result.
When you run this code, the agent will return a structured output that matches your TravelRecommendation schema. For example, you might see:
Notice how the output is no longer a block of free-form text, but a well-structured object with clearly labeled fields. This makes it much easier to access specific pieces of information, such as the destination or the travel tip, in your code.
Once your agent returns a structured output using your Zod schema, you can easily access individual fields. This makes it straightforward to use specific pieces of information in your application.
You can access the individual properties of the output like this:
This will print each part of the recommendation separately:
By accessing properties directly, you can easily use them in your UI, save them to a database, or pass them to other functions in your code.
In this lesson, you learned how to use the outputType parameter with Zod schemas to shape and type-check your agent's outputs in JavaScript. You saw how to define a custom data model using Zod, configure your agent to use this schema, and verify that the output matches the expected structure. This approach makes your agent's responses more reliable and easier to use in downstream applications.
You are now ready to practice these concepts in hands-on exercises. In the next part of the course, you will get the chance to define your own output schemas, configure agents with structured outputs, and handle validation errors. Congratulations on reaching this important milestone — structured outputs are a key skill for building robust and professional agent-powered applications!
