Spox

raw JSON →
0.17.1 verified Sat May 09 auth: no python

A framework for constructing ONNX computational graphs programmatically. Current version 0.17.1, supports Python >=3.10, and follows a monthly/quarterly release cadence. Spox provides a high-level API to build, inline, and manipulate ONNX models with type-safe operators and shape inference.

pip install spox
error AttributeError: module 'spox' has no attribute 'make_model'
cause The function is named make_onnx_model, not make_model.
fix
Use from spox import make_onnx_model or correct the function name.
error ValueError: Failed to infer shape for op MatMul
cause Missing or mismatched shape information for an input or output tensor.
fix
Ensure all argument tensors have correct shapes; use spox.Tensor with dimension symbols or None for unknown.
error spox.exceptions.SpoxValueError: Operator 'GroupNormalization' not found in opset 'ai.onnx.v18'
cause group_normalization was removed from v18, v19, v20 in spox 0.14.0.
fix
Use later opset version (v21+) or alternate implementation.
breaking Removal of group_normalization constructor: spox.opset.ai.onnx.v18.group_normalization and its re-exports in v19, v20 removed in 0.14.0 due to upstream deprecation.
fix Use group_normalization from a later opset version (v21+) or implement manually.
deprecated Importing spox._future.operator_overloading triggers DeprecationWarning since 0.13.0. Consider using ndonnx as an alternative.
fix Replace usage with ndonnx.
gotcha Python version requirement: Spox 0.17.0+ requires Python >=3.10. Older versions may support down to 3.8.
fix Ensure Python version >=3.10, or pin spox<0.17.0 for 3.9 compat.

Construct an ONNX model for Y = A*X + B using Spox.

import spox
from spox import build, make_onnx_model
from spox.opset.ai.onnx import v17 as op

# Build a simple model: Y = A * X + B
def build_linear_model():
    A = spox.argument(spox.Tensor(spox.Float, (3, 3)))
    X = spox.argument(spox.Tensor(spox.Float, (3, None)))
    B = spox.argument(spox.Tensor(spox.Float, (1, None)))
    Y = op.add(op.matmul(A, X), B)
    return build(inputs={'A': A, 'X': X, 'B': B}, outputs={'Y': Y})

model = build_linear_model()
print(make_onnx_model(model).SerializeToString()[:100])