Ollama Python Library
Official Python client for Ollama that wraps the REST API with sync and async clients, typed chat responses, streaming, and local or cloud model access for Python 3.8+.
Visit ollama/ollama-pythonOverview
The Ollama Python Library is the official Python client for Ollama. It wraps the Ollama REST API in idiomatic Python, with synchronous and asynchronous clients, typed chat responses, streaming, custom hosts and headers, and access to local or cloud models from Python 3.8 and up.
Key Features
- Typed chat responses can be read like a dict or through attribute access.
- Streaming responses are enabled with stream=True in sync calls and as an async generator with AsyncClient.
- Client and AsyncClient support the same configuration fields for sync or async codebases.
- Local and cloud models share the same client.chat interface, with direct cloud access to Ollama's hosted API using an API key.
- Custom clients can set host and headers, and extra keyword arguments pass through to httpx.Client.
- API coverage includes chat, generate, list, show, create, copy, delete, pull, push, embed, ps, and systemone.
Use Cases
- Build Python scripts or applications that chat with models served by a local Ollama instance.
- Add streaming chat to synchronous or asynchronous Python services.
- Prototype against local models and move to larger cloud models using the same chat interface.
- Access Ollama cloud models directly with an API key when no local server should sit in the middle.
Getting Started
- Install and run Ollama, then pull a model such as gemma4 with ollama pull gemma4.
- Install the library with pip install ollama.
- Import chat from ollama, pass a model and a list of messages, and print response['message']['content'] or response.message.content.
- For streaming, set stream=True and iterate over chunks; for async, use AsyncClient and await the call.
Deployment & Requirements
- Python 3.8 or later.
- Ollama installed and running for local use.
- A model pulled for use with the library, for example with ollama pull gemma4.
- For the direct cloud API, set OLLAMA_API_KEY and point Client at the Ollama cloud host with an authorization header.
Before You Adopt
- License: MIT. Review its terms before using, modifying, or distributing the project.
- Local use requires Ollama to be installed and running, and a model pulled.
- Direct cloud API access requires setting the OLLAMA_API_KEY environment variable.
- System One confidence scores indicate probability concentration, not calibrated correctness.