JSON tool schemas
Modern LLMs are fine-tuned to understand JSON tool schemas. You describe your tools in a specific format, pass them to the completion call, and the model returns structured tool_calls instead of text. No regex needed.
The LLM never executes code. It can only generate text. The schema tells the LLM what functions exist and what arguments they expect, so it can generate a structured request. Your code then actually calls the function.
# Define a tool using JSON schema
get_weather_tool = {
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city name, e.g. Tokyo"
}
},
"required": ["location"]
}
}
}A JSON schema tells the LLM: the function name, what it does, and what arguments it accepts with their types.
You can define as many tool schemas as you need. Here is a second one with a boolean parameter, so you can see how different types work.
# Another tool with an enum-like boolean parameter
toggle_light_tool = {
"type": "function",
"function": {
"name": "toggle_light",
"description": "Turn a light on or off in a room",
"parameters": {
"type": "object",
"properties": {
"room": {
"type": "string",
"description": "The room name, e.g. kitchen"
},
"status": {
"type": "boolean",
"description": "True to turn on, False for off"
}
},
"required": ["room", "status"]
}
}
}
tools = [get_weather_tool, toggle_light_tool]Tools support different parameter types: strings, booleans, numbers, enums. All are passed as a list.
How JSON schemas flow through the system
Most major providers now support it: OpenAI, Anthropic, Google, Mistral, and others. LiteLLM handles the differences between providers, so the same tool schema works regardless of which model you use.
AI prompt: Try it with AI
Loading practice…
Fill in the blanks: Write a tool schema
Loading practice…
Quiz: Quiz
Loading practice…