Python SDK

Production Python 3.10+ client for Teleneura edge-AI telemetry. Mirrors the native SDK surface (init, report_inference, flush) and POSTs batched aggregates to the same POST /api/telemetry contract as React Native, Flutter, and KMP.

Ideal for PyTorch, ONNX Runtime, TensorFlow, Jupyter pipelines, and edge gateways where you control the inference loop in Python.


Installation

pip install teleneura

From the monorepo (editable, for contributors):

pip install -e packages/teleneura-python

Dependency: httpx (installed automatically).


Quick start

from teleneura import Teleneura, TeleneuraConfig

client = Teleneura()
client.init(TeleneuraConfig(
    api_key="ys_test_…",
    base_url="http://localhost:3001",
    batch_size=32,
    enable_debug_logs=True,
))

client.report_inference("resnet50-int8", 41.2)

with client.track_inference("yolo-nano"):
    outputs = session.run(None, feed)

client.flush()
client.close()

Use your full console secret (ys_live_… or ys_test_…) — the same keys created under API & Keys in the fleet console.


Wrap a model inference

Manual timing

import time

start = time.perf_counter()
predictions = model(input_tensor)
latency_ms = (time.perf_counter() - start) * 1000.0

client.report_inference("my-model-v1", latency_ms)

Context manager (recommended)

with client.track_inference("my-model-v1"):
    predictions = model(input_tensor)

The SDK records elapsed wall time in milliseconds and batches on a background thread — same semantics as Teleneura.reportInference on mobile.


Configuration

TeleneuraConfig(
    api_key="ys_live_…",
    base_url="https://api.example.com",
    batch_size=20,              # 1–500, default 20
    enable_debug_logs=False,
    platform_label="Python",    # auto-enriched with OS + version
    is_quantized=False,
    device_model=None,          # override auto-detect
    manufacturer=None,
    soc_model=None,
    thermal_status="nominal",
    default_battery_temp_c=25.0,
)

On servers and laptops without battery sensors, temperature fields use default_battery_temp_c for both startBatteryTemp and endBatteryTemp — matching how native SDKs send a single reading per batch.


API surface

Method

Role

init(config)

Configure API key, base URL, batching, and optional device overrides.

report_inference(model, latency_ms)

Buffer one sample; auto-POST when batch_size is reached (background thread).

track_inference(model)

Context manager — times the block and calls report_inference.

flush(wait=True)

Upload all pending buffers; raises on HTTP errors when wait=True.

close(flush=True)

Flush, shut down worker pool, release HTTP client.


Errors

Exception

When

TeleneuraConfigErrorInvalid api_key, base_url, or batch_size
TeleneuraNotInitializedErrorMethods called before init()
TeleneuraAuthErrorHTTP 401 — invalid or revoked API key
TeleneuraValidationErrorHTTP 400 — missing required JSON fields
TeleneuraTransportErrorOther HTTP / network failures

Background auto-batches log failures when enable_debug_logs=True; call flush() to surface transport errors synchronously.


PyTorch example

import torch
from teleneura import Teleneura, TeleneuraConfig

teleneura = Teleneura()
teleneura.init(TeleneuraConfig(
    api_key="ys_test_…",
    base_url="http://localhost:3001",
    batch_size=16,
    platform_label="PyTorch CUDA",
))

model = torch.jit.load("model.pt").eval()
inputs = torch.randn(1, 3, 224, 224)

with torch.inference_mode():
    with teleneura.track_inference("torchscript-resnet"):
        _ = model(inputs)

teleneura.flush()
teleneura.close()

Related