Enable AI models to call your functions and APIs. Build intelligent agents that take action in the real world.
Function calling allows AI models to generate structured JSON that matches function signatures you define. The model decides when to call functions and with what argumentsβyou execute the function and return results.
Define up to 128 tools per request. Model picks the right one automatically.
Define parameters with JSON Schema for validated, structured output.
Model can call multiple functions in parallel for efficiency.
Force the model to call a specific function or any function.
Stream function call arguments as they're generated.
Control whether and which tools the model should use.
Here's a complete example of a weather chatbot that uses function calling:
# Define your function def get_weather(location: str, unit: str = "celsius") -> dict: # Your actual weather API call here return {"temp": 22, "condition": "sunny"} # Define the tool for the API 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. San Francisco" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] } }, "required": ["location"] } } } ] # Make the API call response = client.chat.completions.create( model="mythic-4", messages=[{"role": "user", "content": "What's the weather in Paris?"}], tools=tools ) # Check if model wants to call a function if response.choices[0].message.tool_calls: tool_call = response.choices[0].message.tool_calls[0] args = json.loads(tool_call.function.arguments) # Execute the function result = get_weather(**args) # Send result back to the model final_response = client.chat.completions.create( model="mythic-4", messages=[ {"role": "user", "content": "What's the weather in Paris?"}, response.choices[0].message, { "role": "tool", "tool_call_id": tool_call.id, "content": json.dumps(result) } ], tools=tools ) print(final_response.choices[0].message.content) # "The current weather in Paris is 22Β°C and sunny!"
| Property | Type | Description |
|---|---|---|
type |
string | Must be "function" |
function.name |
string | Function name (a-z, 0-9, underscores) |
function.description |
string | What the function does. Be detailed! |
function.parameters |
object | JSON Schema for function parameters |
function.strict |
boolean | Enable strict schema validation |
Write detailed function and parameter descriptions. The model uses these to decide when and how to call your functions. Include examples, valid values, and edge cases.
Control how the model uses tools with the tool_choice parameter:
# Let model decide (default) tool_choice="auto" # Force model to call a function tool_choice="required" # Disable function calling tool_choice="none" # Force specific function tool_choice={"type": "function", "function": {"name": "get_weather"}}
Detailed descriptions help the model know when to call functions.
Always validate function arguments before executing.
Never execute arbitrary code. Whitelist allowed operations.