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,492stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
67/100Good
Development Activity48
Maintenance60
Community72
Maturity48
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
74/100Good
Architecture78
Code Quality85
Innovation78
Learning Curve55

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

Devops · AI Development · Automation

42,074

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
Python100%
Updated yesterday
Python
62%
MIT

AutoGen

AI Development · Automation

60,839

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
Python62%
C#25%
TypeScript12%
Updated 4 months ago
Python
67%
Other

AutoGPT

Automation · Productivity · AI Assistants

187,177

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
Python67%
TypeScript32%
Updated today
Python
67%
Apache 2.0

GPT Researcher

Productivity · AI Assistants

29,319

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
90
Repo Health
91
Technical
63
Dependency
Built with
Python67%
TypeScript20%
Updated 1 weeks ago
Python
81%
Apache 2.0

headroom

AI Development · Developer Tools

69,144

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
84
Repo Health
86
Technical
72
Dependency
Built with
Python81%
Rust13%
Updated yesterday
TypeScript
55%
Other

Jaaz

AI Design Tools · AI Agents

6,626

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

View details
46
Repo Health
59
Technical
71
Dependency
Built with
TypeScript55%
Python34%
JavaScript10%
Updated 6 months ago
Rust
40%
Apache 2.0

LanceDB

Databases · AI Development

11,366

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
72
Dependency
Built with
Rust40%
HTML26%
Python26%
Updated yesterday
Rust
46%
MIT

Meetily

Productivity · AI Assistants

30,423

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

View details
61
Repo Health
72
Technical
71
Dependency
Built with
Rust46%
TypeScript30%
Updated yesterday
Python
67%
Other

Morphik

AI Development · Search · Databases

3,710

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
61
Repo Health
71
Technical
67
Dependency
Built with
Python67%
TypeScript25%
Updated 3 days 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

Search