Python client for Typesense, an open source and typo tolerant search engine.
Actively maintained. Last release 5 days ago, commits in 8 of the last 26 weeks. A safe choice.
pip install typesenseTypesense Python Client
Python client for the Typesense API: https://github.com/typesense/typesense
Installation
$ pip install typesense
You can also add typesense to your project's requirements.txt.
Usage
You can find some examples here.
See detailed API documentation.
Async usage
Use AsyncClient when working in an async runtime:
import asyncio
import typesense
async def main() -> None:
client = typesense.AsyncClient({
"api_key": "abcd",
"nodes": [{"host": "localhost", "port": "8108", "protocol": "http"}],
"connection_timeout_seconds": 2,
})
print(await client.collections.retrieve())
await client.api_call.aclose()
if __name__ == "__main__":
asyncio.run(main())
See examples/async_collection_operations.py for a fuller async walkthrough.
Streaming document exports
documents.export() loads the whole export into memory. documents.export_stream() reads it as it arrives:
with client.collections["books"].documents.export_stream({"batch_size": 500}) as stream:
for line in stream: # str, without the line feed
document = json.loads(line)
with client.collections["books"].documents.export_stream() as stream, open("books.jsonl", "wb") as file:
for chunk in stream.iter_bytes(): # decoded bytes
file.write(chunk)
With AsyncClient, await the call and use async with and async for, with stream.aiter_bytes() for the bytes:
async with await client.collections["books"].documents.export_stream() as stream:
async for line in stream:
document = json.loads(line)
A stream can be read once, either as lines or as bytes. Keep these in mind:
- Always close the stream, ideally with a context manager. With
max_concurrent_requestsset, a stream that is abandoned before its end permanently uses up one of the open export places below. With the sync client, closing a stream from another thread does not interrupt a read in progress there: that read raises once runs out.