Understanding How GPT Uses Tools
Introduction & Overview
Welcome! In this lesson, you will learn how GPT responds when it decides to use your tools. In the previous lesson, you learned how to create tool schemas that describe your methods to GPT. Now, you will discover how to include these tools in your Responses API requests and, more importantly, how to interpret GPT's responses when it seeks to use them.
In this lesson, you will learn how to configure your requests to enable tool use, understand the different types of items GPT returns, and extract the specific information needed to execute the tools GPT requests. By the end, you will be able to recognize when GPT wants to use a tool and gather all the details necessary to facilitate that process.
Setting Up the OpenAI Client and Message Format
Before we send anything to GPT, we need to set up the OpenAI client and build our messages in the format the Responses API expects. We start by requiring the openai gem and initializing the client:
The Responses API uses a slightly more structured message format than you might be used to. Each message has a role (like "user", "assistant", or "developer") and a content array containing typed content blocks. The most common block type for inputs is "input_text". To keep our code clean, we'll define a small helper to build text messages:
One detail matters when you add the model's own replies back into the conversation: content with the assistant role must use the "output_text" type, while "input_text" is only for user and developer messages. The helper picks the type from the role so you never have to think about it again.
This helper produces a properly-shaped message object that we can use throughout our conversation. Using input_text for inputs is the convention used by the OpenAI Responses API.
Using the Developer Prompt to Guide Tool Use
In the Responses API, instructions to the model are typically provided through a special "developer" message rather than a separate system parameter. The developer role is conceptually similar to the system role you may have used before, but in the Responses API it's passed inline as the first item in the input array.
This guidance helps GPT understand your preferences for when and how to use tools, but it is not strictly required for tool functionality. GPT can still recognize and use your tools based solely on their availability in the request — although a clear developer prompt typically results in more consistent and reliable tool use.
