Skip to main content

The Python client for TensorZero

Project description

TensorZero Python Client

Website · Docs · Twitter · Slack · Discord

Quick Start (5min) · Deployment Guide · API Reference · Configuration Reference

The tensorzero package provides a Python client for the TensorZero Gateway. This client allows you to easily make inference requests and assign feedback to them via the gateway.

See our API Reference for more information.

Installation

pip install tensorzero

Basic Usage

Initialization

The TensorZero client offers synchronous (TensorZeroGateway) and asynchronous (AsyncTensorZeroGateway) variants. Additionally, the client can launch an embedded (in-memory) gateway (build_embedded) or connect to an external HTTP gateway (build_http) - both of these methods return a gateway instance.

By default, the asynchronous client returns a Future when you call build_http or build_embedded, so you must await it. If you prefer to avoid the await, you can set async_setup=False to initialize the client in a blocking way.

Synchronous HTTP Gateway

from tensorzero import TensorZeroGateway

with TensorZeroGateway.build_http(gateway_url="http://localhost:3000") as client:
    # ...

Asynchronous HTTP Gateway

import asyncio

from tensorzero import AsyncTensorZeroGateway


async def run():
    async with await AsyncTensorZeroGateway.build_http(
        gateway_url="http://localhost:3000",
        # async_setup=False  # optional: skip the `await` and run `build_http` synchronously (blocking)
    ) as client:
        # ...


if __name__ == "__main__":
    asyncio.run(run())

Synchronous Embedded Gateway

from tensorzero import TensorZeroGateway

with TensorZeroGateway.build_embedded(
    config_file="/path/to/tensorzero.toml",
    clickhouse_url="http://chuser:chpassword@localhost:8123/tensorzero"
) as client:
    # ...

Asynchronous Embedded Gateway

import asyncio

from tensorzero import AsyncTensorZeroGateway


async def run():
    async with await AsyncTensorZeroGateway.build_embedded(
        config_file="/path/to/tensorzero.toml",
        clickhouse_url="http://chuser:chpassword@localhost:8123/tensorzero"
        # async_setup=False  # optional: skip the `await` and run `build_embedded` synchronously (blocking)
    ) as client:
        # ...


if __name__ == "__main__":
    asyncio.run(run())

Inference

Non-Streaming Inference with Synchronous Client

with TensorZeroGateway.build_http(gateway_url="http://localhost:3000") as client:
    response = client.inference(
        model_name="openai::gpt-4o-mini",
        input={
            "messages": [
                {"role": "user", "content": "What is the capital of Japan?"},
            ],
        },
    )

    print(response)

Non-Streaming Inference with Asynchronous Client

async with await AsyncTensorZeroGateway.build_http(gateway_url="http://localhost:3000") as client:
    response = await client.inference(
        model_name="openai::gpt-4o-mini",
        input={
            "messages": [
                {"role": "user", "content": "What is the capital of Japan?"},
            ],
        },
    )

    print(response)

Streaming Inference with Synchronous Client

with TensorZeroGateway.build_http(gateway_url="http://localhost:3000") as client:
    stream = client.inference(
        model_name="openai::gpt-4o-mini",
        input={
            "messages": [
                {"role": "user", "content": "What is the capital of Japan?"},
            ],
        },
        stream=True,
    )

    for chunk in stream:
        print(chunk)

Streaming Inference with Asynchronous Client

async with await AsyncTensorZeroGateway.build_http(gateway_url="http://localhost:3000") as client:
    stream = await client.inference(
        model_name="openai::gpt-4o-mini",
        input={
            "messages": [{"role": "user", "content": "What is the capital of Japan?"}],
        },
        stream=True,
    )

    async for chunk in stream:
        print(chunk)

Feedback

Synchronous

with TensorZeroGateway.build_http(gateway_url="http://localhost:3000") as client:
    response = client.feedback(
        metric_name="thumbs_up",
        inference_id="00000000-0000-0000-0000-000000000000",
        value=True,  # 👍
    )

    print(response)

Asynchronous

