Skip to main content

Typesense 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_requests set, 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 stream_read_timeout_seconds runs out.
  • A failed export has to be restarted. Nodes are only retried until the response headers arrive. After that, errors such as httpx.RemoteProtocolError or httpx.ReadTimeout are raised as they are, and exports cannot resume from where they stopped.
  • Keep reading. If your code stops reading for about 60 seconds, the server drops the connection. stream_read_timeout_seconds (default 60) separately limits how long the client waits for the server's next chunk.
  • Lines are raw. They are not parsed, and the server could write an error message into the body after a successful status, so a line is not guaranteed to be a document.
  • Exports are uncompressed by default. The server compresses on the thread that handles all other requests, which delays them, especially with a large batch_size. Pass compress=True if bandwidth matters more. An Accept-Encoding in additional_headers is always respected.

With max_concurrent_requests, an export only takes a slot while it opens, so requests made while reading an export do not wait for it. To keep connections free for those requests, at most max_connections - max_concurrent_requests exports can be open at once. Further exports wait for one to close, with no timeout, so an export opened inside another needs a second free place or it waits forever. If max_concurrent_requests is not below max_connections, no connection can be kept free, so exports are not limited. The limit does not apply to an http_client you pass in, because its pool size is unknown.

If your application cancels many requests, prefer httpx2, which fixes a connection pool leak that cancellation can trigger in httpx.

Using httpx2

The client sends requests with httpx by default. You can pass an httpx2 client instead. httpx2 is Pydantic's maintained continuation of httpx, and it fixes a connection pool leak in httpcore (encode/httpcore#1093) that can leave an AsyncClient failing every request with PoolTimeout under load.

$ pip install "typesense[httpx2]"
import httpx2
import typesense

http_client = httpx2.AsyncClient(
    timeout=httpx2.Timeout(2.0),
    limits=httpx2.Limits(max_connections=100, max_keepalive_connections=20),
)
client = typesense.AsyncClient(
    {
        "api_key": "abcd",
        "nodes": [{"host": "localhost", "port": "8108", "protocol": "http"}],
    },
    http_client=http_client,
)

typesense.Client takes an httpx2.Client the same way. The connection pool settings in the config (pool_timeout_seconds, max_connections, max_keepalive_connections) only apply to the default client, so set them on your own client instead. The Typesense client does not close a client you pass in.

Compatibility

Typesense Server typesense-python
>= v30.0 >= v2.0.0
>= v28.0 >= v1.0.0
>= v26.0 >= v0.20.0
>= v0.25.0 >= v0.16.0
>= v0.23.0 >= v0.14.0
>= v0.21.0 >= v0.13.0
>= v0.20.0 >= v0.11.0
>= v0.19.0 >= v0.10.0
>= v0.17.0 >= v0.9.0
>= v0.16.0 >= v0.8.0
>= v0.15.0 >= v0.7.0

Contributing

Bug reports and pull requests are welcome on GitHub at [https://github.com/typesense/typesense-python]. If you change any part of the client's source code, run uv run utils/run-unasync.py before opening a PR to keep the generated sync files in sync.

License

typesense-python is distributed under the Apache 2 license.

Metadata

Release files for typesense 2.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for typesense 2.2.0
File Size Uploaded Provenance Actions
typesense-2.2.0.tar.gz 108.3 kB With provenance Details for typesense-2.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for typesense 2.2.0
File Interpreter ABI Platform Actions
typesense-2.2.0-py3-none-any.whl Python 3 none any Details for typesense-2.2.0-py3-none-any.whl

Total release size: 283.4 kB

Release files / typesense-2.2.0.tar.gz

Download URL typesense-2.2.0.tar.gz
Size 108.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a76923f05bc55662acbd7fe247fbeacf05ad84b98964e05b2d70db4dd71f306a
BLAKE2b-256 checksum
How to use checksums
62166b1d5391cd63e918bbfed609b8aecb13dd7adb185a9e820c74e66f523007
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / typesense-2.2.0-py3-none-any.whl

Download URL typesense-2.2.0-py3-none-any.whl
Size 175.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2b14cfaa284e89d183a3b9c76966f6dff62f7b430f8a822792e2287af6a1f2cb
BLAKE2b-256 checksum
How to use checksums
ccc23f93427e0f8aed7b1db87c246d321b8c94ae180e1ce5bcfa92d0e2177049
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Security for typesense 2.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for typesense-2.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.2.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page