Home / Guides / Frameworks and SDKs

Switch from the OpenAI SDK

Updated 2026-10-18 · Python and Node.js examples, streaming and a migration checklist. Usually two lines change.

Connection details

Base URL (OpenAI compatible)
https://api.aifans.fan/v1
API key
Create one in the console; it starts with sk- (no account yet? sign up)
Recommended models
deepseek-v4.1-flash, deepseek-v4-pro (all models and prices)

If your code already uses the OpenAI SDK, switching to AIFans API takes two changes, the base URL and the API key, plus the model name. Requests, responses, streaming and tool calls use the same fields as OpenAI.

Python

# pip install openai
from openai import OpenAI

client = OpenAI(api_key="YOUR_API_KEY", base_url="https://api.aifans.fan/v1")
resp = client.chat.completions.create(
    model="deepseek-v4.1-flash",
    messages=[{"role": "user", "content": "Hello"}],
    max_tokens=1000,
)
print(resp.choices[0].message.content)

Node.js

// npm install openai
import OpenAI from "openai";

const client = new OpenAI({ apiKey: "YOUR_API_KEY", baseURL: "https://api.aifans.fan/v1" });
const resp = await client.chat.completions.create({
  model: "deepseek-v4.1-flash",
  messages: [{ role: "user", content: "Hello" }],
  max_tokens: 1000,
});
console.log(resp.choices[0].message.content);

Streaming

stream = client.chat.completions.create(
    model="deepseek-v4.1-flash", max_tokens=1000, stream=True,
    stream_options={"include_usage": True},   # token usage arrives with the last chunk
    messages=[{"role": "user", "content": "Write a short poem about spring"}])
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Migration checklist

  • Set the OPENAI_BASE_URL and OPENAI_API_KEY environment variables: many frameworks (LangChain, LlamaIndex and others) then need no code change.
  • Models differ. Before going live, compare quality and cost on your own prompts; the Playground in the console is a quick way to start.
  • Retry 429 and 502 with backoff; see the API reference.
  • Give production keys a daily spending limit and an IP allow-list.

Related guides