Docs

Model APIs

Tool calling and structured output

Build tool-call turns, validate function arguments and use JSON Schema for verifiable model output.

Updated

Tool-call lifecycle

tools declares functions that a model can request. A tool call is an execution request, not the result of running a function. Your application validates arguments, authorizes and executes the tool, then returns its result with the matching tool_call_id. Enable this only for a model and route that support tools.

Chat Completions body
{
  "model": "YOUR_MODEL_ID",
  "messages": [
    {
      "role": "user",
      "content": "What is the weather in Hangzhou?"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "Read the current weather for a city.",
        "parameters": {
          "type": "object",
          "properties": {
            "city": {
              "type": "string"
            }
          },
          "required": [
            "city"
          ],
          "additionalProperties": false
        }
      }
    }
  ],
  "tool_choice": "auto"
}

Validate arguments and side effects

  • Function arguments are often a JSON string; parse and validate them against your own schema.
  • Writes, payments and deletions need application authorization and idempotency controls; a model request is not authorization.
  • Bound tool iterations, execution time and output size to avoid unbounded loops.

Structured output

Models with structured output may accept response_format or the equivalent protocol setting. Supported JSON Schema features vary by model. Start with a simple schema before adding nesting. Refusals, truncation and tool calls do not guarantee a final schema-valid text output.

Optional Chat Completions field
{
  "response_format": {
    "type": "json_schema",
    "json_schema": {
      "name": "summary",
      "strict": true,
      "schema": {
        "type": "object",
        "properties": {
          "summary": {
            "type": "string"
          }
        },
        "required": [
          "summary"
        ],
        "additionalProperties": false
      }
    }
  }
}
Even with strict mode, parse and validate the returned object in your application; never eval external data.

Related guides