Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions ollama/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,32 @@ def show(self, model: str) -> ShowResponse:
).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,
Expand Down Expand Up @@ -1305,6 +1331,29 @@ async def show(self, model: str) -> ShowResponse:
).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,
Expand Down