OpenAI Function Calling Intermediate
OpenAI's function calling lets GPT-4o and other models generate structured JSON arguments for functions you define. The model decides when to call functions based on the user's message and the tool definitions you provide.
Defining Tools
Python
from openai import OpenAI import json client = OpenAI() tools = [ { "type": "function", "function": { "name": "get_weather", "description": "Get current weather for a location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "City name, e.g. 'Tokyo'" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] } }, "required": ["location"] } } } ]
Making the Call
Python
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}],
tools=tools
)
message = response.choices[0].message
# Check if the model wants to call a function
if message.tool_calls:
for tool_call in message.tool_calls:
name = tool_call.function.name
args = json.loads(tool_call.function.arguments)
print(f"Call: {name}({args})")
# Output: Call: get_weather({'location': 'Tokyo'})
Complete Tool Execution Loop
Python
def run_conversation(user_message): messages = [{"role": "user", "content": user_message}] response = client.chat.completions.create( model="gpt-4o", messages=messages, tools=tools ) message = response.choices[0].message while message.tool_calls: messages.append(message) # Add assistant message with tool calls for tool_call in message.tool_calls: args = json.loads(tool_call.function.arguments) result = execute_function(tool_call.function.name, args) messages.append({ "role": "tool", "tool_call_id": tool_call.id, "content": json.dumps(result) }) # Get next response (may call more tools or give final answer) response = client.chat.completions.create( model="gpt-4o", messages=messages, tools=tools ) message = response.choices[0].message return message.content
Tool Choice Parameter
| Value | Behavior | Use Case |
|---|---|---|
"auto" (default) |
Model decides whether to call a function | General use |
"none" |
Model will not call any function | Force text-only response |
"required" |
Model must call at least one function | Ensure structured output |
{"type": "function", "function": {"name": "..."}} |
Model must call the specified function | Force specific function |
Parallel Function Calling: GPT-4o can request multiple function calls in a single response. Your code should handle all tool_calls before sending the results back together.
Always Validate: Even though function arguments are structured JSON, always validate the values before executing. The model might provide unexpected values for string fields.
Lilly Tech Systems