From 0638e947c6a4455b45af5345db22c4e4c636f9b0 Mon Sep 17 00:00:00 2001 From: Pawan Singh Kapkoti Date: Mon, 6 Apr 2026 03:25:00 +0100 Subject: [PATCH] feat: add __enter__ and __aenter__ for context manager support Client and AsyncClient had __exit__/__aexit__ and close() methods but were missing __enter__/__aenter__, so `with Client() as c:` and `async with AsyncClient() as c:` would raise TypeError. Closes #532 --- ollama/_client.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ollama/_client.py b/ollama/_client.py index 18cb0fb4..bf2c7681 100644 --- a/ollama/_client.py +++ b/ollama/_client.py @@ -117,9 +117,15 @@ def __init__( **kwargs, ) + def __enter__(self): + return self + def __exit__(self, exc_type, exc_val, exc_tb): self.close() + async def __aenter__(self): + return self + async def __aexit__(self, exc_type, exc_val, exc_tb): await self.close()