# Text & tools

Use Chat Completions for text, supported image input and function calling. Your application runs the functions.

`POST /v1/chat/completions`

`POST /v1/completions`

## Request fields

Send model and messages. The supported optional fields are temperature, top_p, frequency_penalty, presence_penalty, stop, reasoning_effort, response_format, max_tokens or max_completion_tokens, stream, stream_options, tools, tool_choice, parallel_tool_calls and n.

- Use only one output-limit field. The default is 1,024. The accepted range is 1–131,072, within the model’s limits. n must be 1.
- Send 1–1,024 messages using system, developer, user, assistant or tool roles. stop accepts one string or up to eight strings, each up to 200 characters. Fields such as logprobs and seed are rejected.
- For a model that supports image_input, include image_url blocks in user messages. Use HTTPS URLs or base64 PNG, JPEG, GIF or WebP data URLs. Each request allows up to 16 images and 12 MiB of URL or data text. Serialized messages excluding image URLs must fit 2 MiB, including optional assistant reasoning_content. Serialized tool definitions must fit 512 KiB.

## Stream a response

Set stream to true and include stream_options.include_usage if you need the final usage chunk. Read data frames until [DONE]. Read content as answer text and reasoning_content as separate reasoning. Deltas can also contain refusal content and function arguments.

- An interrupted stream is incomplete even if some text arrived. Final usage may be missing. Request status is the recovery path.
- Check for an error frame after HTTP 200. Do not mark the response successful just because headers arrived.
- JSON responses and final usage chunks can include usage.prompt_tokens_details.cached_tokens and usage.completion_tokens_details.reasoning_tokens when reported. These counts are included in their respective token totals. Use request status for the confirmed charge.

### Streaming cURL

```text
curl --fail-with-body --no-buffer "https://urouter.net/gateway/v1/chat/completions" \
  -H "Authorization: Bearer $UROUTER_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
  "model": "YOUR_MODEL_ID",
  "messages": [
    {
      "role": "user",
      "content": "Explain DNS briefly."
    }
  ],
  "max_tokens": 256,
  "stream": true,
  "stream_options": {
    "include_usage": true
  }
}'
```

## Request structured output

Set response_format.type to text for the default text response, json_object for a JSON object or json_schema to supply a schema. Check the model's supported features and parameters in the catalog before requesting a JSON format.

- json_schema requires structured_outputs. json_object requires structured_outputs, json_mode or support for the response_format parameter.
- Supply json_schema.name (1–64 letters, digits, underscores or hyphens) and a schema object. description is optional and limited to 1,024 characters. strict accepts a boolean. The schema may use up to 64 KiB, 4,096 nodes and 32 levels of nesting.

### JSON Schema request field

```text
{
  "response_format": {
    "type": "json_schema",
    "json_schema": {
      "name": "status",
      "schema": {
        "type": "object",
        "properties": { "ready": { "type": "boolean" } },
        "required": ["ready"],
        "additionalProperties": false
      },
      "strict": true
    }
  }
}
```

## Run a function call

Declare function tools and choose a model with tools support. Validate each returned name and JSON argument string before executing it. Append the assistant’s tool_calls and a tool reply for each call ID, then request the next turn. Preserve any returned reasoning_content on that assistant message.

- tool_choice supports auto, none, required or a named function. Function names must be unique. Up to 128 function definitions are accepted.
- Finish all pending tool replies before the next user or assistant message. Tool results use role: tool and the matching tool_call_id.
- Function calling does not grant access to uRouter chat tools. Hosted web search, code execution and automatic function execution are not part of this API contract.

### Function definition

```text
{
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "Read current weather for a city.",
        "parameters": {
          "type": "object",
          "properties": {
            "city": {
              "type": "string"
            }
          },
          "required": [
            "city"
          ],
          "additionalProperties": false
        }
      }
    }
  ],
  "tool_choice": "auto"
}
```

## Legacy Completions

POST /v1/completions accepts a single string prompt. It supports max_tokens, sampling fields, n: 1, stream and stream_options. Its default output limit is 16 tokens. Prompt arrays, echo, best_of, stop and logprobs are unsupported.
