OpenAI SDK integration

Use the official OpenAI SDK for Python or Node.js with Modellane: set the base URL and API key, then call Chat Completions or Responses.

The official OpenAI SDKs work with Modellane unchanged. Set two options, the base URL and the API key, and keep the rest of your code.

The official OpenAI SDKs work without changes, with both the Chat Completions API (POST /v1/chat/completions) and the Responses API (POST /v1/responses). Set the base URL and the API key; every other part of your code stays the same.

Install#

Python

pip install openai

Node.js

npm install openai

Configure the client#

Keep the key in an environment variable and pass the Modellane base URL when you create the client.

Python

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["MODELLANE_API_KEY"],
    base_url="https://usemodellane.com/v1",
)

response = client.chat.completions.create(
    model="lane-1",
    messages=[{"role": "user", "content": "Write a haiku about the sea."}],
)
print(response.choices[0].message.content)

Node.js

import OpenAI from "openai"

const client = new OpenAI({
  apiKey: process.env.MODELLANE_API_KEY,
  baseURL: "https://usemodellane.com/v1",
})

const response = await client.chat.completions.create({
  model: "lane-1",
  messages: [{ role: "user", content: "Write a haiku about the sea." }],
})
console.log(response.choices[0].message.content)

Streaming#

Python

stream = client.chat.completions.create(
    model="lane-1",
    messages=[{"role": "user", "content": "Count to five."}],
    stream=True,
    stream_options={"include_usage": True},
)
for chunk in stream:
    if chunk.choices:
        print(chunk.choices[0].delta.content or "", end="", flush=True)

Node.js

const stream = await client.chat.completions.create({
  model: "lane-1",
  messages: [{ role: "user", content: "Count to five." }],
  stream: true,
  stream_options: { include_usage: true },
})
for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "")
}

Responses API#

The same client also works with the Responses API (client.responses), on the same models and prices. Responses are not stored, so pass the whole conversation in input instead of previous_response_id.

Python

response = client.responses.create(
    model="lane-1",
    instructions="You are a helpful assistant.",
    input="Write a haiku about the sea.",
)
print(response.output_text)

Node.js

const response = await client.responses.create({
  model: "lane-1",
  instructions: "You are a helpful assistant.",
  input: "Write a haiku about the sea.",
})
console.log(response.output_text)

See the Responses API guide for tools, streaming and the fields that are not supported.

Verify#

Run the first example. A printed reply means the base URL, key and model id are right; 401 invalid_api_key points at the key, and 404 model_not_found at the model id.

What is supported#

Use Chat Completions (client.chat.completions), Responses (client.responses.create) and the model list (client.models.list()). Other OpenAI endpoints, such as stored responses, Assistants, embeddings and audio, are not available. Request fields are listed on Chat completions and Responses; retries and timeouts are covered in Rate limits.

Images#