feat: add exists() method to Client and AsyncClient

Closes #640

Adds a convenience `exists(model)` method to both `Client` and
`AsyncClient` that returns `True` if the model is available locally,
`False` otherwise — no exception handling needed at the call site.

Implemented as a thin wrapper around `show()` that catches
`ResponseError`, matching the pattern already used throughout the SDK.
This commit is contained in:
Ghraven 2026-04-28 03:21:56 +08:00
parent dbccf192ac
commit b288f8ef4f

View File

@ -664,6 +664,32 @@ class Client(BaseClient):
).model_dump(exclude_none=True),
)
def exists(self, model: str) -> bool:
"""
Check whether a model is available locally.
This is a convenience wrapper around :meth:`show` that returns a
plain ``bool`` instead of raising :class:`ResponseError` when the
model is not found, making it suitable for use in ``if`` statements
without exception-based control flow.
Args:
model: The name of the model to check (e.g. ``"llama3.1:8b"``).
Returns:
``True`` if the model exists locally, ``False`` otherwise.
Example::
if not ollama.exists("llama3.1:8b"):
ollama.pull("llama3.1:8b")
"""
try:
self.show(model)
return True
except ResponseError:
return False
def ps(self) -> ProcessResponse:
return self._request(
ProcessResponse,
@ -1305,6 +1331,29 @@ class AsyncClient(BaseClient):
).model_dump(exclude_none=True),
)
async def exists(self, model: str) -> bool:
"""
Check whether a model is available locally.
Async variant of :meth:`Client.exists`.
Args:
model: The name of the model to check (e.g. ``"llama3.1:8b"``).
Returns:
``True`` if the model exists locally, ``False`` otherwise.
Example::
if not await client.exists("llama3.1:8b"):
await client.pull("llama3.1:8b")
"""
try:
await self.show(model)
return True
except ResponseError:
return False
async def ps(self) -> ProcessResponse:
return await self._request(
ProcessResponse,