How to Use Function Calling in Prompt Engineering
Use
function calling by defining a function schema and letting the AI model call it with arguments extracted from the prompt. This enables structured, reliable outputs by specifying function names and parameters in the prompt or API call.Syntax
Function calling involves specifying a function name and parameters that the AI model can invoke. You define a function schema with a name and parameters describing expected inputs. The model then returns a call with arguments matching the schema.
Key parts:
- name: The function's identifier.
- parameters: JSON schema describing input fields.
- function_call: Option to specify if the model should call a function automatically or manually.
python
functions = [
{
"name": "get_weather",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location"]
}
}
]
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "What's the weather in Paris?"}],
functions=functions,
function_call="auto"
)Example
This example shows how to define a function schema for a weather query and get the AI to call it with parameters extracted from the user's question.
python
from openai import OpenAI client = OpenAI() functions = [ { "name": "get_weather", "parameters": { "type": "object", "properties": { "location": {"type": "string"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} }, "required": ["location"] } } ] response = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": "What's the weather in New York in celsius?"}], functions=functions, function_call="auto" ) print(response.choices[0].message.function_call)
Output
{"name": "get_weather", "arguments": "{\"location\": \"New York\", \"unit\": \"celsius\"}"}
Common Pitfalls
Common mistakes when using function calling include:
- Not defining the function schema correctly, causing the model to fail to call the function.
- Missing required parameters in the schema, leading to incomplete calls.
- Forgetting to set
function_callto"auto"or specifying the function name explicitly. - Not parsing the returned
function_call.argumentsJSON string properly.
Example of a wrong and right way:
python
# Wrong: Missing required parameter 'location' functions_wrong = [ { "name": "get_weather", "parameters": { "type": "object", "properties": { "unit": {"type": "string"} }, "required": ["location"] # 'location' missing in properties } } ] # Right: Include all required parameters functions_right = [ { "name": "get_weather", "parameters": { "type": "object", "properties": { "location": {"type": "string"}, "unit": {"type": "string"} }, "required": ["location"] } } ]
Quick Reference
- Define functions with
nameandparametersJSON schema. - Use
function_call="auto"to let the model call functions automatically. - Parse
function_call.argumentsas JSON to get parameter values. - Ensure required parameters are included in the schema.
Key Takeaways
Define clear function schemas with names and parameter types for reliable function calling.
Set function_call to "auto" to enable automatic function invocation by the model.
Always parse the returned function_call arguments as JSON to use the parameters.
Include all required parameters in the function schema to avoid errors.
Test your function calling setup with simple examples before complex prompts.