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

# Python SDK

> Use Yelu AI API with the official OpenAI Python SDK for chat, responses, streaming, and error handling.

The official OpenAI Python SDK works with Yelu by changing the API key and base URL.

## Install

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
python -m pip install --upgrade openai
```

## Configure the client

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["YELU_API_KEY"],
    base_url="https://api.yelu.ai/v1",
    timeout=60.0,
    max_retries=2,
)
```

## Chat Completions

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
completion = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "Be concise."},
        {"role": "user", "content": "Explain vector search."},
    ],
    max_completion_tokens=200,
)
print(completion.choices[0].message.content)
print(completion.usage)
```

## Responses

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
response = client.responses.create(
    model="gpt-4o-mini",
    instructions="Answer for a software engineer.",
    input="What is backpressure?",
    max_output_tokens=200,
)
print(response.output_text)
```

## Stream output

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
stream = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Write a short release note."}],
    stream=True,
)
for chunk in stream:
    text = chunk.choices[0].delta.content or ""
    print(text, end="", flush=True)
```

## Handle errors

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import openai

try:
    client.models.list()
except openai.AuthenticationError as exc:
    print("Check YELU_API_KEY", exc.status_code)
except openai.RateLimitError as exc:
    print("Back off before retrying", exc.status_code)
except openai.APIConnectionError as exc:
    print("Network or TLS failure", exc.__cause__)
except openai.APIStatusError as exc:
    print("Yelu returned", exc.status_code, exc.response.text)
```

<Tip>
  Keep the Yelu base URL explicit when the same application also calls OpenAI directly. This prevents an environment-variable change from routing requests to the wrong service.
</Tip>

## Async client

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import asyncio
import os
from openai import AsyncOpenAI

client = AsyncOpenAI(
    api_key=os.environ["YELU_API_KEY"],
    base_url="https://api.yelu.ai/v1",
)

async def main():
    response = await client.responses.create(
        model="gpt-4o-mini",
        input="Give one practical API reliability tip.",
    )
    print(response.output_text)

asyncio.run(main())
```

See the [Chat example](/en/examples/chat), [Streaming guide](/en/guides/streaming), and [Errors](/en/errors) for protocol-level behavior.
