Ollama's Python Library: Install, Chat, Stream, Embeddings and Error Handling
Note: Some future links on this site may be affiliate links. We never claim hands-on testing; guides are researched from the public sources listed in each article.
How we wrote this: A summary of the official ollama-python README on GitHub (linked below), checked October 29, 2026. Code is adapted from its examples. The model name
gemma4is the README's example; use any model you have pulled.
If you've been calling Ollama's local API with raw HTTP requests, the official Python library does the same thing with less code. It's maintained by the Ollama project and, per its README, supports Python 3.8 and newer.
Before you start
The README lists two prerequisites:
- Ollama is installed and running on the machine (see the setup guides for Mac, Windows or Linux).
- You've pulled a model, for example
ollama pull gemma4.
Then install the package:
pip install ollama
A first chat call
from ollama import chat
response = chat(
model='gemma4',
messages=[{'role': 'user', 'content': 'Why is the sky blue?'}],
)
print(response.message.content)
The README shows both styles of reading the result: dictionary access (response['message']['content']) or attributes (response.message.content).
Stream the answer as it's written
Long answers feel faster when they print as they're generated. Set stream=True and loop over the chunks:
from ollama import chat
stream = chat(
model='gemma4',
messages=[{'role': 'user', 'content': 'Why is the sky blue?'}],
stream=True,
)
for chunk in stream:
print(chunk['message']['content'], end='', flush=True)
Talk to Ollama on another machine
By default the library talks to the local server. To point it somewhere else, such as a Linux box on your network, create a Client:
from ollama import Client
client = Client(host='http://localhost:11434')
response = client.chat(
model='gemma4',
messages=[{'role': 'user', 'content': 'Why is the sky blue?'}],
)
Swap in the other machine's address. Extra keyword arguments (such as headers) are passed through to the underlying httpx.Client. Remember that Ollama only listens on 127.0.0.1 unless the server's OLLAMA_HOST is changed; see Ollama on Linux.
Async code
For asyncio apps there's AsyncClient, configured the same way:
import asyncio
from ollama import AsyncClient
async def main():
message = {'role': 'user', 'content': 'Why is the sky blue?'}
async for part in await AsyncClient().chat(
model='gemma4', messages=[message], stream=True
):
print(part['message']['content'], end='', flush=True)
asyncio.run(main())
Without stream=True, await AsyncClient().chat(...) returns the whole response at once.
The rest of the API
The README says the library mirrors the Ollama REST API. The calls you'll use most:
| Task | Call |
|---|---|
| One-off completion | ollama.generate(model='gemma4', prompt='...') |
| Embeddings (one or a batch) | ollama.embed(model='...', input='...') or input=['...', '...'] |
| List local models | ollama.list() |
| What's loaded right now | ollama.ps() |
| Model details | ollama.show('gemma4') |
| Download / remove | ollama.pull('gemma4'), ollama.delete('gemma4') |
| Make a variant | ollama.create(model='example', from_='gemma4', system='...') |
| Copy / publish | ollama.copy(...), ollama.push(...) |
Embeddings are the building block for search over your own files; the embeddings and semantic search guide explains which models to use. create with a system prompt does in code what a Modelfile does on the command line.
Handle errors
The library raises an error when a request returns an error status, or when one is detected mid-stream. The README's pattern pulls a missing model automatically:
import ollama
model = 'does-not-yet-exist'
try:
ollama.chat(model)
except ollama.ResponseError as e:
print('Error:', e.error)
if e.status_code == 404:
ollama.pull(model)
A note on cloud models
The README also documents Ollama's cloud models, which run on Ollama's servers rather than your computer and require signing in (ollama signin) or an API key from ollama.com. Those requests leave your machine. If the point of your setup is keeping data local, stick with models that run locally.
Next steps
- Get structured data back: structured outputs (JSON)
- Let a model call your functions: tool calling basics
- Prefer JavaScript? See Ollama's JavaScript library.