ollama-python

The official Python client for chatting, generating, and managing models on a local or cloud-hosted Ollama server.

SDK
PyPI
v0.6.2
10,557 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
70 /100 Good
Development Activity 56
Maintenance 64
Community 72
Maturity 48
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
74 /100 Good
Architecture 78
Code Quality 85
Innovation 78
Learning Curve 55

ollama-python is the official Python SDK for Ollama, the local-first LLM runtime. It provides synchronous and asynchronous clients that wrap Ollama’s REST API with typed Pydantic request and response models, covering chat, text generation, embeddings, and full model-lifecycle operations such as pull, push, create, copy, and delete.

Beyond a single local server, the same client classes can target Ollama’s hosted cloud API by swapping the host and adding an API key, so code written against a local model can be pointed at larger cloud-hosted models with a configuration change rather than a rewrite. A built-in helper converts plain Python functions into tool-call schemas automatically, removing a common source of hand-written JSON-schema boilerplate in agentic tool-calling code.

What You Get

  • A Client and AsyncClient pair covering chat, generate, embed, and model-management endpoints with matching method signatures
  • Typed Pydantic response models (ChatResponse, GenerateResponse, EmbedResponse, ListResponse, etc.) that support both attribute and dict-style access
  • Streaming support via stream=True, returning a (async) generator of incrementally parsed response chunks
  • Automatic conversion of plain Python functions into Ollama tool-call schemas for agentic/tool-calling workflows
  • Built-in Image handling for multimodal chat/generate calls from raw bytes, file paths, or base64 strings
  • Hosted web-search and web-fetch helpers (web_search, web_fetch) for agent tool loops

Common Use Cases

  • Local LLM prototyping — call ollama.chat() against a locally running model with no cloud API keys or network access required
  • Streaming chat UIs — wire AsyncClient().chat(..., stream=True) into a backend SSE/WebSocket endpoint to stream tokens as they’re generated
  • Agent tool-calling pipelines — pass plain Python functions to chat(tools=[...]) and rely on automatic schema conversion instead of hand-written JSON schemas
  • Cloud-offloaded inference — repoint Client(host='https://ollama.com') with an API key to run larger cloud-hosted models with no code changes beyond configuration
  • Model lifecycle automation — script pull, create, and delete calls to manage custom fine-tuned models as part of a deployment pipeline

Under The Hood

Architecture ollama/_client.py defines a shared BaseClient (header setup, timeout/proxy handling) subclassed by Client (sync, built on httpx.Client) and AsyncClient (async, built on httpx.AsyncClient via anyio), each implementing near-identical method pairs — chat, generate, embed, embeddings, pull, push, create, delete, list, copy, show, ps, web_search, web_fetch — that build request payloads from the Pydantic models in ollama/_types.py, send them through a shared _request/_request_raw helper, and either return a parsed response object or, when stream=True, yield an iterator/async-generator of incrementally parsed JSON chunks; ollama/_utils.py holds a standalone convert_function_to_tool helper invoked from _client.py, and ollama/init.py instantiates one module-level Client and re-exports its bound methods as top-level functions (ollama.chat(), ollama.generate()) for a lower-ceremony scripting layer on top of the class-based API.

Tech Stack Python (>=3.8) with httpx>=0.27 as the HTTP transport for both clients, pydantic>=2.9 for typed request/response models and JSON-schema generation for tool definitions, and anyio for async primitives; built and versioned via hatchling/hatch-vcs (version derived from git tags), linted and formatted with ruff (custom line-length and quote-style rules), and tested through a hatch-test environment running pytest with pytest-anyio and pytest-httpserver. Distributed on PyPI as the ollama package; its runtime deployment target is simply being pip-installed alongside a locally running or cloud-hosted Ollama server rather than a service of its own.

Code Quality tests/test_client.py, test_utils.py, and test_type_serialization.py exercise both Client and AsyncClient against pytest-httpserver mocks, covering request construction, streaming, tool-schema conversion, and Pydantic (de)serialization edge cases. Errors are explicit and typed via a custom ResponseError raised from parsed HTTP error bodies rather than generic exceptions; naming stays consistent snake_case mirroring the underlying REST endpoints, and type hints plus Pydantic models give strong type safety throughout _client.py. ruff enforces both linting and formatting, and a GitHub Actions workflow (test.yaml) runs the test suite in CI alongside a separate publish.yaml for releases — comprehensive test coverage, typing, and CI enforcement.

