Back to Blog

JSON for AI Function Calling: A Practical Guide

Feb 08, 20266 min read

Modern Large Language Models (LLMs) are no longer just text generators; they act as reasoning engines capable of interacting with the outside world. The standard protocol for this interaction is Function Calling (often called Tool Use), which relies entirely on strict JSON schema definitions.

What is Function Calling?

Instead of returning a conversational string, an LLM can return a structured JSON object containing the name of a function to call and the arguments to pass to it. Your application then executes the function (e.g., querying a database, looking up the weather) and feeds the result back to the LLM.

The Importance of Strict JSON Schemas

To tell an LLM what tools it has available, you must provide a definition using a subset of JSON Schema. Any syntax error, ambiguous type definition, or missing description can cause the model to hallucinate arguments or ignore the tool entirely.

Here is an example of a good function definition:

JSON Schema
{
  "name": "get_current_weather",
  "description": "Get the current weather in a given location",
  "parameters": {
    "type": "object",
    "properties": {
      "location": {
        "type": "string",
        "description": "The city and state, e.g., San Francisco, CA"
      },
      "unit": {
        "type": "string",
        "enum": ["celsius", "fahrenheit"]
      }
    },
    "required": ["location"]
  }
}

Common Errors in Function Calling JSON

  • Missing the "type": "object" declaration at the root of the parameters property.
  • Trailing Commas: Some LLM API endpoints strictly enforce RFC 8259 and will throw a 400 Bad Request if your schema has trailing commas.
  • Vague Descriptions: The description field is actually a prompt instruction for the LLM. Leaving it blank reduces tool accuracy significantly.

🛠️ Auto-Generate Your Schemas

Don't write JSON schemas by hand. Paste an example of your desired function arguments into our JSON to Schema Generator to Instantly get the boilerplate you need for OpenAI or Anthropic tool use arrays.