Skip to content

Conversation

@ServOMorph
Copy link

Summary

Adds explicit resource management to Client and AsyncClient by implementing:

  • close() method for manual cleanup
  • Context manager protocol (__enter__/__exit__)
  • Async context manager protocol (__aenter__/__aexit__)

Motivation

Closes #532

This allows users to properly clean up HTTP connections and release resources, following Python best practices.

Changes

Client (synchronous)

  • Added close() method to explicitly close the underlying httpx.Client
  • Implemented __enter__ and __exit__ to support context manager usage

AsyncClient (asynchronous)

  • Added async close() method to explicitly close the underlying httpx.AsyncClient
  • Implemented __aenter__ and __aexit__ to support async context manager usage

Usage Examples

Synchronous

# Manual close
client = Client()
try:
    response = client.chat(model='gemma3', messages=[...])
finally:
    client.close()

# Context manager (recommended)
with Client() as client:
    response = client.chat(model='gemma3', messages=[...])
# Automatically closed
Asynchronous
# Manual close
client = AsyncClient()
try:
    response = await client.chat(model='gemma3', messages=[...])
finally:
    await client.close()

# Async context manager (recommended)
async with AsyncClient() as client:
    response = await client.chat(model='gemma3', messages=[...])
# Automatically closed
Testing
Tested manually with both synchronous and asynchronous usage patterns. All existing functionality remains unchanged.
Checklist
 Implementation follows existing code style
 Works for both Client and AsyncClient
 Maintains backward compatibility (existing code continues to work)
 Follows Python context manager protocol correctly

…yncClient

- Add close() method to Client and AsyncClient for explicit resource cleanup
- Implement __enter__/__exit__ for Client to support context manager usage
- Implement __aenter__/__aexit__ for AsyncClient to support async context manager usage
- Allows usage: with Client() as client: ...
- Allows usage: async with AsyncClient() as client: ...

Closes ollama#532
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.

Add Client.close() and context manager

1 participant