tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Generative AI > OpenAI API > OpenAI Function Calling

OpenAI Function Calling

Author: Venkata Sudhakar

Function calling lets you tell the OpenAI API to return its response as a structured JSON object that matches a schema you define, instead of free-form text. You describe the fields you want - their names, types, and descriptions - and the model fills them in based on the input text. This is perfect for extracting structured data from unstructured sources: a customer writes a messy email about their insurance claim, and function calling turns it into a clean JSON record with the policy number, incident date, claimed amount, and incident type all in the right fields - ready to insert into your database.

You define your schema as a JSON object in the tools parameter, and set tool_choice to force the model to always use it. The model never replies with free text when tool_choice is set - it always fills in the schema. This is more reliable than asking the model to "respond in JSON" in the prompt, because the API enforces the schema structure rather than hoping the model formats it correctly. Function calling handles missing fields gracefully by leaving them null rather than hallucinating values.

The below example shows an insurance company using function calling to automatically extract claim details from customer emails, turning unstructured messages into structured database records.


Testing with two realistic customer emails,


It gives the following output,

=== Email 1 ===
Customer:    Kavya Reddy
Policy:      POL-88234
Type:        ACCIDENT
Date:        2025-03-15
Amount:      Rs 45000
Description: Customer's car was rear-ended by a truck at a traffic light on MG Road.

=== Email 2 ===
Customer:    Suresh Kumar
Policy:      INS-44910
Type:        THEFT
Date:        2025-03-22
Amount:      Rs 100000
Description: Shop was broken into and a laptop and cash register were stolen.

# "one lakh" was correctly converted to 100000
# The date format was standardised to YYYY-MM-DD automatically
# Missing fields were omitted rather than hallucinated

Function calling is the right tool whenever you need to reliably turn free text into database-ready records. Common business uses: extracting line items from invoice emails, parsing job application details from CV text, pulling booking details from travel request emails, converting customer feedback into tagged support tickets, and reading expense claim forms submitted as plain text. Because the schema is enforced by the API rather than prompt instructions, the output structure is consistent even when the input text is messy, abbreviated, or poorly formatted.


 
  


  
bl  br