API Design The public API mirrors Ollama’s REST endpoint names almost one-to-one (chat, generate, embed, show, ps), so anyone familiar with Ollama’s HTTP docs needs no translation layer to use the Python client. Getting started requires nothing beyond pip install ollama and a running local server, and the module-level functions let simple scripts skip client instantiation entirely while power users can still drop to Client/AsyncClient for custom hosts, headers, or timeouts. The automatic function-to-tool schema conversion is the most notable ergonomic touch, removing a class of manual JSON-schema boilerplate that many comparable LLM SDKs still require by hand.

Used by 10 apps in this directory

Python
100%
Apache 2.0

Agno

AI Development · Automation · Devops

42,358

Build, run, and manage agent platforms with a full production stack — SDK, runtime, and control plane included.

View details
93
Repo Health
87
Technical
66
Dependency
Built with
Python 100%
Updated 5 days ago
Python
62%
MIT

AutoGen

AI Development · Automation

61,194

Build autonomous and human-in-the-loop multi-agent AI systems with a layered, event-driven Python and .NET framework pioneered at Microsoft Research.

View details
56
Repo Health
78
Technical
73
Dependency
Built with
Python 62%
C# 25%
TypeScript 12%
Updated 5 months ago
Python
66%
Other

AutoGPT

AI Assistants · Automation · Productivity

187,596

Build, deploy, and run autonomous AI agents that automate complex multi-step workflows using a visual block-based graph editor.

View details
93
Repo Health
78
Technical
66
Dependency
Built with
Python 66%
TypeScript 33%
Updated 5 days ago
Python
70%
Apache 2.0

GPT Researcher

AI Assistants · Productivity

29,650

The pioneering open-source autonomous AI agent that conducts deep, multi-source research and produces citation-backed reports exceeding 2,000 words — faster and more reliably than any human researcher.

View details
91
Repo Health
91
Technical
63
Dependency
Built with
Python 70%
TypeScript 18%
Updated 1 weeks ago
Python
82%
Apache 2.0

headroom

AI Development · Developer Tools

73,966

Compress everything your AI agent reads — tool outputs, logs, RAG chunks, and files — before it reaches the LLM, achieving 60–95% fewer tokens with the same answers.

View details
85
Repo Health
86
Technical
69
Dependency
Built with
Python 82%
Rust 12%
Updated 6 days ago
TypeScript
55%
Other

Jaaz

AI Agents · AI Design Tools

6,670

Open-source AI creative agent that turns visual sketches and canvas gestures into images and videos — no text prompts required.

View details
48
Repo Health
59
Technical
71
Dependency
Built with
TypeScript 55%
Python 34%
JavaScript 10%
Updated 7 months ago
Rust
43%
Apache 2.0

LanceDB

AI Development · Databases

11,544

Open-source, embedded vector database built on the Lance columnar format for fast multimodal search across billions of vectors, backed by Y Combinator (W23).

View details
90
Repo Health
86
Technical
71
Dependency
Built with
Rust 43%
Python 25%
HTML 23%
Updated 6 days ago
Rust
48%
MIT

Meetily

AI Assistants · Productivity

31,171

Privacy-first AI meeting assistant that transcribes and summarizes your meetings entirely on your local machine — no cloud, no data leakage.

View details
85
Repo Health
72
Technical
67
Dependency
Built with
Rust 48%
TypeScript 29%
Updated 2 weeks ago
Python
66%
Other

Morphik

AI Development · Databases · Search

3,716

Morphik is an AI-native ingestion and retrieval engine that lets developers store, search, and reason over visually rich documents — scanned PDFs, manuals, slides, and video — without duct-taping together OCR, an embedding model, and a vector database.

View details
60
Repo Health
71
Technical
67
Dependency
Built with
Python 66%
TypeScript 23%
Updated 1 weeks ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers