# Chat Completions (OpenAI compatible)

Compatible with OpenAI `POST /v1/chat/completions`. With masking enabled, detected PII fields in `messages` are replaced with placeholders before the model call. SSE streaming is supported with `stream: true`.

POST `/v1/chat/completions`

## Request body

required `application/json`

`model` string required

Target model identifier (defined in the panel).

`messages` ChatMessage[] required

min items 1

Array of `ChatMessage`

`role` string required

Allowed: `system` `user` `assistant` `tool`

`content` string required

Message text. With masking enabled, detected personal-data fields in all roles (`system`, `user`, `assistant`, `tool`) are replaced with placeholders inside the gateway.

`stream` boolean

If true, the response is returned as an SSE stream.

default: false

`temperature` number

min 0 · max 2 · default: 1

`max_tokens` integer

Maximum number of tokens to generate.

min 1

`top_p` number

min 0 · max 1

## Responses

200 Successful response

`id` string

`object` string

`created` integer

`model` string

`choices` ChatChoice[]

Array of `ChatChoice`

`index` integer

`finish_reason` string

`message` ChatMessage

`role` string required

Allowed: `system` `user` `assistant` `tool`

`content` string required

Message text. With masking enabled, detected personal-data fields in all roles (`system`, `user`, `assistant`, `tool`) are replaced with placeholders inside the gateway.

`usage` Usage

`prompt_tokens` integer

`completion_tokens` integer

`total_tokens` integer

401 Missing or invalid virtual key

`error` object

`message` string

`type` string

413 The input exceeds the masking limit. The request is not forwarded to the model; split the text and retry.

`error` object

`message` string

`type` string

429 Quota or rate limit exceeded

`error` object

`message` string

`type` string

502 The masking step could not run. The request is **fail-closed** and is not forwarded to the model; there is no "send it unmasked" option.

`error` object

`message` string

`type` string

### Request

#### openai-python

```
from openai import OpenAI

client = OpenAI(
    base_url="https://gw-tr.gurubase.io/v1",
    api_key="SANAL_ANAHTAR",
)

resp = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "user", "content": "Ayşe Demir'in talebini özetle."},
    ],
)
print(resp.choices[0].message.content)
```

#### python

```
import requests

response = requests.post(
    "https://gw-tr.gurubase.io/v1/chat/completions",
    headers={
        "Content-Type": "application/json"
    },
    json={
  "model": "gpt-4o-mini",
  "messages": [
    {
      "role": "system",
      "content": "string"
    }
  ],
  "stream": False,
  "temperature": 1,
  "max_tokens": 0,
  "top_p": 0
},
)
```

#### curl

```
curl -X POST "https://gw-tr.gurubase.io/v1/chat/completions" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "gpt-4o-mini",
  "messages": [
    {
      "role": "system",
      "content": "string"
    }
  ],
  "stream": false,
  "temperature": 1,
  "max_tokens": 0,
  "top_p": 0
}'
```

#### js

```
const response = await fetch("https://gw-tr.gurubase.io/v1/chat/completions", {
  method: "POST",
  headers: {
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
  "model": "gpt-4o-mini",
  "messages": [
    {
      "role": "system",
      "content": "string"
    }
  ],
  "stream": false,
  "temperature": 1,
  "max_tokens": 0,
  "top_p": 0
})
});
```

### Response

#### 200

```
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1718000000,
  "model": "gpt-4o-mini",
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": {
        "role": "system",
        "content": "string"
      }
    }
  ],
  "usage": {
    "prompt_tokens": 0,
    "completion_tokens": 0,
    "total_tokens": 0
  }
}
```

#### 401

```
{
  "error": {
    "message": "Sanal anahtar gerekli veya geçersiz.",
    "type": "invalid_request_error"
  }
}
```

#### 413

```
{
  "error": {
    "message": "string",
    "type": "string"
  }
}
```

#### 429

```
{
  "error": {
    "message": "string",
    "type": "string"
  }
}
```

#### 502

```
{
  "error": {
    "message": "string",
    "type": "string"
  }
}
```
