Add `Client.close()` and context manager
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 10.5k
- Forks
- 1.2k
- Avg merge
- 4m
- Merged PRs (30d)
- 1
Description
Currently, a client to interact with the Ollama API can be created using
from ollama import Client
client = Client(host='http://localhost:11434', ...)
However, there is no way to close the client and give back the resources it acquired. Eventually, Python's garbage collector will, but it would be nice to be able to do that as early as possible.
From my understanding, adding the following methods to ollama._client.Client would address this:
class Client(BaseClient):
...
def close(self):
self._client.close()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close()
return False
And then something similar for the AsyncClient.
That way, the client could be used like:
with Client(host='http://localhost:11434', ...) as client:
client.chat(...)
If you want, I can look into this and prepare a PR.
Update: This should also fix warnings when using ollama-python with pytest:
.../.venv/lib/python3.12/site-packages/_pytest/unraisableexception.py:31: ResourceWarning: unclosed <socket.socket fd=15, family=2, type=1, proto=6, laddr=('127.0.0.1', 64484), raddr=('127.0.0.1', 11434)>
gc.collect()
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading ollama._client.Client and AsyncClient, including how each underlying _client is created. Verify that close and context-manager behavior releases resources for both clients and addresses the reported pytest ResourceWarning.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100