Defining a Tool Schema and Requiring Tool Use
Introduction: From Prompted JSON to Native Tool Calls
You've just learned how to prompt the model to return structured JSON by including a schema in your system prompt. Now you're ready to take the next step: using OpenAI's native tool-calling feature, which provides stronger guarantees and implements Factor 4 (Tools are just structured outputs) of the 12-Factor Agents methodology directly. By the end of this lesson, you'll be able to define tool schemas, require their use, and handle the structured output reliably.
Defining a Tool Schema
A tool schema is a formal definition of a function that the model can "call" by returning structured data. You define the function's name, what it does, and what parameters it expects. The schema follows JSON Schema conventions, which means you specify types, descriptions, and which fields are required.
The schema is a dictionary that defines the interface between your code and the model. It contains several key fields that tell the model exactly how the tool should be used:
type: For tool definitions, this is always set to"function".name: The identifier your code will use to recognize this tool—in this case,"final_answer".description: A natural language explanation that helps the model understand when it is appropriate to use this tool.strict: Setting this toTrueenables Structured Outputs for tools, guaranteeing the model's response will exactly match your parameter schema.parameters: An object using JSON Schema notation to define the expected arguments, such as our"answer"string.required: A list of property names that the model must include for the tool call to be considered valid.additionalProperties: False: A strict constraint that prevents the model from adding unexpected fields to the output.
This explicit contract gives you confidence that any final_answer call will have exactly the data you expect. By defining these constraints, you move the validation logic from your application code directly into the model's generation process.
