Skip to content

feat: add __enter__ and __aenter__ for context manager support#642

Open
Pawansingh3889 wants to merge 1 commit intoollama:mainfrom
Pawansingh3889:feat/client-close-context-manager
Open

feat: add __enter__ and __aenter__ for context manager support#642
Pawansingh3889 wants to merge 1 commit intoollama:mainfrom
Pawansingh3889:feat/client-close-context-manager

Conversation

@Pawansingh3889
Copy link
Copy Markdown

Adds missing enter and aenter to BaseClient. close()/exit already existed but without entry methods, with Client() as c: raised TypeError.

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 ollama#532
@ParthSareen ParthSareen self-requested a review April 22, 2026 07:55
Ghraven added a commit to Ghraven/ollama-python that referenced this pull request May 3, 2026
Fixes ollama#642

While `BaseClient` inherits from both `AbstractContextManager` and
`AbstractAsyncContextManager`, it only defined `__exit__` and `__aexit__`.
`AbstractContextManager` provides a default `__enter__` that returns `self`,
but `AbstractAsyncContextManager.__aenter__` is abstract and must be
implemented explicitly — meaning `async with AsyncClient() as c:` raised
`TypeError` at runtime.

This commit adds explicit `__enter__` and `__aenter__` methods that both
return `self`, completing the context manager protocol for both sync and
async clients and making `with Client() as c:` and `async with AsyncClient() as c:`
work as expected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant