Ollama OpenAI Compatible API
Ollama ships an OpenAI compatible API under http://localhost:11434/v1. Many tools and libraries were written for the OpenAI Chat Completions format; by changing only the base URL you can point them at local models.
Chat Completions with curl
curl http://localhost:11434/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "llama3.2",
"messages": [{ "role": "user", "content": "Say hello in Spanish." }]
}'{
"id": "chatcmpl-123",
"object": "chat.completion",
"model": "llama3.2",
"choices": [{
"index": 0,
"message": { "role": "assistant", "content": "¡Hola!" },
"finish_reason": "stop"
}],
"usage": { "prompt_tokens": 30, "completion_tokens": 4, "total_tokens": 34 }
}
(Sample response.)Using the OpenAI Python SDK
An API key is required by the SDK but ignored by Ollama, so any string works.
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")
resp = client.chat.completions.create(
model="llama3.2",
messages=[{"role": "user", "content": "Give me one tip for clean code."}],
)
print(resp.choices[0].message.content)Use clear, descriptive names for variables and functions so the code explains itself. (Sample response.)
Using the OpenAI JavaScript SDK
import OpenAI from 'openai';
const client = new OpenAI({ baseURL: 'http://localhost:11434/v1', apiKey: 'ollama' });
const resp = await client.chat.completions.create({
model: 'llama3.2',
messages: [{ role: 'user', content: 'What is 12 * 8?' }],
});
console.log(resp.choices[0].message.content);12 * 8 = 96. (Sample response.)
Supported Endpoints
At the time of writing (2026) Ollama supports the most common OpenAI-style endpoints, including:
- /v1/chat/completions - chat, streaming, tools, JSON mode, images
- /v1/completions - plain text completion
- /v1/embeddings - vector embeddings
- /v1/models - list local models
Compatibility is good but not perfect: provider-specific features may be missing. When you need everything Ollama offers (keep_alive, raw options) use the native /api endpoints instead.
Frequently asked questions
Do I need an API key for Ollama?
No. The local server has no authentication. OpenAI SDKs require a key value, so pass any placeholder like "ollama".
Can I switch between OpenAI and Ollama easily?
Yes. If your code uses the Chat Completions format, change base_url and model name, ideally through environment variables.
Does the Ollama OpenAI endpoint support streaming?
Yes, set stream: true exactly as you would with the OpenAI API.