Completing the Tool Use Cycle with GPT-5
Introduction & Overview
In the previous lessons, you learned how to create tool schemas and understand GPT-5's responses when it wants to use tools. You can now recognize when GPT-5 requests tool execution by checking for type: "function_call" items in the output array and extracting the necessary details from function call items. However, knowing what GPT-5 wants to do is only half the story — you still need to actually execute those tools and complete the conversation cycle.
In this lesson, you'll learn how to bridge that gap by executing the functions GPT-5 requests, capturing their results, and sending those results back to GPT-5 in the proper format. By the end of this lesson, you'll have a complete tool execution pipeline that can handle GPT-5's tool requests from start to finish, maintaining proper conversation flow throughout the entire process.
The Complete Tool Execution Flow
Before we dive into the implementation, let's understand the complete workflow we'll be building in this lesson. Here's the step-by-step process that transforms GPT-5 from a simple chatbot into a capable agent:
- Set up the foundation - Create function mappings, tool schemas, and initial conversation messages
- Send the initial request - Make the first API call to GPT-5 using
responses.createwith the user's question and available tools - Detect tool use requests - Iterate through the
outputarray to find items withtype: "function_call" - Extract tool information - Pull out the function name, arguments, and call ID from each function call item
- Execute the requested functions - Parse the JSON arguments and use our function mapping to call the actual Python functions with GPT-5's parameters
- Collect and format tool results - Structure all function outputs in the
function_call_outputformat with matchingcall_idvalues - Send results back to GPT-5 - Make a second API call with the complete conversation, including both the original
function_callitems AND their outputs - Display the final response - Show GPT-5's natural language answer that incorporates the tool outputs
This complete cycle enables GPT-5 to seamlessly use tools as part of its reasoning process, transforming raw function outputs into conversational responses that directly answer user questions.
Setting Up the Foundation
Before diving into tool execution, we need to establish the foundation that connects GPT-5's tool requests to our actual Python functions. As we covered in previous lessons, this involves creating a mapping dictionary and preparing our tool schemas and initial messages. The critical component here is the function mapping dictionary — this serves as the bridge between the tool names GPT-5 uses and our actual Python functions.
This setup creates everything we need for tool execution: the tools dictionary enables dynamic function lookup, the system_prompt guides GPT-5's behavior, the tool_schemas provide technical specifications, and the messages array starts the conversation. The function mapping is particularly important because it allows our code to execute the correct function based on GPT-5's string-based tool requests.
