# Python

> Call DeepSeek, MiniMax and Kimi from Python with the official OpenAI library: install, set the base URL, stream answers and handle errors.

Source: https://srotgateway.com/docs/python/

Use the official `openai` package. Only the base URL and the key change.

```bash
pip install openai
export SROT_API_KEY="sg-..."
```

## Simple call

```python
import os
from openai import OpenAI

client = OpenAI(base_url="https://api.srotgateway.com/v1", api_key=os.environ["SROT_API_KEY"])

reply = client.chat.completions.create(
    model="deepseek-v4.1-flash-fast",
    messages=[
        {"role": "system", "content": "Answer in simple English."},
        {"role": "user", "content": "What is an API?"},
    ],
)
print(reply.choices[0].message.content)
print(reply.usage)  # tokens you are charged for
```

## Streaming

Streaming shows the answer as it is written and is the safest way to get long answers.

```python
stream = client.chat.completions.create(
    model="deepseek-v4.1-flash-fast",
    messages=[{"role": "user", "content": "Write a 500-word story."}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
```

## Thinking level

Models that can "think" before answering accept `reasoning_effort` (for example `low`, `high`). Higher levels use more output tokens.

```python
reply = client.chat.completions.create(
    model="deepseek-v4.1-flash-fast",
    messages=[{"role": "user", "content": "Solve: 17 × 23"}],
    reasoning_effort="low",
)
```

## Retries

If you get `429` or `503`, wait a couple of seconds and try again. The OpenAI library already retries some errors; you can raise it with `OpenAI(..., max_retries=4)`. See [Errors and limits](https://srotgateway.com/docs/errors-and-limits/).
