# Chat completions (/docs/entwickler/chat-completions)

> Send a conversation to a model and get the answer as JSON or a stream, with anonymisation and the Responses endpoint.

Send a conversation to a model and get its next message, as one JSON response or as a stream.

```text
POST https://api.peakprivacy.ch/v1/ai/chat/completions
POST https://api.peakprivacy.ch/v1/ai/completions    (same endpoint)
```

## Example

**curl**

```bash
curl https://api.peakprivacy.ch/v1/ai/chat/completions \
  -H "Authorization: Bearer $PEAKPRIVACY_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{
    "model": "deepseek-v4-flash",
    "messages": [
      {"role": "system", "content": "You write short internal notes."},
      {"role": "user", "content": "Draft a two-line reminder about the expense report deadline."}
    ],
    "temperature": 0.3
  }'
```

**Python**

```python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.peakprivacy.ch/v1/ai",
    api_key=os.environ["PEAKPRIVACY_API_KEY"],
)

completion = client.chat.completions.create(
    model="deepseek-v4-flash",
    messages=[
        {"role": "system", "content": "You write short internal notes."},
        {"role": "user", "content": "Draft a two-line reminder about the expense report deadline."},
    ],
    temperature=0.3,
)
print(completion.choices[0].message.content)
```

**JavaScript**

```js
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.peakprivacy.ch/v1/ai",
  apiKey: process.env.PEAKPRIVACY_API_KEY,
});

const completion = await client.chat.completions.create({
  model: "deepseek-v4-flash",
  messages: [
    { role: "system", content: "You write short internal notes." },
    { role: "user", content: "Draft a two-line reminder about the expense report deadline." },
  ],
  temperature: 0.3,
});
console.log(completion.choices[0].message.content);
```

The Python and JavaScript snippets further down reuse this `client`.

