The official Python library

Ollama · Practical · 12 min

The library wraps the REST API with typed responses and less code, and is the quickest path from Python.

Install the official library with pip install ollama. It talks to the local server for you.

Chat:

from ollama import chat
response = chat(model="gemma4:e2b", messages=[{"role": "user", "content": "Hi!"}])
print(response.message.content)

The messages list holds dictionaries with role and content, exactly like the REST API. The response is an object: response.message.content is the text, and fields like response.eval_count hold statistics. (Dictionary-style access such as response["message"]["content"] also works.)

Streaming: pass stream=True and loop over the result; each chunk has a piece of text in chunk.message.content.

Settings: options={"temperature": 0, "num_ctx": 8192} sets model parameters; keep_alive="10m" controls how long the model stays loaded.

Other functions mirror the API: generate(model, prompt), embed(model, input), list(), ps(), pull(model), show(model), delete(model).

Another machine: create a Client(host="http://192.168.1.50:11434") and call the same methods on it (client.chat(...)). The OLLAMA_HOST environment variable also changes the default address.

Async code: AsyncClient offers the same methods for programs that handle many requests at once.

Errors raise ollama.ResponseError, which has .status_code and .error. A common one is 404 when the model isn't downloaded; you can catch it and call pull automatically.

Remember the API is stateless: append each reply to your messages list to keep a conversation going.

Example

# pip install ollama
import ollama
from ollama import Client, chat

messages = [{"role": "system", "content": "You are a concise assistant."},
            {"role": "user", "content": "Name two uses of a hash map."}]

reply = chat(model="gemma4:e2b", messages=messages, options={"temperature": 0})
print(reply.message.content)          # Caching lookups and counting items.
messages.append(reply.message)        # keep the conversation going

for chunk in chat(model="gemma4:e2b", stream=True,
                  messages=messages + [{"role": "user", "content": "One more?"}]):
    print(chunk.message.content, end="", flush=True)

try:
    chat(model="not-downloaded:1b", messages=messages)
except ollama.ResponseError as e:
    print("\nError:", e.status_code, e.error)    # Error: 404 model 'not-downloaded:1b' not found

remote = Client(host="http://192.168.1.50:11434")  # a server elsewhere on your network

Remember

  • chat(model, messages) returns response.message.content
  • stream=True, options={...} and keep_alive mirror the API
  • Client(host=...) for other servers; AsyncClient for async code

Watch out for

  • Forgetting to append the assistant's reply to messages, so the model loses track of the conversation.

Check yourself

  1. How do you use the Python library against Ollama running on another machine?

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

Official docs: github.com/ollama/ollama-python