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
26 changes: 26 additions & 0 deletions ollama/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ class Client(BaseClient):
def __init__(self, host: Optional[str] = None, **kwargs) -> None:
super().__init__(httpx.Client, host, **kwargs)

def close(self) -> None:
"""Close the underlying HTTP client and release resources."""
self._client.close()

def __enter__(self):
"""Support usage as a context manager."""
return self

def __exit__(self, exc_type, exc_val, exc_tb):
"""Ensure the client is closed when exiting the context."""
self.close()
return False

def _request_raw(self, *args, **kwargs):
try:
r = self._client.request(*args, **kwargs)
Expand Down Expand Up @@ -686,6 +699,19 @@ class AsyncClient(BaseClient):
def __init__(self, host: Optional[str] = None, **kwargs) -> None:
super().__init__(httpx.AsyncClient, host, **kwargs)

async def close(self) -> None:
"""Close the underlying HTTP client and release resources."""
await self._client.aclose()

async def __aenter__(self):
"""Support usage as an async context manager."""
return self

async def __aexit__(self, exc_type, exc_val, exc_tb):
"""Ensure the client is closed when exiting the async context."""
await self.close()
return False

async def _request_raw(self, *args, **kwargs):
try:
r = await self._client.request(*args, **kwargs)
Expand Down