opensourceprojects.dev

A broadsheet for software that doesn't ask for your email

A Python client for Ollama, sync or async, local or cloud
GitHub RepoViews9

Project Description

View on GitHub

Getting Your Python Code Talking to Local and Cloud LLMs Without the Boilerplate

You've got Ollama running on your machine, a model pulled and ready, and now you want to actually use it from Python. You could hand-roll HTTP requests against the REST API, but that gets old fast once you're dealing with streaming, async, and response parsing. The Ollama Python library is the official client that handles that plumbing for you, and it works with Python 3.8 and up.

What It Does

The Ollama Python library is a thin client that wraps the Ollama REST API in idiomatic Python. You import chat, point it at a model, hand it a list of messages, and get back a typed response object. That's the core loop. The library ships both a synchronous Client and an AsyncClient, and both can be configured with a custom host and headers.

It also covers the cloud side of things. You can either run larger cloud models through your local Ollama instance (after a one-time ollama signin and a ollama pull), or point the client directly at https://ollama.com with an API key and talk to the cloud API without a local server in the middle. The whole thing sits on top of httpx, so any extra keyword arguments you pass to the client flow straight through to httpx.Client.

Why It's Cool

The response objects are actually typed. Instead of digging through raw JSON, you get a ChatResponse you can index like a dict (response['message']['content']) or access with attribute-style syntax (response.message.content). Small thing, but it saves a lot of bracket-juggling when you're iterating.

Streaming is one flag. Set stream=True and the call returns an iterable you can loop over, printing chunks as they arrive. With AsyncClient, that same flag turns the function into an async generator you drive with async for. No separate streaming API to learn—it's the same call with a different return shape.

Local and cloud are the same interface. This is the part that stands out. Whether you're hitting a model on your laptop or a 120B model on Ollama's infrastructure, you're calling client.chat() with the same message format. Moving from local to cloud is mostly a matter of changing the model name and, for the direct cloud path, adding a host and an auth header. The README even shows the cloud API call using the identical client.chat('gpt-oss:120b', messages=messages, stream=True) pattern.

Async support isn't bolted on. AsyncClient takes the same configuration fields as Client, so you're not maintaining two mental models depending on whether you're in a sync or async codebase.

Custom clients are trivial. Need to hit a non-default host or pass a header? Instantiate Client(host=..., headers=...) and you're done. Since extra kwargs go to httpx, you get its full configuration surface for free.

How to Try It

First, make sure Ollama is installed and running, and pull a model:

ollama pull gemma4

Then install the library:

pip install ollama

Here's the minimal sync example from the README:

from ollama import chat
from ollama import ChatResponse

response: ChatResponse = chat(
  model='gemma4',
  messages=[
    {
      'role': 'user',
      'content': 'Why is the sky blue?',
    },
  ],
)
print(response['message']['content'])

Want streaming? Add stream=True and loop:

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)

For async, swap in AsyncClient and await the call—the README has a complete example, including the async-generator version for streaming. If you want to try cloud models, sign in with ollama signin, pull something like gpt-oss:120b-cloud, and call it through your local client. To go straight to the cloud API, grab an API key from ollama.com, export it as OLLAMA_API_KEY, and point Client(host='https://ollama.com', headers={'Authorization': 'Bearer ' + ...}) at it.

Full source and the rest of the examples are at github.com/ollama/ollama-python.

Final Thoughts

This library isn't trying to be a framework—it's a clean binding to an API you probably already want to use. The typed responses and the single-flag streaming are the kind of conveniences that quietly save you time, and the shared interface between local and cloud means you can prototype small and scale up without rewriting your integration. If you're building anything in Python that talks to Ollama, whether it's a quick script or an async service, this is the sensible starting point.

Follow @githubprojects for more developer tools and open source projects.

Back to Projects
Project ID: ollama-pythonLast updated: November 26, 2026 at 02:49 AM