Introduction & Context

Welcome back! In the last lesson, you learned how to inspect the result object after running an OpenAI agent. 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 a dictionary 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 output_type 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.

Understanding The output_type Parameter

The output_type parameter is a powerful feature of the OpenAI Agents SDK. It allows you to specify the exact format you want the agent’s output to follow. When you set this parameter, the agent will try to produce responses that match the structure you define.

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 top_tip, you can define this structure and use it as the output_type. The SDK will then validate the agent’s output against this structure, making it much easier to process and debug.

By using output_type, 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 raise an error if the output does not match the expected type.

Creating A Custom Data Model With Pydantic

To define a custom output structure, you can use Pydantic, a popular Python library for data validation and settings management. Pydantic allows you to create data models using Python classes, and it will automatically check that any data matches the structure you define.

Let’s say you want your agent to recommend a travel destination. You can create a Pydantic model called TravelRecommendation with three fields: destination, reason, and top_tip. Here’s how you can define this model:

In this example, each field is a string, and you can add a description to help document what each field means. Pydantic will make sure that any data assigned to this model matches the expected types and structure.

Configuring The Agent With The output_type Parameter

Now that you have a custom data model, you can configure your agent to use it as the output_type. This tells the agent to always return its output in the format defined by your Pydantic model.

Here’s how you can set up the agent:

By assigning output_type=TravelRecommendation, you are instructing the agent to produce outputs that match the fields and types defined in your model. 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.

Running The Agent And Inspecting The Structured Output

With your agent configured, you can now run it and see how the output is shaped according to your custom data model.

When you run this code, the agent will return a structured output that matches your TravelRecommendation model. 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.

Accessing Specific Properties of the Structured Output

Once your agent returns a structured output using your Pydantic model, you can easily access individual fields just like attributes of a Python object. 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.

Summary & Next Steps

In this lesson, you learned how to use the output_type parameter to shape and type-check your agent’s outputs. You saw how to define a custom data model with Pydantic, configure your agent to use this model, 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 models, 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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal