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.5.0.tar.gz (2.2 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.5.0-cp310-abi3-win_amd64.whl (37.6 MB view details)

Uploaded CPython 3.10+Windows x86-64

tensorzero-2026.5.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.5.0-cp310-abi3-musllinux_1_2_aarch64.whl (41.6 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

tensorzero-2026.5.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.5.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.5.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.5.0.tar.gz.

File metadata

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

File hashes

Hashes for tensorzero-2026.5.0.tar.gz
Algorithm Hash digest
SHA256 a5ca24bf7ee888afba70d2c58bcf46672a8b0920f20dcb079715b73ceba84b50
MD5 b206395a3f1ff3a8a42d108aaf607605
BLAKE2b-256 71f3f7f950aa6df52b53009c79af4d31335e802c50ced4b0347760ffe2a56a1d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensorzero-2026.5.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 706a3131851a09da9301805e29314bfe6a8e6abc61920f4c92963df43ab5daa8
MD5 223d3cff39128e84ccf5fa403ccc92aa
BLAKE2b-256 4163de9b271f653e8e88b2fdba1005206224abd7185823cbd9830123560901df

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensorzero-2026.5.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 6948d4acbd7f6e29baed678f01b931c1966c368850627bd8804aca880501f113
MD5 e4abc3082c6b37653415980c62fb690f
BLAKE2b-256 0cd02009dd445325a67605e0440037d51c5af5e3dd80d11c00311cf635fa5a72

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensorzero-2026.5.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 6d19d7a6110dd7aa479144db1a9a6a13b882275e6569cdffa6b81ea864e3e2e9
MD5 cbabd3e27d2e6fc9e1bd21608474aafd
BLAKE2b-256 ea1fcfc29ef7cbe5f05dd9f6dc197ea3d28a0f4e6ecde6cc0d2cd155450cb4ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensorzero-2026.5.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 659705f2011813b87d96700f0bde5f3f8aee920f44a82708d871f7c393bdf7e3
MD5 a1e9343e657fa68a4f9614638fe85cd5
BLAKE2b-256 2f7d3078725db6ffc12ec0cbdf9d816a218810cfbe493cc7e0c73af924695656

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensorzero-2026.5.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ffdf543c94336eb6977f4d7348ad8f4f71549b8e8f5bcecf2ce6fdbb815693e2
MD5 bf537d51156a2e02a289d43b0ad60529
BLAKE2b-256 c3ab9a807848dc151428e72315c58ad47461ab214449731522529f54f17571ce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensorzero-2026.5.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 802990660bb4ae05ad6e82d61b811d0a75c25b17b23c540ab8521e1715d60a36
MD5 b057a6b4cb7f66221c2a4408101f6037
BLAKE2b-256 b3334e94714c55b64535cb0bb9d6a7fa0db848f12492c659250c78f54913216a

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