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

# Official OpenAI SDK

> Just change base_url in Python / Node.js / Go / Java to use OpenPAI

The official OpenAI SDK is designed to allow overriding `base_url`, so it is **fully compatible with OpenPAI** — no need to swap libraries.

## Python

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

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

client = OpenAI(
    api_key="sk-XXXXXXXXXXXXXXXX",
    base_url="https://openp.ai/v1",
)

# Chat
resp = client.chat.completions.create(
    model="gpt-5.5",
    messages=[{"role": "user", "content": "hello"}],
)

# Streaming
stream = client.chat.completions.create(
    model="gpt-5.5",
    messages=[{"role": "user", "content": "tell a story"}],
    stream=True,
)
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")

# Embeddings
emb = client.embeddings.create(
    model="text-embedding-3-small",
    input=["hello world"],
)

# Image generation
img = client.images.generate(
    model="dall-e-3",
    prompt="a panda",
    size="1024x1024",
)
```

## Node.js / TypeScript

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

```ts theme={null}
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.OPENPAI_API_KEY!,
  baseURL: "https://openp.ai/v1",
});

const resp = await client.chat.completions.create({
  model: "gpt-5.5",
  messages: [{ role: "user", content: "hello" }],
});
```

## Go

`github.com/sashabaranov/go-openai`:

```go theme={null}
import "github.com/sashabaranov/go-openai"

cfg := openai.DefaultConfig("sk-XXXXXXXXXXXXXXXX")
cfg.BaseURL = "https://openp.ai/v1"
client := openai.NewClientWithConfig(cfg)

resp, _ := client.CreateChatCompletion(ctx,
    openai.ChatCompletionRequest{
        Model: "gpt-5.5",
        Messages: []openai.ChatCompletionMessage{
            {Role: openai.ChatMessageRoleUser, Content: "hello"},
        },
    })
```

## Java

`com.openai:openai-java`:

```java theme={null}
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;

OpenAIClient client = OpenAIOkHttpClient.builder()
    .apiKey(System.getenv("OPENPAI_API_KEY"))
    .baseUrl("https://openp.ai/v1")
    .build();
```

## Environment variables

The OpenAI SDK reads the following environment variables by default — configuring via environment variables is recommended:

```bash theme={null}
export OPENAI_API_KEY=sk-XXXXXXXXXXXXXXXX
export OPENAI_BASE_URL=https://openp.ai/v1
```

Then your code needs no explicit arguments:

```python theme={null}
from openai import OpenAI
client = OpenAI()  # reads the environment variables automatically
```

## Notes

* **Timeouts**: long-context / reasoning models may require raising the client timeout to 5-10 minutes.
* **Retries**: OpenPAI already retries upstream failures at the gateway, so the client can disable its own retries (`max_retries=0`) or keep them as a fallback.
* **Beta features**: whether certain OpenAI Beta endpoints (Assistants API, Threads) are supported is subject to console announcements.
