Function calling lets a model interface with external systems by returning the arguments for a function you defined with a JSON Schema. Structured Outputs is available in two forms: through function calling, and through a json_schema response format. When you want the model to act, define the function; when you want a structured reply, use the response format. This guide covers the function form.
Prerequisites
- An API that supports function calling.
- One function with a clear job, such as looking up a record.
- Code that executes the function from the arguments the model returns.
Steps
Step 1: Define the function
Write the function definition with a name, a description of when to use it, and a parameters schema listing each argument’s name, type and whether it is required. The description is what helps the model choose the function.
Step 2: Let the model choose it
Send the function in the tools list with a request that needs it. The model decides whether to call it; the API does not execute the function, your code does.
Step 3: Run the arguments
Read the arguments from the model’s tool call, validate them against the schema, then run your function with them. Send the function’s result back to the model so it can give a final answer.
Worked example: Cedar Street Library book lookup
The fictional Cedar Street Library defines a get_book function with one required argument, title, and the model calls it for a reader’s question.
| Step | What happens | Check |
|---|---|---|
| Define | get_book(title: string, required) | Schema written |
| Model call | get_book with title ‘The Salt Road’ | Argument present |
| Execute | Library code returns the book record | Record found |
| Final answer | Model answers from the record | Answer cites the record |
The model calls get_book with the title, the code returns the record, and the final answer comes from that record, completing the 4-step loop.
Expected result. The model returns arguments matching the function schema, your code runs the function, and the final answer uses the function’s result. UNTESTED: this guide was not run against a live model or API. The worked example below is fictional practice material, and its result is the expected result, not a measured one.
Limits
- The API generates arguments; it does not run your function. Executing the call is your code’s job.
- A vague function description makes the model call the function at the wrong times; write the description carefully.
- This guide does not cover parallel tool calls.
