Ollama Python Library

The Ollama Python library is the official, lightweight client for calling Ollama from Python 3.8+ code. It wraps the REST API with friendly functions like chat, generate, embed and list.

Install the Library

pip install ollama
Successfully installed ollama-0.x.x

Chat from Python

from ollama import chat

response = chat(
    model="llama3.2",
    messages=[{"role": "user", "content": "Why is the sky blue? One sentence."}],
)
print(response.message.content)
Sunlight scatters off air molecules, and blue light scatters the most, so the sky looks blue.
(Sample response.)

The response object exposes fields as attributes (response.message.content) and also supports dictionary access (response["message"]["content"]).

Streaming Responses

Pass stream=True to receive an iterator of chunks and print text as it is generated:

from ollama import chat

stream = chat(
    model="llama3.2",
    messages=[{"role": "user", "content": "Write a haiku about coffee."}],
    stream=True,
)
for chunk in stream:
    print(chunk.message.content, end="", flush=True)
Dark cup, rising steam
morning whispers through the pot
eyes open, mind wakes
(Sample response.)

Custom Client and Async

Use Client to connect to another host, or AsyncClient inside asyncio apps such as FastAPI:

import asyncio
from ollama import AsyncClient

async def main():
    client = AsyncClient(host="http://192.168.1.50:11434")
    resp = await client.chat(
        model="llama3.2",
        messages=[{"role": "user", "content": "Hi!"}],
    )
    print(resp.message.content)

asyncio.run(main())
Hello! How can I help you today?
(Sample response.)

Other Useful Functions

  • ollama.generate(model, prompt) - single-prompt completion
  • ollama.embed(model, input) - embeddings
  • ollama.list() / ollama.ps() - local and loaded models
  • ollama.pull(model) / ollama.delete(model) - manage models
  • Errors raise ollama.ResponseError with a status_code you can inspect

Frequently asked questions

Is the Ollama Python library the same as LangChain?

No. It is a thin official client for the Ollama API. LangChain and LlamaIndex are larger frameworks that can use Ollama underneath.

How do I connect the Python library to a remote Ollama server?

Create Client(host="http://server:11434") or set the OLLAMA_HOST environment variable.

Can I use the OpenAI Python SDK instead?

Yes, point it at http://localhost:11434/v1. The native library exposes Ollama-specific features more directly.