Defining Function Schemas
You'll learn: How to write JSON Schema specifications that tell the LLM exactly what parameters a function accepts and what structure it returns.
What Are Function Schemas?
When you enable function calling (structured output via tools), you need to describe each function's interface in a machine-readable format. This is where JSON Schema comes in—a standard vocabulary for defining the shape of JSON data.
Think of a function schema as a contract: it tells the LLM "here's what you can send me, and here's what each field means." The LLM reads this schema and generates function calls that match your specification exactly.
Core JSON Schema Elements for Functions
A function schema typically includes:
name: The function identifier (string)description: What the function does (helps the LLM decide when to use it)parameters: A JSON Schema object defining the input shapetype: Usually"object"for named parametersproperties: Dictionary of parameter names and their schemasrequired: Array of mandatory parameter names
Each property in parameters.properties has its own schema:
type:"string","number","boolean","array","object", etc.description: What this parameter representsenum: Allowed values (optional constraint)
Example Schema Shape
{
"name": "get_weather",
"description": "Fetch current weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"},
"units": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["city"]
}
}
This schema tells the LLM: "You can call get_weather with a required city string and an optional units choice."
Key Takeaway: Function schemas use JSON Schema syntax to precisely define what parameters a function accepts, enabling the LLM to generate valid, structured function calls that match your API's expectations.