Introduction & Overview

Welcome to the first lesson of this course on coordinating OpenAI agent workflows in Python. In the previous course, you learned how to make your agent-powered applications more responsive by using asynchronous and streamed execution modes. You saw how these techniques help your applications feel faster and more interactive, especially in real-time scenarios like chatbots.

In this lesson, we will build on that foundation by focusing on a key aspect of conversational AI: handling multi-turn conversations. Many real-world applications — such as customer support bots, travel assistants, or tutoring systems — require the agent to remember what was said earlier in the conversation. This ability to maintain and use dialogue history is what makes interactions feel natural and coherent.

By the end of this lesson, you will know how to use the to_input_list() method to preserve and pass conversation history to your agent, enabling stateful, multi-turn dialogues. You will see how this method fits into the agent workflow, and you will practice using it to create more engaging and context-aware applications.

Understanding Multi-Turn Conversations

A multi-turn conversation is a dialogue in which the user and the agent exchange several messages back and forth, rather than just a single question and answer. For example, a user might ask for a travel recommendation, then follow up with questions about the best time to visit or what to pack. In these cases, it is important for the agent to remember the previous messages so it can give relevant and accurate responses.

For example, a simple multi-turn conversation history might look like this:

The main challenge in multi-turn conversations is maintaining the context. If the agent forgets what was said earlier, its answers may become confusing or repetitive. The goal is to keep track of the entire conversation history and provide it to the agent each time it is asked to respond. This way, the agent can generate answers that make sense in the context of the ongoing dialogue.

Getting Messages and Agent Steps from Result

To help you manage conversation history, the OpenAI Agents SDK provides the to_input_list() method on the result object returned by the runner. This method is designed to make it easy to preserve and reuse the full sequence of messages exchanged so far.

When you call to_input_list() on a result, it returns a list of all the input items that were part of the conversation up to that point. This includes the original user input and any messages generated by the agent. You can then append a new user message to this list and pass it as input to the agent for the next turn. This approach ensures that the agent always has access to the complete conversation history, allowing it to respond in a way that is consistent and context-aware.

Let’s walk through a practical example to see how this works in code.

Step 1: Getting the First Answer

Suppose you are building a travel assistant agent. To enable the agent to answer follow-up questions naturally, it needs to remember the ongoing conversation. Let's begin by running the agent with an initial user question, which will serve as the starting point for the dialogue.

When you run this code, the agent responds to the user’s initial question. Here’s an example of what the output might look like:

Step 2: Inspecting the Input List

After receiving the agent’s response, you can inspect the conversation history so far by using the to_input_list() method. For better readability, you can print the input list using json.dumps with indentation.

Add the following lines after printing the first answer:

This will display the current state of the conversation history in a nicely formatted JSON structure:

This list contains both the user’s original question and the agent’s detailed response. By preserving this structure, you ensure that the agent can reference previous turns in the conversation.

Step 3: Concatenating a Follow-Up and Inspecting the New Input List

To continue the conversation, you’ll want to add a new user message to the input list by creating a new list that combines the existing conversation history (from result.to_input_list()) with a dictionary representing the user's follow-up question. It’s important to maintain the correct format for each message—each entry should be a dictionary with at least a "role" (such as "user" or "assistant") and a "content" field. This ensures the agent receives the full dialogue context in the structure it expects, allowing it to generate coherent, context-aware responses. For example:

Now, the input list includes the new user question:

By appending the new message, you’re preparing the agent to answer in the context of the entire conversation, not just the latest question.

Step 4: Running the Agent with the Updated Conversation and Getting the Second Answer

With the updated input list—which consists of the input list from the last response plus the new user message—you can now run the agent again. This time, instead of passing a single string as input, you pass the entire list of messages to the agent. This ensures the agent has access to the full conversation history, including the new user message, allowing it to provide a contextually relevant answer to the follow-up question.

Add the following code after printing the updated input list:

Here’s an example of the agent’s response to the follow-up:

Because the agent receives the full conversation history, it can answer the follow-up question in a way that feels natural and informed, maintaining the flow and context of the dialogue. This approach is essential for building engaging, stateful conversational applications.

Summary & Preparation For Practice

In this lesson, you learned how to maintain and pass dialogue history to an agent using the to_input_list() method. This technique allows you to build stateful, multi-turn conversations in which the agent can remember and respond to previous messages, making your applications more natural and engaging.

You saw how to use to_input_list() to collect the conversation so far, append new user inputs, and continue the dialogue seamlessly. You are now ready to practice these skills in hands-on exercises. Keep up the great work — mastering multi-turn conversations is a key step toward building powerful, context-aware AI 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