OpenAI SDK Drop-in in Ollama

From the Ollama cheat sheet ยท OpenAI Compatibility ยท verified Jul 2026

OpenAI SDK Drop-in

Point the OpenAI SDK at Ollama for local inference with no code changes.

python
# Python โ€” use OpenAI SDK with Ollama
from openai import OpenAI

client = OpenAI(
  base_url='http://localhost:11434/v1/',
  api_key='ollama',  # required but ignored
)

response = client.chat.completions.create(
  model='llama3.2',
  messages=[{'role': 'user', 'content': 'Hello!'}],
)
print(response.choices[0].message.content)
๐Ÿ’ก Just change base_url to localhost:11434/v1/ โ€” the API key is required but ignored
โšก Supports /v1/chat/completions, /v1/completions, /v1/embeddings, and /v1/models
๐Ÿ“Œ Use "ollama cp" to alias models to OpenAI names for zero-code-change migration
๐ŸŸข Great for testing apps locally before switching to cloud API providers
openaicompatibilitysdk
Back to the full Ollama cheat sheet