Skip to content

Function calling

A tool definition tells the model what function it may request. The model returns a function name and arguments, not the result. Your application validates the arguments, decides whether to run the function, and sends the result back to the model.

Function calling requires a deployment with a compatible tool parser. Check support before choosing a model. This is not hosted MCP: if your application connects to an MCP server, it is responsible for that connection and may expose selected operations to the model as client-side functions.

Wrap the definition in a function object:

{
"tools": [
{
"type": "function",
"function": {
"name": "lookup_unit",
"description": "Find a unit conversion factor.",
"parameters": {
"type": "object",
"properties": {"unit": {"type": "string"}},
"required": ["unit"]
}
}
}
],
"tool_choice": "auto"
}

Read choices[].message.tool_calls and its function name and JSON-encoded argument string. Keep the assistant message with its calls in the history. For each accepted call append a role: "tool" message, set its tool_call_id to that call’s ID and put the result in content. Send the updated messages in a new request. When streaming, accumulate delta.tool_calls fragments by choice and tool-call index; parse the arguments only after the call is complete.

Responses uses a flat function definition. Use a model that supports both Responses and function calling.

{
"tools": [
{
"type": "function",
"name": "lookup_unit",
"description": "Find a unit conversion factor.",
"parameters": {
"type": "object",
"properties": {"unit": {"type": "string"}},
"required": ["unit"]
}
}
],
"tool_choice": "auto"
}

Read function_call items in output. Each call includes a call_id, function name and JSON-encoded arguments. For the next request, resend the original input items and the model’s relevant output items, followed by a function_call_output item:

{"type": "function_call_output", "call_id": "call_example", "output": "{\"factor\":1000}"}

Use the original call_id, not the response ID or the output item’s ID. Responses does not save the conversation for you. Do not pass previous_response_id.

Use tool_choice: "auto" to let a compatible model decide whether to call a function, or "none" to disable tool use for the request. A forced or required choice is subject to model and parser support, and the object syntax differs between Chat Completions and Responses.

Do not execute a function based on partial streamed arguments. Treat all arguments as untrusted and validate them against your schema and application permissions before executing. If a request is retried, use your own idempotency checks for side effects.