# Migrating from OpenAI (/docs/entwickler/migrate-from-openai)

> Move code from the OpenAI API to PeakPrivacy: what to change, what is dropped or missing, before-and-after code in Python and JavaScript and a checklist.

If your code uses the official OpenAI SDKs, moving to PeakPrivacy is mostly configuration: another base URL, a PeakPrivacy token and PeakPrivacy model ids.

All examples were run against the live API on 11.11.2026 with the Python SDK 3.49 and the JavaScript SDK 6.49.

## The three changes
1. **Base URL:** `https://api.peakprivacy.ch/v1/ai`
2. **API key:** a PeakPrivacy token, see [Authentication](/docs/entwickler/authentication#create).
3. **Model id:** an id from [`GET /v1/ai/models`](/docs/entwickler/models), for example `gpt-swiss`.

For plain chat completions, that is often all. If your code creates the client without arguments, environment variables are enough, because both SDKs read them:

```bash
export OPENAI_BASE_URL="https://api.peakprivacy.ch/v1/ai"
export OPENAI_API_KEY="$PEAKPRIVACY_API_KEY"
```

Then change the model id in your code.

## Before and after
A ticket summary, first against OpenAI, then against PeakPrivacy. Comments 1 to 3 mark the [three changes](/docs/entwickler/migrate-from-openai#short). Comments 4 and 5 are needed because the OpenAI code uses the `developer` role and `max_completion_tokens`, see [What behaves differently](/docs/entwickler/migrate-from-openai#differences).

**Python**

**Before (OpenAI)**

```python
from openai import OpenAI

client = OpenAI()  # reads OPENAI_API_KEY

TICKET = "Nadia (Accounting): Since this morning the scanner on floor 2 sends PDFs without the last page. Restarting didn't help. She needs it for the month-end closing on Friday."

completion = client.chat.completions.create(
    model="gpt-4.1-mini",
    messages=[
        {"role": "developer", "content": "You summarise IT support tickets in two sentences."},
        {"role": "user", "content": TICKET},
    ],
    max_completion_tokens=300,
)
print(completion.choices[0].message.content)
```

**After (PeakPrivacy)**

```python
import os

from openai import OpenAI

client = OpenAI(
    base_url="https://api.peakprivacy.ch/v1/ai",  # 1. PeakPrivacy base URL
    api_key=os.environ["PEAKPRIVACY_API_KEY"],    # 2. PeakPrivacy token
)

TICKET = "Nadia (Accounting): Since this morning the scanner on floor 2 sends PDFs without the last page. Restarting didn't help. She needs it for the month-end closing on Friday."

completion = client.chat.completions.create(
    model="gpt-swiss",  # 3. PeakPrivacy model id
    messages=[
        {"role": "system", "content": "You summarise IT support tickets in two sentences."},  # 4. system, not developer
        {"role": "user", "content": TICKET},
    ],
    max_tokens=300,  # 5. max_tokens, not max_completion_tokens
)
print(completion.choices[0].message.content)
```

**JavaScript**

**Before (OpenAI)**

```js
import OpenAI from "openai";

const client = new OpenAI(); // reads OPENAI_API_KEY

const TICKET = "Nadia (Accounting): Since this morning the scanner on floor 2 sends PDFs without the last page. Restarting didn't help. She needs it for the month-end closing on Friday.";

const completion = await client.chat.completions.create({
  model: "gpt-4.1-mini",
  messages: [
    { role: "developer", content: "You summarise IT support tickets in two sentences." },
    { role: "user", content: TICKET },
  ],
  max_completion_tokens: 300,
});
console.log(completion.choices[0].message.content);
```

**After (PeakPrivacy)**

```js
import OpenAI from "openai";

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

const TICKET = "Nadia (Accounting): Since this morning the scanner on floor 2 sends PDFs without the last page. Restarting didn't help. She needs it for the month-end closing on Friday.";

const completion = await client.chat.completions.create({
  model: "gpt-swiss", // 3. PeakPrivacy model id
  messages: [
    { role: "system", content: "You summarise IT support tickets in two sentences." }, // 4. system, not developer
    { role: "user", content: TICKET },
  ],
  max_tokens: 300, // 5. max_tokens, not max_completion_tokens
});
console.log(completion.choices[0].message.content);
```

Output of the PeakPrivacy version:

```text
Nadia from Accounting reports that the scanner on floor 2 has been generating PDFs that are missing the final page since this morning, and a restart did not resolve the problem. She requires a fix before Friday to complete month‑end closing.
```

## What behaves differently
PeakPrivacy passes on only the fields listed in [Request body](/docs/entwickler/chat-completions#fields). Everything else is dropped without an error, so code that relies on it keeps running but behaves differently.

| With OpenAI | With PeakPrivacy | What to do |
|---|---|---|
| `developer` role | Accepted by some models. `mistral-small-swiss` returned `500`. | Send instructions as `system`. |
| `max_completion_tokens` | Dropped. | Use `max_tokens`. Only some models apply it: in our tests `gpt-swiss` did; `gpt-4.1`, `deepseek-v4-flash` and `mistral-small-swiss` didn't. Don't rely on it to limit answer length. |
| `response_format`, `.parse()` helpers | `response_format` is dropped, so `.parse()` fails with a validation error. | Prompt for JSON and validate it, see [Getting JSON back](/docs/entwickler/json-output). |
| `n`, `stop`, `seed`, `logprobs`, `tool_choice`, `parallel_tool_calls`, `presence_penalty`, `frequency_penalty`, `user` | Dropped. With `n` above 1 you still get one choice. | Remove them. Cut at stop sequences in your own code. |
| `stream_options` | Not needed. | Every stream ends with a usage chunk, see [Streaming](/docs/entwickler/streaming#format-chat). |
| `previous_response_id`, `store` on `/responses` | Dropped. Each request stands alone. | Send the conversation in `input`, see [Conversations](/docs/entwickler/conversations#multi-turn). |
| `reasoning_effort` | Applied by some models only. | See [Reasoning](/docs/entwickler/reasoning#models). |
| Images in `messages` | Supported by models with `meta.capabilities.vision` set to `true`. In our test, `gpt-4.1` read an image sent as a data URL. | Choose the model by that flag, see [Choosing a model › Capabilities](/docs/entwickler/choose-a-model#capabilities). |
| `client.files` | `404`. PeakPrivacy has its own Files API. | Use plain HTTP, see [Files](/docs/entwickler/files). |
| Embeddings, images, audio, fine-tuning | No endpoints. The SDK calls fail with `404`. | Keep these with another provider, or drop them. |
| `client.models.retrieve(id)` | `404`. | Use `client.models.list()`. |
| Answer text | `ß` is replaced by `ss`. | Nothing, unless you compare strings. |

PeakPrivacy also adds a field: `anonymize` runs messages through the anonymiser first, see [Anonymisation](/docs/entwickler/anonymisation).

## Error handling
The SDK exception classes still work: `BadRequestError` for `400`, `AuthenticationError` for `401`, `RateLimitError` for `429`, and so on. The bodies inside have [PeakPrivacy shapes](/docs/entwickler/errors), and the two SDKs read them differently.

**Python**

```python
import os

import openai
from openai import OpenAI

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

try:
    client.chat.completions.create(
        model="gpt-4.1-mini",  # an OpenAI id that PeakPrivacy doesn't offer
        messages=[{"role": "user", "content": "Hello"}],
    )
except openai.APIStatusError as error:
    print(error.status_code, error.response.json())
```

```text
400 {'message': 'Validation error', 'errors': {'model': ['The selected model is invalid.']}}
```

`error.response.json()` returns the full body, including `trace_id` on `500` errors. `error.body` can hold only part of it.

**JavaScript**

```js
import OpenAI from "openai";

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

try {
  await client.chat.completions.create({
    model: "gpt-4.1-mini", // an OpenAI id that PeakPrivacy doesn't offer
    messages: [{ role: "user", content: "Hello" }],
  });
} catch (error) {
  if (!(error instanceof OpenAI.APIError)) throw error;
  console.log(error.status, error.message);
}
```

```text
400 400 status code (no body)
```

The JavaScript SDK only reads a top-level `error` field. Validation details and `trace_id` don't reach the exception. To see them, send the same request with curl.

## Pick a model
PeakPrivacy model ids aren't one-to-one equivalents of OpenAI models. Use this table to decide what to try first, then test with your own prompts. Security levels and the full list are on [Models](/docs/entwickler/models#tiers).

| If you used | Try first | Why |
|---|---|---|
| `gpt-4o`, `gpt-4.1`, `gpt-5` | The same id, [Extern] | Available under the same name. Before you send personal data, test a model with the [Schweiz] level. With `tools` on chat completions, `gpt-5` returned `500` in our tests unless the request had `"reasoning_effort": "none"`, see [Tool calling › Which models support it](/docs/entwickler/tool-calling#models). |
| Small, fast models such as `gpt-4.1-mini` or `gpt-5-mini` | `gpt-swiss` or `mistral-small-swiss`, both [Schweiz] | `gpt-swiss` answered in one to three seconds in our tests. `mistral-small-swiss` doesn't reason and streams cleanly. |
| Reasoning models such as `o3` or `o4-mini` | `gpt-swiss` with `reasoning_effort`, or `gpt-5` on `/responses` | See [Reasoning › Models](/docs/entwickler/reasoning#models). |
| Image input | A model with `meta.capabilities.vision` set to `true` | See [Choosing a model › Capabilities](/docs/entwickler/choose-a-model#capabilities). |

Check at startup that the ids you use are still listed:

```python
import os

from openai import OpenAI

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

REQUIRED = {"gpt-swiss", "deepseek-v4-flash"}
missing = REQUIRED - {model.id for model in client.models.list()}
if missing:
    raise RuntimeError(f"Models not available: {sorted(missing)}")
print("All models available")
```

## Checklist
- [ ] Base URL and token set, in code or through `OPENAI_BASE_URL` and `OPENAI_API_KEY`.
- [ ] Model ids replaced and checked against `client.models.list()` at startup.
- [ ] Security level of each model checked against the data you send, see [Security levels](/docs/sicherheit/sicherheitsstufen) (German).
- [ ] `developer` messages sent as `system`.
- [ ] `max_completion_tokens` replaced by `max_tokens`, and no length limit that depends on it.
- [ ] `response_format` and `.parse()` replaced by a prompt and validation.
- [ ] Dropped fields such as `n`, `stop`, `seed` and `tool_choice` removed.
- [ ] Stream consumers read `usage` from the last chunk and check `finish_reason`.
- [ ] Responses calls send the history in `input` instead of `previous_response_id`.
- [ ] Uploads moved to the PeakPrivacy Files API. Embeddings, images and audio handled elsewhere.
- [ ] Error handling logs the status instead of parsing OpenAI error objects. In Python it also logs the `trace_id` from `error.response.json()` (non-streamed requests).

## See also
- [API quickstart › Differences from the OpenAI API](/docs/entwickler#differences)
- [Chat completions](/docs/entwickler/chat-completions)
- [Getting JSON back](/docs/entwickler/json-output)
- [Errors and limits](/docs/entwickler/errors)
