OpenAI-compatible endpoint

Ollama · Practical · 10 min

Many apps and libraries only speak the OpenAI API; compatibility lets them use local models unchanged.

Many tools and libraries were written for OpenAI's API. Ollama also offers an OpenAI-compatible API under http://localhost:11434/v1/, so you can point that code at Ollama by changing two settings:

  • base_url (the server address) to http://localhost:11434/v1/,
  • api_key to any non-empty string, such as "ollama". It is required by the client library but ignored by a local server.

Supported endpoints include:

  • /v1/chat/completions: chat, streaming, JSON mode and structured output, tools, and base64 images.
  • /v1/completions: plain text completions.
  • /v1/embeddings: embeddings.
  • /v1/models: list models.
  • /v1/responses: OpenAI's newer Responses API, without its stateful features (you send the full history yourself).

Limitations to know: some parameters aren't supported (for example logprobs, tool_choice, n), and images must be sent as base64 data rather than URLs. There is also no parameter for context length. To use a bigger context through this API, create a custom model with PARAMETER num_ctx in a Modelfile, or set OLLAMA_CONTEXT_LENGTH on the server.

Ollama also has an Anthropic-compatible endpoint for tools that use Anthropic's Messages API, and the same /v1 paths work against https://ollama.com/v1 for cloud models with an API key.

Native /api/... endpoints expose more Ollama-specific options (keep_alive, think, every model option), so prefer them, or the ollama library, when you don't need OpenAI compatibility.

Example

# pip install openai
from openai import OpenAI

client = OpenAI(base_url="http://localhost:11434/v1/", api_key="ollama")   # key is ignored locally

resp = client.chat.completions.create(
    model="gpt-oss:20b",
    messages=[{"role": "user", "content": "Say this is a test"}],
)
print(resp.choices[0].message.content)          # This is a test.

emb = client.embeddings.create(model="embeddinggemma", input=["hello", "world"])
print(len(emb.data), len(emb.data[0].embedding))   # 2 768

print([m.id for m in client.models.list().data])   # ['gpt-oss:20b', 'embeddinggemma:latest', ...]

Remember

  • Use base_url="http://localhost:11434/v1/" and a dummy api_key
  • Chat, completions, embeddings, models and (stateless) Responses are supported
  • No context-length parameter: use a Modelfile or OLLAMA_CONTEXT_LENGTH

Watch out for

  • Expecting num_ctx in an OpenAI-compatible request to change the context; set it via a Modelfile or the server instead.

Check yourself

  1. An app only supports the OpenAI API. What do you change to make it use a local Ollama model?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: docs.ollama.com/api/openai-compatibility