---
title: "Python SDK"
description: "Using Liutong with the OpenAI Python SDK"
---

Liutong is fully compatible with the [OpenAI Python SDK](https://github.com/openai/openai-python). No special client library is needed — just point the base URL at Liutong.

## Installation

```bash
pip install openai
```

## Setup

```python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.liutong.llby.org/v1",
    api_key=os.environ.get("LIUTONG_API_KEY", "lt_your_api_key"),
)
```

## Chat completion

```python
response = client.chat.completions.create(
    model="crimson-falcon-4",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What are the benefits of Rust?"},
    ],
    temperature=0.7,
    max_tokens=512,
)

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

## Streaming

```python
stream = client.chat.completions.create(
    model="crimson-falcon-4",
    messages=[{"role": "user", "content": "Write a short poem about the moon."}],
    stream=True,
)

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

## Reasoning

```python
response = client.chat.completions.create(
    model="indigo-owl-4",
    messages=[
        {"role": "user", "content": "What is 2^100? Show your work."},
    ],
)

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

## Embeddings

```python
response = client.embeddings.create(
    model="jade-mole-4",
    input="The quick brown fox jumps over the lazy dog.",
)

embedding = response.data[0].embedding
print(f"Vector dimensions: {len(embedding)}")
```

## Error handling

```python
from openai import APIError, AuthenticationError

try:
    response = client.chat.completions.create(
        model="crimson-falcon-4",
        messages=[{"role": "user", "content": "Hello"}],
    )
except AuthenticationError:
    print("Invalid API key")
except APIError as e:
    print(f"API error: {e}")
```

## Async usage

```python
import asyncio
from openai import AsyncOpenAI

client = AsyncOpenAI(
    base_url="https://api.liutong.llby.org/v1",
    api_key="lt_your_api_key",
)

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

asyncio.run(main())
```
