From 136340f2ef1e179921a7f80e34667d1c4f2e960c Mon Sep 17 00:00:00 2001 From: Ghraven <115199279+Ghraven@users.noreply.github.com> Date: Tue, 28 Apr 2026 03:22:16 +0800 Subject: [PATCH] feat: expose __version__ and top-level version() helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #646 Adds two things to the public API: 1. `ollama.__version__` — the installed package version, resolved via `importlib.metadata` (same mechanism already used in `_client.py`). Now exported in `__all__` so it's part of the documented public API. 2. `ollama.version()` / `ollama.async_version()` — thin top-level wrappers around `Client.version()` so callers can check the running Ollama server version without instantiating a client. Also wires up `exists = _client.exists` to expose the new method at the top level, consistent with all other module-level shortcuts. --- ollama/__init__.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/ollama/__init__.py b/ollama/__init__.py index 92bba280..e415a60d 100644 --- a/ollama/__init__.py +++ b/ollama/__init__.py @@ -1,3 +1,10 @@ +from importlib import metadata as _metadata + +try: + __version__: str = _metadata.version('ollama') +except _metadata.PackageNotFoundError: + __version__ = '0.0.0' + from ollama._client import AsyncClient, Client from ollama._types import ( ChatResponse, @@ -20,6 +27,7 @@ ) __all__ = [ + '__version__', 'AsyncClient', 'ChatResponse', 'Client', @@ -37,11 +45,13 @@ 'ShowResponse', 'StatusResponse', 'Tool', + 'version', 'WebFetchResponse', 'WebSearchResponse', ] _client = Client() +_async_client = AsyncClient() generate = _client.generate chat = _client.chat @@ -55,5 +65,37 @@ copy = _client.copy show = _client.show ps = _client.ps +exists = _client.exists web_search = _client.web_search web_fetch = _client.web_fetch + + +def version() -> str: + """Return the running Ollama server version string. + + Hits the ``/api/version`` endpoint on the local Ollama server and returns + the version string (e.g. ``"0.18.2"``). + + This is distinct from :data:`__version__`, which is the version of the + *Python client package* itself. + + Returns: + The Ollama server version string. + + Example:: + + import ollama + + print(ollama.__version__) # "0.6.1" — client package version + print(ollama.version()) # "0.18.2" — running server version + """ + return _client.version() + + +async def async_version() -> str: + """Async variant of :func:`version`. + + Returns: + The Ollama server version string. + """ + return await _async_client.version()