async with await AsyncTensorZeroGateway.build_http(gateway_url="http://localhost:3000") as client:
    response = await client.feedback(
        metric_name="thumbs_up",
        inference_id="00000000-0000-0000-0000-000000000000",
        value=True,  # 👍
    )

    print(response)

Stubtest

Run uv run stubtest tensorzero.tensorzero to confirm that the stub tensorzero.pyi file matches PyO3 compilation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tensorzero-2026.6.0.tar.gz (2.3 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tensorzero-2026.6.0-cp310-abi3-win_amd64.whl (37.4 MB view details)

Uploaded CPython 3.10+Windows x86-64

tensorzero-2026.6.0-cp310-abi3-musllinux_1_2_x86_64.whl (42.5 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ x86-64

tensorzero-2026.6.0-cp310-abi3-musllinux_1_2_aarch64.whl (41.6 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

tensorzero-2026.6.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (42.2 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

tensorzero-2026.6.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (41.4 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

tensorzero-2026.6.0-cp310-abi3-macosx_11_0_arm64.whl (38.4 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file tensorzero-2026.6.0.tar.gz.

File metadata

  • Download URL: tensorzero-2026.6.0.tar.gz
  • Upload date:
  • Size: 2.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.13.1

File hashes

Hashes for tensorzero-2026.6.0.tar.gz
Algorithm Hash digest
SHA256 d27d4b79c412c888d1edf0d0a9ec99a664976a63cb4e6568a35e3e2529990784
MD5 59946214b80e7cb46e185ca68715b059
BLAKE2b-256 3425932f78db3755656a969f0c023b65742f501a3763a6ba9b5f6ac231ba580e

See more details on using hashes here.

File details

Details for the file tensorzero-2026.6.0-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for tensorzero-2026.6.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 832b46d04de255a9ef0ca5f5db3e33d75c6bc31fa33cd15efccb0e81ada8d2d1
MD5 4b24e9a3f1e23bbc20be3f0c9ef8b072
BLAKE2b-256 4a20b4b0194315095193a92f4930469248e2e184c96ec4b06070333aa2fa3bea

See more details on using hashes here.

File details

Details for the file tensorzero-2026.6.0-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tensorzero-2026.6.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 50f8c6bd6ecf321d2cc94596d47d8e749fbf9e08ae0baa75ee024eb72605f9f4
MD5 2b8a2adb1591144a4c53145ecc9c9df7
BLAKE2b-256 b1823e26232f7bcd9b0a16a6090b5227cde743b8aeaebe0750bb68c4eb9e9f26

See more details on using hashes here.

File details

Details for the file tensorzero-2026.6.0-cp310-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tensorzero-2026.6.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 4539c846b68b62957fc76ba64df154b1c00d0a5d206e1b7b2a8ee2dc8a7b8b2a
MD5 22b17ce7509c52ec253e6a5c6c70579e
BLAKE2b-256 dcdfd66c5832c4cd77f2d700691072c5ea84c1539b5e7b7867e972de981b426d

See more details on using hashes here.

File details

Details for the file tensorzero-2026.6.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tensorzero-2026.6.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e23151baa7d3841808354d2517750b786cfa7680336e7762ece67e8fa185ba68
MD5 d8f68228861dd78f8eb78307c4a33c3a
BLAKE2b-256 d13d6b99b1992c7031753825cad1fa9623d09820135bef9a8ab93524b2862b4a

See more details on using hashes here.

File details

Details for the file tensorzero-2026.6.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for tensorzero-2026.6.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 3db2adf01b0737800966a4e1be592eb5e57ffc7549658263d3d97a7a45ed107f
MD5 ef1a16115e4decf08cc88273df2a000d
BLAKE2b-256 231f002bcca5eaebba838a247e269adc21d5f8f644cd5619b976168b2be8d032

See more details on using hashes here.

File details

Details for the file tensorzero-2026.6.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tensorzero-2026.6.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7a99799e9a3a924984573ace1e16e2ac56c28888c30290be48b7ea7423390b55
MD5 1007b8a9dcc459c279f3a0ad04fa9073
BLAKE2b-256 eee8a791e45a9dc5c258613c46b7d8438fe6df25942ccebc2abf609a822acd0e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page