Function calling allows you to define custom functions that models can invoke during a conversation. Unlike the built-in tools (web_search and fetch_url), custom functions let you connect the model to your own systems—databases, APIs, business logic, or any external service.
Custom Integrations
Connect models to your databases, APIs, and internal systems.
Multi-Turn Pattern
Model requests a function call, you execute it, then return results.
Built-in vs Custom Tools: Use web_search and fetch_url for web information retrieval. Use function calling when you need the model to interact with your own systems or perform custom operations.
Clear description of what the function does and when to use it
parameters
object
Yes
JSON Schema defining the function’s parameters
strict
boolean
No
Enable strict schema validation
Write clear, specific descriptions. The model uses these to decide when to call each function. Include details about what the function returns and any constraints.
Setting strict: true ensures function calls reliably adhere to your schema. When enabled, the model will always include all required parameters with the correct types.
Here’s a complete example showing the full function calling flow:
Copy
Ask AI
from perplexity import Perplexityimport jsonclient = Perplexity()# 1. Define your function toolstools = [ { "type": "function", "name": "get_horoscope", "description": "Get today's horoscope for an astrological sign.", "parameters": { "type": "object", "properties": { "sign": { "type": "string", "description": "An astrological sign like Taurus or Aquarius" } }, "required": ["sign"] } }]# Your actual function implementationdef get_horoscope(sign: str) -> str: # In a real app, this might call an external API return f"{sign}: Today brings new opportunities for growth."# 2. Send initial request with toolsresponse = client.responses.create( model="openai/gpt-5.2", tools=tools, input="What is my horoscope? I am an Aquarius.")# 3. Process the response and handle function callsnext_input = [item.model_dump() for item in response.output]for item in response.output: if item.type == "function_call": # 4. Execute the function args = json.loads(item.arguments) result = get_horoscope(args["sign"]) # 5. Add the function result to the input next_input.append({ "type": "function_call_output", "call_id": item.call_id, "output": json.dumps({"horoscope": result}) })# 6. Send the function results back to get the final responsefinal_response = client.responses.create( model="openai/gpt-5.2", input=next_input)print(final_response.output_text)
After executing the function, return the results using function_call_output:
Copy
Ask AI
{ "type": "function_call_output", "call_id": "call_abc123", # Must match the original call_id "output": "{\"horoscope\": \"Aquarius: Today brings new opportunities.\"}"}
You can use custom functions alongside built-in tools like web_search:
Copy
Ask AI
tools = [ {"type": "web_search"}, { "type": "function", "name": "save_to_database", "description": "Save information to the user's database.", "parameters": { "type": "object", "properties": { "data": {"type": "string", "description": "Data to save"} }, "required": ["data"] } }]response = client.responses.create( model="openai/gpt-5.2", tools=tools, input="Search for the latest AI news and save a summary to my database.", instructions="Use web_search to find information, then save_to_database to store it.")
The model relies on function descriptions to decide when to call them. Be specific:
Copy
Ask AI
# Good - clear and specific{ "name": "get_user_orders", "description": "Retrieve a user's order history from the database. Returns the last 10 orders including order ID, date, items, and total amount. Use this when the user asks about their past purchases or order status."}# Less effective - vague{ "name": "get_orders", "description": "Gets orders."}