⚡ Bridging LLMs and External Execution with Function Calling
While chat completions excel at natural language synthesis, they cannot natively query live databases, fetch real-time weather feeds, or execute financial transactions. Custom Function Tools allow OpenAI models to analyze user queries, determine when external data is required, and output structured JSON arguments ready for execution by your local code runner.
1. Interactive Function Calling Simulator
Test the live function calling lifecycle below. Select a preset query or type your own to see how the model constructs tool calls and handles return payloads.
[Simulator Ready] Click "Execute Tool" to simulate OpenAI function calling & response loop.
2. Python Implementation: Function Dispatcher Loop
A production Python script demonstrating how to define JSON schemas, invoke chat completions with tools, dispatch local functions, and return results to the model.
Python (function_calling_demo.py)
import json
from openai import OpenAI
client = OpenAI()
# 1. Define Local Function Implementation
def get_current_weather(location: str, unit: str = "celsius"):
# Simulated weather API lookup
weather_data = {
"tokyo": {"temperature": 18, "condition": "Partly Cloudy", "unit": unit},
"san francisco": {"temperature": 14, "condition": "Foggy", "unit": unit},
"paris": {"temperature": 22, "condition": "Sunny", "unit": unit}
}
city = location.lower()
return json.dumps(weather_data.get(city, {"error": "Location not found"}))
# 2. Define Tool Schema for OpenAI
tools = [{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather for a given city.",
"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"]
}
}
}]
def run_conversation(user_query: str):
messages = [{"role": "user", "content": user_query}]
print(f"[*] User Prompt: {user_query}\n")
# First API call: Model decides whether to call a function
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=tools,
tool_choice="auto"
)
response_message = response.choices[0].message
messages.append(response_message)
# Check if model invoked any tool calls
if response_message.tool_calls:
print("[+] Model requested tool execution:")
for tool_call in response_message.tool_calls:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
print(fn_call_log := f" - Function: {function_name}({function_args})")
# Execute local function dispatcher
if function_name == "get_current_weather":
function_response = get_current_weather(
location=function_args.get("location"),
unit=function_args.get("unit", "celsius")
)
# Append tool response back to message history
messages.append({
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": function_response,
})
# Second API call: Model synthesizes final answer using tool output
second_response = client.chat.completions.create(
model="gpt-4o",
messages=messages
)
print(f"\n[+] Final Synthesized Answer:\n{second_response.choices[0].message.content}")
if __name__ == "__main__":
run_conversation("What's the weather like in Tokyo right now?")
Execution Output:
[*] User Prompt: What's the weather like in Tokyo right now?
[+] Model requested tool execution:
- Function: get_current_weather({'location': 'Tokyo', 'unit': 'celsius'})
[+] Final Synthesized Answer:
The current weather in Tokyo is 18°C and partly cloudy.
3. Production Best Practices for Function Calling
Engineering standards for robust schema validation, error handling, and security in production function calling loops.
Function Calling Best Practices Checklist
PRODUCTION TOOL-CALLING CHECKLIST:
┌────────────────────────┬───────────────────────────────────────────────────────────────────────────────┐
│ BEST PRACTICE │ IMPLEMENTATION & OBJECTIVE │
├────────────────────────┼───────────────────────────────────────────────────────────────────────────────┤
│ Use Pydantic Schemas │ Automatically generate OpenAI JSON schemas from Pydantic models in Python │
│ │ using libraries like `instructor` or `pydantic`. │
├────────────────────────┼───────────────────────────────────────────────────────────────────────────────┤
│ Strict Error Catching │ Always catch exceptions inside local tool dispatchers and return JSON error │
│ │ strings so the LLM can explain failures gracefully. │
├────────────────────────┼───────────────────────────────────────────────────────────────────────────────┤
│ Enforce Tool Policies │ Use `tool_choice="required"` or force specific tool IDs when deterministic │
│ │ workflow steps are mandatory. │
└────────────────────────┴───────────────────────────────────────────────────────────────────────────────┘
Masterclass Rule: Treating function outputs as untrusted input and implementing robust try/except JSON error payloads prevents cascading agent failures in production.