Skip to content

feat(client): add close and context manager support to Client and Asy… - #169

Merged
tharropoulos merged 2 commits into
typesense:masterfrom
abhirajadhikary06:feat/client-close-context-manager
Nov 27, 2026
Merged

tharropoulos merged 2 commits into
typesense:masterfrom
abhirajadhikary06:feat/client-close-context-manager

Conversation

@abhirajadhikary06

Copy link
Copy Markdown
Contributor

Closes #168

Summary

Exposes close() / aclose() and context manager methods (with / async with) directly on Client and AsyncClient so users can manage connection pool lifecycles without reaching into client.api_call.

Changes

  • Add __aenter__, __aexit__, and aclose to AsyncClient in src/typesense/async_/client.py, delegating to self.api_call.aclose().
  • Add __enter__, __exit__, and close to Client in src/typesense/sync/client.py, delegating to self.api_call.close().
  • Add unit tests in tests/client_test.py verifying close() / aclose(), context manager exit behavior, and preservation of caller-supplied http_client instances.

Testing

  • Compiled modified files with py_compile.
  • Verified sync Client lifecycle against httpx.Client.is_closed and async AsyncClient delegation with AsyncMock.
  • Please run the full CI suite (run-unasync.py --check, ruff, mypy, and pytest).

@abhirajadhikary06

Copy link
Copy Markdown
Contributor Author

@tharropoulos review this when you have time

@haydenhoang haydenhoang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good.

One request: the docs still show the old way of closing the client, so please update them to use the new API:

README.md, in the "Async usage" example: replace await client.api_call.aclose() with async with typesense.AsyncClient({...}) as client:

The same client.api_call.aclose() pattern appears in:

  • examples/async_collection_operations.py
  • examples/async_conversation_streaming.py
  • examples/async_export_streaming.py

It might also be worth a short note in the README that a caller-supplied http_client is left open, so the caller has to close it.

Fine to do in a follow-up PR if you prefer.

@tharropoulos tharropoulos left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up.

One change requested inline, on the __aenter__ return type. Hayden's docs request still stands as well, since README.md and the three async examples still call client.api_call.aclose().

Comment thread src/typesense/async_/client.py Resolved
@abhirajadhikary06

Copy link
Copy Markdown
Contributor Author

Thanks, this looks good.

One request: the docs still show the old way of closing the client, so please update them to use the new API:

README.md, in the "Async usage" example: replace await client.api_call.aclose() with async with typesense.AsyncClient({...}) as client:

The same client.api_call.aclose() pattern appears in:

  • examples/async_collection_operations.py
  • examples/async_conversation_streaming.py
  • examples/async_export_streaming.py

It might also be worth a short note in the README that a caller-supplied http_client is left open, so the caller has to close it.

Fine to do in a follow-up PR if you prefer.

Updated README.md and the three async examples (examples/async_collection_operations.py, examples/async_conversation_streaming.py, and examples/async_export_streaming.py) to use async with typesense.AsyncClient(...) as client: instead of await client.api_call.aclose(), and added a note in README.md that a caller-supplied http_client is left open for the caller to close (commit 77ec039).

@tharropoulos tharropoulos left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@tharropoulos
tharropoulos merged commit 0f7fe6d into typesense:master Nov 27, 2026
4 checks passed
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.

Client and AsyncClient do not support close() / aclose() or context managers

3 participants