0PricingLogin
AI Engineering Academy · Lesson

Defining Function Schemas for the API

Write JSON Schema definitions for your functions, pass them in the tools parameter, and understand how the model decides when and how to call them.

What Is Function Calling?

OpenAI's function calling (now called tool calling) lets you describe Python functions to the model in a structured JSON Schema format. When the model determines a function should be called, instead of producing free-text it returns a structured JSON object with the function name and arguments — which your code then executes reliably.

The tools Parameter Structure

You pass your function definitions to the API in the tools parameter as a list of objects. Each object has a type of 'function' and a function key containing the name, description, and a JSON Schema defining the parameters.

from openai import OpenAI

client = OpenAI()

tools = [
    {
        'type': 'function',
        'function': {
            'name': 'get_current_weather',
            'description': 'Get the current weather in a given location.',
            'parameters': {
                'type': 'object',
                'properties': {
                    'location': {
                        'type': 'string',
                        'description': 'City and country, e.g. London, UK'
                    },
                    'unit': {
                        'type': 'string',
                        'enum': ['celsius', 'fahrenheit'],
                        'description': 'Temperature unit to use.'
                    }
                },
                'required': ['location']
            }
        }
    }
]

All lessons in this course

  1. Defining Function Schemas for the API
  2. Processing Tool Calls in Your Application
  3. Parallel Function Calling
  4. Building a Natural Language Database Interface
← Back to AI Engineering Academy