Ollama can act as an OpenAI-compatible server, which means the official OpenAI Python client can send chat requests to a local Ollama instance or to Ollama’s cloud API, according to Ollama’s OpenAI compatibility documentation. For local use, point the client at http://localhost:11434/v1/ and pass any non-empty API key value; Ollama ignores it. For cloud use, point the client at https://ollama.com/v1 and set OLLAMA_API_KEY.
This guide is for Python developers who want to use familiar OpenAI SDK code with Ollama. It covers installation, local and cloud connections, a stateless chat example, the stateless Responses endpoint, and common setup problems. The technical details come from Ollama’s official OpenAI compatibility documentation and blog post; this is a documentation-based guide, not a benchmark or a report of a private test run.
What Ollama’s OpenAI compatibility actually provides
Ollama supports a subset of the OpenAI API. That subset includes the chat completions endpoint used by the OpenAI Python library, and the documentation shows both local and cloud usage. The local server example uses /v1/chat/completions at http://localhost:11434. The cloud example uses https://ollama.com/v1 and does not require a local Ollama installation.
The compatibility layer is useful when you already have Python code, tooling, or abstractions built around the OpenAI client. Instead of rewriting request logic, you change the base_url, provide an API key value, and choose an Ollama model. The tradeoff is that compatibility is not complete. Ollama’s documentation states that it supports a subset of the OpenAI API, and the cloud API specifically does not support stateful Responses, built-in web search through /v1/responses, or custom/freeform tool-call replay.
Two terms are easy to confuse. The OpenAI-compatible API is the HTTP surface that accepts OpenAI-style requests. The OpenAI Python client is the library you install with pip install openai. Ollama is not the OpenAI service; it implements enough of the request and response shape for compatible clients to work.
Prerequisites and environment choices
Before writing code, decide whether you are connecting to a local Ollama server or to Ollama’s cloud API. The requirements differ.
- Local server: Ollama must be installed and running on the machine, and the model you name in the request must be available locally. Ollama’s blog example uses
ollama pull llama2before calling the API. - Cloud API: No Ollama installation is required. You need an
OLLAMA_API_KEYenvironment variable and a cloud model name such asgemma4:31bfrom the documentation example. - Python client: Install the OpenAI Python package with
pip install openai. The same client is used in both local and cloud examples.
A local setup keeps prompts and responses on your machine, assuming you are not routing them elsewhere. A cloud setup removes local hardware requirements but depends on network access, an API key, and the cloud service’s supported feature set. Choose based on privacy, hardware, and whether the model you need exists locally or in the cloud.
Step 1: Install the OpenAI Python client
Use the standard OpenAI Python package. Ollama’s documentation installs it with:
pip install openaiAfter installation, import OpenAI from the package. The examples below use only the client library and Python’s os module for the cloud API key.
Step 2: Connect to a local Ollama server
For a local server, create the client with the local base URL. The API key argument is required by the OpenAI client, but Ollama ignores its value. Ollama’s documentation uses ollama as a placeholder.
from openai import OpenAI
client = OpenAI(
base_url='http://localhost:11434/v1/',
api_key='ollama',
)
chat_completion = client.chat.completions.create(
messages=[
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': 'Say this is a test'},
],
model='gpt-oss:20b',
)
print(chat_completion.choices[0].message.content)Replace gpt-oss:20b with a model you have pulled. The older Ollama blog example uses llama2 after running ollama pull llama2. The model string must match an available local model, or the request will fail.
A successful run prints the assistant message text. If you see a connection error, check that the Ollama server is running and that the host and port match http://localhost:11434. If the model is missing, pull it or change the model argument. The local API key can be any non-empty string; it is not used for authentication by Ollama.
Step 3: Connect to Ollama’s cloud API
The cloud API uses a different base URL and a real API key. Set the key in your environment rather than hard-coding it in a script.
import os
from openai import OpenAI
client = OpenAI(
base_url='https://ollama.com/v1',
api_key=os.environ['OLLAMA_API_KEY'],
)
response = client.chat.completions.create(
model='gemma4:31b',
messages=[
{'role': 'user', 'content': 'Say hello in one sentence.'},
],
)
print(response.choices[0].message.content)This example follows Ollama’s cloud documentation. It does not require a local Ollama installation. The model name gemma4:31b is the documentation’s example; use a cloud model that your account can access. If authentication fails, confirm that OLLAMA_API_KEY is set in the same shell or process that runs the Python script.
Cloud compatibility has explicit limits. According to Ollama’s documentation, the cloud API does not support stateful Responses, built-in web search through /v1/responses, or custom/freeform tool-call replay. Plan around those limits if your application depends on them.
Step 4: Keep context by resending the message history
Chat completion calls are stateless in the sense that the server does not automatically remember earlier turns for you. To maintain a conversation, include the previous messages in the messages array on each request. Ollama’s blog example shows a multi-turn message list with system, user, assistant, and user roles.
messages = [
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': 'Who won the world series in 2020?'},
{'role': 'assistant', 'content': 'The LA Dodgers won in 2020.'},
{'role': 'user', 'content': 'Where was it played?'},
]
chat_completion = client.chat.completions.create(
model='llama2',
messages=messages,
)
print(chat_completion.choices[0].message.content)This snippet is adapted from the documented example and is illustrative. It shows how a Python application can pass prior context back into the request. In a real chat loop, append the model’s response to the list before asking the next question, and trim old turns if the context grows too large for the model.
Step 5: Use stateless Responses requests in the cloud API
Ollama’s cloud documentation says the same OpenAI client supports stateless Responses requests. The request shape is shorter than a chat completion:
response = client.responses.create(
model='gemma4:31b',
input='Say hello in one sentence.',
)
print(response.output_text)This is a cloud API example. The documentation notes that stateful Responses are not supported, along with built-in web search through /v1/responses and custom/freeform tool-call replay. For local server usage, the documented examples focus on /v1/chat/completions. If you need the Responses API locally, verify it against your Ollama version and the current compatibility documentation before depending on it.
Troubleshooting common setup issues
Most early failures come from the connection target, the model name, or the API key. The table below maps symptoms to likely causes and checks. It is based on the documented local and cloud setup requirements, not on a specific error-message catalog.
| Symptom | Likely cause | Check or fix |
|---|---|---|
| Client cannot connect to the local server | Ollama is not running, or the base URL is wrong | Start Ollama and confirm the local endpoint is http://localhost:11434. Use base_url='http://localhost:11434/v1/' as shown in the docs. |
| Model not found or unavailable | The model string does not match a local model | Run ollama pull <model> for a local model, or change model to one that is available. The docs show llama2 and gpt-oss:20b in local examples. |
| Authentication error on the cloud API | OLLAMA_API_KEY is missing or invalid | Set the environment variable in the same shell or process that runs Python. Use base_url='https://ollama.com/v1'. |
| Local client complains about the API key | The OpenAI client requires a key argument even when Ollama ignores it | Pass any non-empty string, such as api_key='ollama'. |
| A feature from the OpenAI API is missing | Ollama supports a subset of the OpenAI API | Check the compatibility documentation. The cloud API does not support stateful Responses, built-in web search through /v1/responses, or custom/freeform tool-call replay. |
| The model forgets earlier turns | Chat completions do not automatically keep state | Resend the conversation history in the messages array on each request. |
Limitations, compatibility, and practical tradeoffs
The main limitation is coverage. Ollama’s documentation is explicit that the OpenAI compatibility layer supports a subset of the OpenAI API. That means code that uses only chat completions and simple message arrays is the safest starting point. More advanced OpenAI features may not exist, may behave differently, or may be unavailable in the cloud API.
Local and cloud usage also differ in operations. A local server gives you control over model files and data flow, but you must manage model pulls, disk space, memory, and the server process. A cloud connection avoids local installation and hardware constraints, but it requires an API key, network access, and acceptance of the cloud feature limits. Neither option is automatically better; the right choice depends on the application.
Model names are another practical detail. The documentation uses gpt-oss:20b and llama2 for local examples and gemma4:31b for cloud examples. These are examples, not guarantees that every model is available in every environment. Always verify the model string against your local pulls or cloud account.
Next steps
Start with the smallest possible script: install openai, connect to http://localhost:11434/v1/ with api_key='ollama', and send one user message to a model you have pulled. Once that prints a response, switch the base URL and API key to test the cloud path. After both paths work, add a message-history loop and error handling for connection, model, and authentication failures.
If you run into a problem, gather the details that make it reproducible: operating system, Ollama version, whether you are using local or cloud, the exact model string, the base URL, and the full error text with secrets removed. Sharing those details in the community helps others diagnose the issue without guessing.
FAQ
Do I need an API key for local Ollama?
The OpenAI Python client requires an api_key argument, but Ollama ignores its value. Use any non-empty string such as ollama.
Can I use the OpenAI Python client with Ollama?
Yes. Ollama documents OpenAI compatibility and shows the OpenAI Python client with both local and cloud base URLs.
Does Ollama support all OpenAI API features?
No. Ollama supports a subset. The cloud API does not support stateful Responses, built-in web search through /v1/responses, or custom/freeform tool-call replay.
How do I keep conversation context?
Chat completion requests do not automatically remember earlier turns. Resend the prior messages in the messages array.
Is the Responses API available locally?
The documented local examples use /v1/chat/completions. The cloud docs show stateless Responses requests. Verify local Responses support against your Ollama version and current compatibility docs.