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.4.0.tar.gz (2.0 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.4.0-cp310-abi3-win_amd64.whl (37.4 MB view details)

Uploaded CPython 3.10+Windows x86-64

tensorzero-2026.4.0-cp310-abi3-musllinux_1_2_x86_64.whl (42.1 MB view details)

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

tensorzero-2026.4.0-cp310-abi3-musllinux_1_2_aarch64.whl (41.2 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

tensorzero-2026.4.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (41.8 MB view details)

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

tensorzero-2026.4.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (41.0 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

tensorzero-2026.4.0-cp310-abi3-macosx_11_0_arm64.whl (38.0 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for tensorzero-2026.4.0.tar.gz
Algorithm Hash digest
SHA256 eb4c897e21ea58d5d82f7630a6a7b529cee100d8ffc82cb86b71dd4c92e1f842
MD5 e6d8fc4ce0014cb7ec735de150ed9fc2
BLAKE2b-256 cb659891ad95f6dc1d55e5d543d32baa04aadd56e494156e66cc0a8a26990d15

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensorzero-2026.4.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 91abcceae2ff3f8ccb571dbd8f09d67d4370268d02f1dcca0a222c72d0d5ffe7
MD5 72afb596277d0640c08cc6b05246139b
BLAKE2b-256 fc8e758f1e994655124d1a0a1ff65dd0f045365f7a0f65ec824f1267bb7c185c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensorzero-2026.4.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5963a600ab7bfd30b4456349cdaa5a953bbc4907e119e9a405274615a908fb5a
MD5 2f07dc8b5d7b153a99bb073df4ac2674
BLAKE2b-256 1cf71da8eb232ee2a37d30c6275341df8e9818d10479ebd61ddbc0086f816580

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensorzero-2026.4.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 f1a6ce77145597f187b94548808a42ce6347b675d90e51ff60e2de9efbf212e5
MD5 77957281c7c67c8f9ce80077e9184f25
BLAKE2b-256 e62782e4f3ac60469e168138733b9f5b9f99ee98188aaef7de56fd4d975b35fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensorzero-2026.4.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f3738495fb7b1d707d1d8647380bfa1cf08935b41f667b1cd539054e7f9fa5d2
MD5 439fa4a3683e6ebbe78510970470d4e4
BLAKE2b-256 658e9f2aac4d2b4bd58bba31c9c0fc3ed10d59210bd45328e4fcfe0e9fec8a77

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensorzero-2026.4.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d2698759072e1b9cf274fe80d0eb7427aa075245820a376484f0fa20d18556e5
MD5 8e1ee94f7a864a7c3fb09b368537b969
BLAKE2b-256 b61c9cdd4a2091867dcb50176758ab0fb3ddf409587123fe39f3d3964f41711c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tensorzero-2026.4.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a4d908f8eb7ca14f9470a5279d09460698d837673715e5056812b43b7a7c31ca
MD5 346572bbf3c9d4842af0a202dfbf01c5
BLAKE2b-256 c60cead3e759a81c22e7faede6b83f50b09936161f48c0251e089c360aaadc7d

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