OpenAI Compatible API Guide
OpenAI Compatible API: The Universal Interface Standard
The OpenAI Compatible API has become the de facto standard interface for AI language model APIs. Dozens of providers, open-source projects, and relay services implement this same interface, allowing you to switch between models and providers without changing your application code. This guide explains the standard, who implements it, and how to take advantage of compatibility.
What Is the OpenAI Compatible API?
The OpenAI Compatible API refers to any service that accepts the same request format and returns the same response format as the OpenAI /v1/chat/completions endpoint. The core interface includes:
- POST /v1/chat/completions — Chat and text generation
- POST /v1/embeddings — Text embedding generation
- GET /v1/models — List available models
- POST /v1/images/generations — Image generation
The key benefit is portability: code written for OpenAI works with any compatible provider by simply changing the base URL.
Providers Implementing OpenAI Compatible API
- claude4u.com — Multi-provider relay supporting OpenAI, Claude, Gemini models
- Azure OpenAI — Microsoft's hosted OpenAI models with enterprise features
- Together AI — Open-source model hosting with OpenAI compatibility
- Groq — Ultra-fast inference with OpenAI-compatible interface
- Fireworks AI — High-performance model hosting
- Anyscale — Scalable model serving platform
- vLLM — Open-source inference server with OpenAI API
- Ollama — Local model running with OpenAI compatibility
- LM Studio — Desktop app for running local models
- LiteLLM — Python proxy supporting 100+ LLM providers
Switching Providers with One Line
from openai import OpenAI
# OpenAI direct
client = OpenAI(api_key="sk-openai-key")
# Switch to claude4u.com relay (supports multiple providers)
client = OpenAI(
api_key="your-relay-key",
base_url="https://claude4u.com/v1"
)
# Switch to local Ollama
client = OpenAI(
api_key="ollama",
base_url="http://localhost:11434/v1"
)
# Same code works with all providers
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
The Standard Request Format
{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is 2+2?"}
],
"temperature": 0.7,
"max_tokens": 1000,
"stream": false
}
The Standard Response Format
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1700000000,
"model": "gpt-4o",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "2+2 equals 4."
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 8,
"total_tokens": 28
}
}
Building Provider-Agnostic Applications
import os
from openai import OpenAI
def create_client():
"""Create an OpenAI-compatible client from environment variables."""
return OpenAI(
api_key=os.environ["API_KEY"],
base_url=os.environ.get("API_BASE_URL", "https://claude4u.com/v1")
)
def generate_response(client, model, messages, **kwargs):
"""Provider-agnostic generation function."""
return client.chat.completions.create(
model=model,
messages=messages,
**kwargs
)
# Usage - switch providers via environment variables
client = create_client()
response = generate_response(
client,
model=os.environ.get("MODEL", "gpt-4o"),
messages=[{"role": "user", "content": "Hello!"}]
)
Node.js Provider-Agnostic Setup
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.API_KEY,
baseURL: process.env.API_BASE_URL || 'https://claude4u.com/v1'
});
// Works with any OpenAI-compatible provider
async function chat(userMessage) {
const response = await client.chat.completions.create({
model: process.env.MODEL || 'gpt-4o',
messages: [{ role: 'user', content: userMessage }]
});
return response.choices[0].message.content;
}
Feature Compatibility Matrix
Not all providers support every feature. Here is what to check:
- Streaming — Supported by most providers
- Function calling / Tools — Supported by major providers, some open-source models may not support it
- Vision (image input) — Requires multimodal models
- JSON mode — Available in newer models and most relay services
- Embeddings — Requires separate embedding models
- Image generation — Only available from providers hosting DALL-E or equivalent models
Using with Popular Frameworks
# LangChain
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="gpt-4o",
base_url="https://claude4u.com/v1",
api_key="your-relay-key"
)
# AutoGen
config_list = [{
"model": "gpt-4o",
"api_key": "your-relay-key",
"base_url": "https://claude4u.com/v1"
}]
Get Started with 轻舟 AI
Stable, fast AI API relay — supports Claude, OpenAI, Gemini and more
Sign Up Free
轻舟 AI