Sub topic 11
Using OpenAI-compatible clients against Ollama
Many tools and libraries speak the OpenAI API format. Ollama exposes a compatible endpoint, so you can point those tools at your local server instead of a hosted service. This chapter assumes you can call the REST API from the previous chapters and have a model such as llama3.2 pulled.
Why compatibility matters
Existing code written for a hosted service often needs only two changes to run locally: the base URL and the API key. Your prompts and data stay on your machine, and you can switch between local and hosted models by changing configuration.
Ollama serves the compatible API under /v1 on the same port as its native API.
| Setting | Value for Ollama |
|---|---|
| Base URL | `http://localhost:11434/v1` |
| API key | Any non-empty string, such as `ollama` (ignored) |
| Model | A local model name, such as `llama3.2` |
A first request
Install the client with pip install openai. Then run this script.
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")
result = client.chat.completions.create(
model="llama3.2",
messages=[{"role": "user", "content": "Give me one tip for writing clear commit messages."}],
)
print(result.choices[0].message.content)The client requires an API key and refuses to start without one. Ollama does not check it, so any placeholder works.
The reply has the OpenAI shape. The text is in choices[0].message.content, not in message.content as in Ollama's native /api/chat response.
How the request flows
Using other tools
Any tool that lets you set a base URL can use Ollama. Many read environment variables, so you can often avoid editing code.
export OPENAI_BASE_URL=http://localhost:11434/v1
export OPENAI_API_KEY=ollamaThe recent OpenAI Python client reads both variables, so OpenAI() with no arguments then targets Ollama.
Common errors
- Connection refused. The server is not running or is on another host. Check
http://localhost:11434in a browser or withcurl. - Model not found (404). The
modelvalue must match a name fromollama list. Runollama pullfirst. - Missing API key error. The client library needs some value, even though Ollama ignores it.
- Missing base URL path. Leaving off
/v1sends requests to routes Ollama does not serve. - Unsupported features. The compatible endpoint covers common chat, completion, and embedding calls, but not every OpenAI feature. Behavior can differ by model, so test the specific feature you need, such as tool calling or JSON output.
Settings such as num_ctx are not part of the OpenAI format. To change them, bake them into a model with a Modelfile, which a later chapter covers, and then request that model by name.
Exercise
Your script uses the OpenAI Python client and fails with a connection error, even though ollama run llama3.2 works in a terminal. Name two settings to check in the client.
Show answer
Check that the base URL is http://localhost:11434/v1, including the /v1 path and the correct port, and that the client is not still pointing at the default hosted address. Also confirm the API key is set to a non-empty placeholder and the model name matches ollama list.