ollama/ollama-python
Ollama Python library
What it solves
The Ollama Python Library gives Python developers a simple, high-level way to talk to Ollama, the tool that runs large language models on your own machine. Instead of hand-crafting HTTP requests to Ollama's REST API, you can call a few Python functions to chat with a model, generate text, manage models (pull, push, create, copy, delete), and even get embeddings. It also smooths the path to using larger models hosted in Ollama's cloud, either through your local Ollama setup or directly via the cloud API.
How it works
The library wraps Ollama's REST API. After installing with pip install ollama, you make sure Ollama is running and have pulled a model (e.g. ollama pull gemma4). Then you can call functions like ollama.chat(model='gemma4', messages=[...]) or ollama.generate(model='gemma4', prompt='...'). Responses come back as typed objects, and you can enable streaming by setting stream=True to get chunks as they arrive.
For more control, you can create a custom Client (or AsyncClient) with a specific host and extra headers. The async client lets you use await and async generators for non‑blocking calls. Cloud models are supported in two ways: you can sign in locally and pull a cloud model (e.g. ollama pull gpt-oss:120b-cloud) and then use it like a local model, or you can point the client directly at https://ollama.com with an API key. The library also includes a systemone function for structured decision‑making, where you provide a state and a set of questions (choice, probability, or score) and get a single JSON answer.
Who it's for
Python developers who want to integrate Ollama's local or cloud AI models into their applications without dealing with raw HTTP. It's useful for building chatbots, text‑generation tools, embedding pipelines, or any app that needs to call an LLM. The library is designed for Python 3.8+ and works both in synchronous and asynchronous codebases.
Highlights
- Simple
pip install ollamaand a clean function‑based API for chat, generate, list, show, create, copy, delete, pull, push, embed, and ps. - Streaming responses with
stream=Truefor both sync and async clients. - Async support via
AsyncClientwithawaitand async generators. - Cloud model support: use larger models through your local Ollama or directly via the ollama.com API with an API key.
- Custom clients let you set host and headers (e.g. for authentication or proxies).
systemonefor structured, single‑response decision tasks with choice, probability, and score questions (including image input in newer versions).- Clear error handling with
ollama.ResponseError, including status codes to help you react (e.g. auto‑pull a missing model).
Related
- Project
- Project
- Dispatch
- Dispatch
- Dispatch