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

# Python

> Use ARouter with the OpenAI, Anthropic, or Gemini Python SDK.

## OpenAI SDK

The [OpenAI Python SDK](https://github.com/openai/openai-python) works out of the box with ARouter.

### Installation

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

### Basic Usage

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

client = OpenAI(
    base_url="https://api.arouter.ai/v1",
    api_key="lr_live_xxxx",
)

response = client.chat.completions.create(
    model="openai/gpt-5.4",
    messages=[{"role": "user", "content": "Explain quantum computing in one sentence."}],
)
print(response.choices[0].message.content)
```

### Multi-Provider Routing

Switch providers by changing the model string:

```python theme={null}
# Anthropic via OpenAI SDK
response = client.chat.completions.create(
    model="anthropic/claude-sonnet-4.6",
    messages=[{"role": "user", "content": "Hello!"}],
)

# DeepSeek via OpenAI SDK
response = client.chat.completions.create(
    model="deepseek/deepseek-v3.2",
    messages=[{"role": "user", "content": "Hello!"}],
)

# Gemini via OpenAI SDK
response = client.chat.completions.create(
    model="google/gemini-2.5-flash",
    messages=[{"role": "user", "content": "Hello!"}],
)
```

### Streaming

```python theme={null}
stream = client.chat.completions.create(
    model="openai/gpt-5.4",
    messages=[{"role": "user", "content": "Write a haiku about code."}],
    stream=True,
)

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

### Async

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

client = AsyncOpenAI(
    base_url="https://api.arouter.ai/v1",
    api_key="lr_live_xxxx",
)

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

asyncio.run(main())
```

### Embeddings

```python theme={null}
response = client.embeddings.create(
    model="openai/text-embedding-3-small",
    input="The quick brown fox jumps over the lazy dog",
)
print(response.data[0].embedding[:5])
```

***

## Anthropic SDK

The [Anthropic Python SDK](https://github.com/anthropics/anthropic-sdk-python) works natively.

### Installation

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

### Basic Usage

```python theme={null}
import anthropic

client = anthropic.Anthropic(
    base_url="https://api.arouter.ai",
    api_key="lr_live_xxxx",
)

message = client.messages.create(
    model="claude-sonnet-4.6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}],
)
print(message.content[0].text)
```

<Warning>
  For the Anthropic SDK, set `base_url` to `https://api.arouter.ai` (without `/v1`).
  The SDK adds `/v1/messages` automatically.
</Warning>

### Streaming

```python theme={null}
with client.messages.stream(
    model="claude-sonnet-4.6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a story."}],
) as stream:
    for text in stream.text_stream:
        print(text, end="")
```

***

## Gemini SDK

The [Google Generative AI Python SDK](https://github.com/google/generative-ai-python) works by
configuring the API endpoint.

### Installation

```bash theme={null}
pip install google-generativeai
```

### Basic Usage

```python theme={null}
import google.generativeai as genai

genai.configure(
    api_key="lr_live_xxxx",
    transport="rest",
    client_options={"api_endpoint": "https://api.arouter.ai"},
)

model = genai.GenerativeModel("gemini-2.5-flash")
response = model.generate_content("Hello!")
print(response.text)
```

<Note>
  The `transport="rest"` parameter is required. The Gemini SDK defaults to gRPC,
  which is not supported by ARouter.
</Note>
