Writing Tool Schemas for GPT
Introduction & Goals
Welcome to your first lesson in developing GPT agents with tool integration! In this lesson, you'll learn the foundational skill of preparing function schemas that enable GPT to understand and request the use of your custom tools through a process called function calling.
By the end of this lesson, you'll understand how to write TypeScript functions and create JSON schemas that describe these functions to GPT. These schemas are the bridge that allows GPT to understand what your functions do and how to call them, even though GPT never sees your actual TypeScript code. This foundational step is essential before you can build a complete GPT agent system that can execute tools and use their results.
How GPT Uses Tools Through Function Calling
Function calling is the mechanism that allows GPT to use external tools and capabilities beyond text generation. Here's how the process works:
- You provide GPT with function schemas (
JSONdescriptions of your tools). - GPT analyzes user requests and determines if any of your tools would be helpful.
- If GPT decides a tool is needed, it responds with a tool use request that includes the function name and specific parameters.
- Your system receives this tool use request and executes the corresponding
TypeScriptfunction with the provided parameters. - Your system sends the function result back to GPT.
- GPT incorporates this result into its response to the user or decides to use additional tools if needed.
The key insight is that GPT only sees the schemas (JSON descriptions), never your actual TypeScript code. The schemas must contain all the information GPT needs to understand what each tool does and how to use it correctly. This separation means you can organize your TypeScript functions however you like — GPT relies entirely on the schema descriptions to make decisions about tool usage.
Writing TypeScript Tool Functions
When creating tool functions for GPT agents, your TypeScript functions serve two purposes: they contain the actual logic that will be executed, and they provide the foundation for creating accurate schemas.
You might notice a specific pattern in how we define these functions: they accept a single object argument rather than multiple separate arguments.
Why use a single object argument?
This is a critical safety pattern for AI engineering. When GPT decides to call a tool, it generates parameters as a JSON object (e.g., {"a": 10, "b": 5}). In the JSON specification, objects are technically unordered. This means GPT might validly return {"b": 5, "a": 10}.
If your function relied on the order of arguments—like function(a, b)—and the parameters arrived in a different order than expected, you could end up with serious bugs (imagine a subtraction function calculating 5 - 10 instead of 10 - 5).
By defining your function to accept a single object (args), TypeScript maps values by their name (args.a, args.b) rather than their position. This ensures your tools always execute correctly, regardless of the order in which GPT generates the parameters.
