> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kymaapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python

> Full Python integration guide with OpenAI SDK.

## Install

```bash theme={null}
pip install openai
```

## Setup

```python theme={null}
from openai import OpenAI

client = OpenAI(
    base_url="https://kymaapi.com/v1",
    api_key="kyma-your-api-key"
)
```

## Basic chat

```python theme={null}
response = client.chat.completions.create(
    model="qwen-3.6-plus",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Explain quantum computing simply."}
    ]
)

print(response.choices[0].message.content)
```

## Multi-turn conversation

```python theme={null}
messages = [
    {"role": "system", "content": "You are a Python tutor."}
]

while True:
    user_input = input("You: ")
    if user_input.lower() == "quit":
        break
    
    messages.append({"role": "user", "content": user_input})
    
    response = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=messages
    )
    
    reply = response.choices[0].message.content
    messages.append({"role": "assistant", "content": reply})
    print(f"AI: {reply}")
```

## Streaming

```python theme={null}
stream = client.chat.completions.create(
    model="llama-3.3-70b",
    messages=[{"role": "user", "content": "Write a haiku about coding"}],
    stream=True
)

for chunk in stream:
    content = chunk.choices[0].delta.content
    if content:
        print(content, end="", flush=True)
```

## Async

```python theme={null}
import asyncio
from openai import AsyncOpenAI

client = AsyncOpenAI(
    base_url="https://kymaapi.com/v1",
    api_key="kyma-your-api-key"
)

async def main():
    response = await client.chat.completions.create(
        model="qwen-3.6-plus",
        messages=[{"role": "user", "content": "Hello!"}]
    )
    print(response.choices[0].message.content)

asyncio.run(main())
```

## Parameters

```python theme={null}
response = client.chat.completions.create(
    model="llama-3.3-70b",
    messages=[{"role": "user", "content": "Be creative"}],
    temperature=0.9,      # 0-2, higher = more creative
    max_tokens=1000,       # max response length
    top_p=0.95,           # nucleus sampling
)
```

## Generating images

Image models use a separate async endpoint. POST to `/v1/images/generations`, get a job `id` back, poll `/v1/jobs/{id}` until status is `succeeded`. Use `requests` directly - the OpenAI SDK does not cover these endpoints.

```python theme={null}
import requests, time

API = "https://kymaapi.com/v1"
HEADERS = {"Authorization": "Bearer kyma-your-api-key"}

submit = requests.post(
    f"{API}/images/generations",
    headers=HEADERS,
    json={
        "model": "flux-2-pro",
        "prompt": "An aerial photo of a misty mountain range at sunrise",
        "size": "16:9",
    },
).json()

job_id = submit["id"]

while True:
    job = requests.get(f"{API}/jobs/{job_id}", headers=HEADERS).json()
    if job["status"] in ("succeeded", "failed", "expired", "refunded"):
        break
    time.sleep(3)

print(job["output"]["url"])
```

Other image models work the same way: swap the model (`recraft-v4`, `ideogram-v3`, or `flux-kontext-pro` with an `image_url`). `/v1/videos/generations` takes the same shape plus `duration_s`, with `kling-3-pro`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.