## Request body
| Name | Type | Required | Description |
|---|---|---|---|
| `model` | string | Yes | Model id from [`GET /v1/ai/models`](/docs/entwickler/models). |
| `messages` | array | Yes | The conversation, oldest first. Each message has a `role` (`system`, `user`, `assistant` or `tool`) and `content`. Use `system`, not `developer`: some models return `500` for `developer`. See [Conversations](/docs/entwickler/conversations). |
| `stream` | boolean | No | `true` returns Server-Sent Events, see [Streaming](#streaming). Default `false`. |
| `temperature` | number | No | 0 to 2, at most two decimals. Default `0.7`. Some models ignore it or use a fixed value. |
| `top_p` | number | No | 0 to 1, at most two decimals. Some models ignore it. |
| `max_tokens` | integer | No | At least 1. Capped at the model's `max_tokens`. Most models ignore it; in our tests only `gpt-swiss` applied it. Don't rely on it to limit cost. |
| `tools` | array | No | Function definitions in the OpenAI format. Calls come back in `choices[].message.tool_calls`. Not every model supports tools: see [Tool calling](/docs/entwickler/tool-calling#models). |
| `reasoning` | object | No | `effort`: how much the model thinks first. Wins over `reasoning_effort`. Which models apply it: [Reasoning](/docs/entwickler/reasoning#models). Unsupported values are dropped without an error. |
| `reasoning_effort` | string | No | Same as `reasoning.effort`. |
| `anonymize` | boolean | No | PeakPrivacy field. Runs every message except `system` messages through the anonymiser and turns streaming off. Default `false`. See [Anonymisation](#anonymize). |

**Ignored fields.** `random_seed` (integer) and `safe_prompt` (boolean) are validated, then ignored by current models. `instructions` and `file_uuids` are passed on, but don't rely on them. Send instructions as a `system` message. Don't send `file_uuids`: the completions endpoints don't load uploaded files, and in our tests `deepseek-v4-flash` returned `500` with it. Send the text in `messages`, see [Use a file in a completion](/docs/entwickler/files#completions). Other fields, such as `stop`, `n`, `response_format`, `tool_choice`, `seed`, `presence_penalty`, `frequency_penalty`, `user` or `max_completion_tokens`, are dropped without an error. Use `max_tokens` instead of `max_completion_tokens`.

## Response
```json
{
  "id": "5cb77078-8a1a-4b7d-9b0d-1f2d7f5e0d3a",
  "object": "chat.completion",
  "created": 1781500000,
  "model": "deepseek-v4-flash",
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": { "role": "assistant", "content": "Reminder: please submit your expense report by Friday." }
    }
  ],
  "usage": { "prompt_tokens": 24, "completion_tokens": 18, "total_tokens": 42 }
}
```

| Field | Type | Description |
|---|---|---|
| `id` | string | Request id. Quote it when you contact PeakPrivacy about a request. |
| `model` | string | PeakPrivacy id of the model that answered. |
| `choices[].message` | object | The answer, with `role` and `content`, plus `tool_calls` if the model called a tool. `ß` in `content` is replaced by `ss`. |
| `choices[].finish_reason` | string | Why generation stopped, for example `stop`, `length` or `tool_calls`. |
| `usage` | object | `prompt_tokens`, `completion_tokens` and `total_tokens`. |

## Streaming
With `"stream": true`, the response is a stream of Server-Sent Events. [Streaming](/docs/entwickler/streaming) has complete consumers that detect an early end, handle errors and cancel a stream.

**curl**

```bash
curl --no-buffer https://api.peakprivacy.ch/v1/ai/chat/completions \
  -H "Authorization: Bearer $PEAKPRIVACY_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{
    "model": "mistral-small-swiss",
    "stream": true,
    "messages": [{"role": "user", "content": "Greet me in three different ways."}]
  }'
```

**Python**

```python
stream = client.chat.completions.create(
    model="mistral-small-swiss",
    messages=[{"role": "user", "content": "Greet me in three different ways."}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
```

**JavaScript**

```js
const stream = await client.chat.completions.create({
  model: "mistral-small-swiss",
  messages: [{ role: "user", content: "Greet me in three different ways." }],
  stream: true,
});
for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
```

Each event is a `data:` line with one `chat.completion.chunk`:

- Text arrives in `choices[].delta.content`. Reasoning models also send `delta.reasoning_content`. See [Reasoning › Streaming](/docs/entwickler/reasoning#streaming) for a current limitation.
- The chunk that ends the text has `finish_reason` set.
- Every chunk has `usage`. Depending on the model it shows zeros or running counts, so read it from the last chunk, whose `choices` array can be empty.
- `id` changes from chunk to chunk.
- `data: [DONE]` ends a complete stream.

```text
data: {"id":"...","object":"chat.completion.chunk","created":1781500000,"model":"mistral-small-swiss","choices":[{"index":0,"delta":{"role":"assistant","content":"Hello"},"finish_reason":null}],"usage":{"prompt_tokens":0,"completion_tokens":0,"total_tokens":0}}

…

data: {"id":"...","object":"chat.completion.chunk","created":1781500001,"model":"mistral-small-swiss","choices":[{"index":0,"delta":{"content":""},"finish_reason":"stop"}],"usage":{"prompt_tokens":0,"completion_tokens":0,"total_tokens":0}}

data: {"id":"...","object":"chat.completion.chunk","created":1781500001,"model":"mistral-small-swiss","choices":[],"usage":{"prompt_tokens":12,"completion_tokens":9,"total_tokens":21}}

data: [DONE]
```

Errors found before the stream starts come back as a normal JSON response with a `4xx` or `5xx` status, so check the status before you read the body as a stream. Failures after the first chunk arrive as an `event: error`, see [Errors during a stream](/docs/entwickler/errors#streaming).

## Anonymisation
With `"anonymize": true`, the PeakPrivacy anonymiser processes every message except `system` messages before the model sees them. Keep personal data out of the system prompt. [Anonymisation](/docs/entwickler/anonymisation) explains what the anonymiser replaces and where its limits are.

Such a request never streams: you get one JSON response even with `"stream": true`. An SDK call with streaming turned on then returns no chunks and no error, so leave `stream` off. The OpenAI SDKs don't know the field, so send it like this:

**curl**

Add the field to the JSON body:

```json
{
  "model": "deepseek-v4-flash",
  "anonymize": true,
  "messages": [{"role": "user", "content": "Summarise: Luca Meier asks Nadia for the Q3 figures by Friday."}]
}
```

**Python**

```python
completion = client.chat.completions.create(
    model="deepseek-v4-flash",
    messages=[{"role": "user", "content": "Summarise: Luca Meier asks Nadia for the Q3 figures by Friday."}],
    extra_body={"anonymize": True},
)
```

**JavaScript**

```js
const completion = await client.chat.completions.create({
  model: "deepseek-v4-flash",
  messages: [{ role: "user", content: "Summarise: Luca Meier asks Nadia for the Q3 figures by Friday." }],
  // @ts-expect-error PeakPrivacy field (only needed in TypeScript)
  anonymize: true,
});
```

The Responses endpoint doesn't anonymise its `input`.

## Responses endpoint
`POST /v1/ai/responses` returns an OpenAI Responses object instead of a chat completion. Models with `meta.capabilities.responses` set to `false` don't support it, see [Endpoint support](/docs/entwickler/models#capabilities).

| Name | Type | Required | Description |
|---|---|---|---|
| `model` | string | Yes | Model id. |
| `input` | string or array | Yes | The prompt as text, or an array of input items. |
| `instructions` | string | No | Instructions for the model, like a system message. |
| `max_output_tokens` | integer | No | At least 1. Behaves like `max_tokens`. |
| `temperature`, `top_p`, `stream`, `tools` | | No | As in [Request body](#fields). |
| `reasoning` | object | No | `effort` and `summary`. See [Reasoning](/docs/entwickler/reasoning#parameters). |

**curl**

```bash
curl https://api.peakprivacy.ch/v1/ai/responses \
  -H "Authorization: Bearer $PEAKPRIVACY_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{
    "model": "deepseek-v4-flash",
    "instructions": "Answer in one short paragraph.",
    "input": "List three principles of data protection."
  }'
```

**Python**

```python
response = client.responses.create(
    model="deepseek-v4-flash",
    instructions="Answer in one short paragraph.",
    input="List three principles of data protection.",
)
print(response.output_text)
```

**JavaScript**

```js
const response = await client.responses.create({
  model: "deepseek-v4-flash",
  instructions: "Answer in one short paragraph.",
  input: "List three principles of data protection.",
});
console.log(response.output_text);
```

The response is the model's Responses object, with `model` set to the PeakPrivacy model id. The text is in the `output` array, in content items of type `output_text`. With `"stream": true`, each event has an `event:` line with its type and a `data:` line. Text arrives in `response.output_text.delta` events, and a `response.completed` event ends the stream. There is no `data: [DONE]` line.

## Errors
- `400` for an unknown `model`, a `temperature` or `top_p` out of range or with more than two decimals, or a `max_tokens` below 1. See [400 Bad Request](/docs/entwickler/errors#400).
- `400` with code `unsupported_endpoint` if the model doesn't support the endpoint, also with `"stream": true`. See [Model doesn't support the endpoint](/docs/entwickler/errors#unsupported-endpoint).
- `400` or, depending on the model, `500` with `trace_id` if the model rejects the request, for example because the prompt exceeds its context window. See [The model rejects the request](/docs/entwickler/errors#model-rejected).
- `500` with `trace_id` if the model fails or takes longer than 120 seconds without streaming. See [Timeouts](/docs/entwickler/errors#timeouts).

## See also
- [Models](/docs/entwickler/models)
- [Streaming](/docs/entwickler/streaming)
- [Files](/docs/entwickler/files#completions)
- [Errors and limits](/docs/entwickler/errors)
