Skip to content

Feat/add context manager support - #713

Open
SaiDevaHarshaR wants to merge 6 commits into
ollama:mainfrom
SaiDevaHarshaR:feat/add-context-manager-support
Open

Feat/add context manager support#713
SaiDevaHarshaR wants to merge 6 commits into
ollama:mainfrom
SaiDevaHarshaR:feat/add-context-manager-support

Conversation

@SaiDevaHarshaR

Copy link
Copy Markdown

What this does

Fixes #614 — adds context manager support to Client and AsyncClient,
enabling the with-as syntax for automatic resource cleanup.

Usage

# Before — manual management
client = ollama.Client()
response = client.chat(...)

# After — automatic cleanup
with ollama.Client() as client:
    response = client.chat(...)
# client automatically cleaned up on exit

Changes

  • Added __enter__ method to Client returning self
  • Added __enter__ method to AsyncClient returning self
  • __exit__ already existed on both classes calling self.close()

Testing

Verified with-as syntax works correctly and client
is cleaned up after the block exits.

Closes #614

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.

Could you add the with-as syntax

1 participant