Registry / ai-ml / onnxscript

onnxscript

JSON →
library0.7.1pypypi✓ verified 24d ago

ONNX Script is a Python library that enables developers to naturally author ONNX functions and models using a subset of Python. It provides tools to translate Python functions into serialized ONNX graphs, offering an expressive, simple, and debuggable way to define ONNX models. The library is actively maintained with frequent patch releases addressing bug fixes and minor improvements.

pip install onnxscript
INSTALL
IMPORT
SIG · ONNXSCRIPT
O
onnxscript
ai-mlpythonv0.7.1
Install
12.5s avg
Import
2008ms
Disk
263MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.7.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
build_error
glibc
py 3.103.95 runs
installs and imports cleanly · install 12.5s · import 2.008s · 243MB
263MB installed
● package 263MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

script
from onnxscript import script
The primary decorator to mark a Python function for ONNX conversion.
opsetXX
from onnxscript import opset15 as op
Import specific ONNX opsets (e.g., opset15, opset17) to access ONNX operators as Python functions.
FLOAT
from onnxscript.onnx_types import FLOAT
Used for type annotations to specify ONNX tensor types. Other types like INT64, BOOL are also available.

This quickstart demonstrates defining a simple ONNX function `MatmulAdd` using the `@script` decorator and ONNX operators from `opset15`. It shows how to use type annotations for inputs and outputs, evaluate the function in eager mode, convert it to an ONNX ModelProto, and save it to a file. The example includes basic input data generation and ONNX model validation.

import onnx from onnxscript import script, FLOAT from onnxscript import opset15 as op import numpy as np # Define an ONNX function using the @script decorator @script() def MatmulAdd(X: FLOAT['N', 'K'], Wt: FLOAT['K', 'M'], Bias: FLOAT['M',]) -> FLOAT['N', 'M']: return op.MatMul(X, Wt) + Bias # Create some dummy input data x_data = np.random.rand(64, 128).astype(np.float32) wt_data = np.random.rand(128, 10).astype(np.float32) bias_data = np.random.rand(10,).astype(np.float32) # Evaluate the ONNX Script function in eager mode (for debugging/testing) result_eager = MatmulAdd(x_data, wt_data, bias_data) print(f"Eager mode output shape: {result_eager.shape}") # Convert the ONNX Script function to an ONNX ModelProto model_proto = MatmulAdd.to_model_proto( (x_data, wt_data, bias_data), # Example inputs for tracing shapes output_names=['output'] ) # Save the ONNX model onnx_file_path = "matmul_add_model.onnx" onnx.save(model_proto, onnx_file_path) print(f"ONNX model saved to {onnx_file_path}") # Optionally, check the model for validity try: onnx.checker.check_model(model_proto) print("ONNX model is valid!") except onnx.checker.ValidationError as e: print(f"ONNX model validation error: {e}")
Debug
Known issues
breakingIn v0.6.0, the `.param_schemas` and `schema` properties of `ONNXFunction` were removed. They are replaced by the more flexible `.op_signature` property.
fix
Migrate code to use `ONNXFunction.op_signature` for accessing operator signatures.
affects: >=0.6.0
breakingIn v0.5.5, a change to the constant folding pass resulted in the creation of initializers instead of constant nodes. This might affect downstream tools or expectations regarding the ONNX graph structure.
fix
Review models optimized with constant folding to ensure compatibility with tools expecting constant nodes. Adapt parsing logic if necessary.
affects: >=0.5.5
gotchaONNX Script only supports a *subset* of Python. Not all Python language constructs (e.g., complex control flows, arbitrary data structures) can be translated into valid ONNX graphs, which can lead to unexpected errors during scripting.
fix
Refer to the official documentation for the supported Python subset. Design ONNX functions with the ONNX operator set in mind, focusing on numerical and tensor operations.
affects: All
gotchaThe eager mode evaluation of ONNX Script functions is primarily intended for debugging and understanding the function's behavior within Python. It is not optimized for performance and should not be used for high-performance inference.
fix
For production inference, always export the ONNX Script function to an ONNX model and use a high-performance ONNX runtime (e.g., ONNX Runtime).
affects: All
gotchaExplicit type annotations for inputs, outputs, and attributes are crucial when defining functions with `@script()`. Missing or incorrect annotations (e.g., for tensor types, shapes, or attribute types like `int`, `float`) can lead to conversion errors or incorrect ONNX graph generation.
fix
Always provide clear and correct type annotations, leveraging `onnxscript.onnx_types` for tensors and standard Python types for attributes, matching the expected ONNX operator signatures.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'onnxscript'
The `onnxscript` package is not installed or is not accessible in the current Python environment, often occurring when it's an indirect dependency for other libraries like PyTorch's ONNX exporter.
fix
Ensure `onnxscript` is installed in your active environment: `pip install --upgrade onnxscript` or `pip install -U onnx onnxscript` if using with `torch.onnx`.
ONNXRuntimeError: [ONNXRuntimeError] : 9 : INVALID_ARGUMENT : Node input type mismatch
There is a discrepancy between the data type or shape of the input provided to an ONNX model and the expected data type or shape defined in the model's graph when using ONNX Runtime for inference.
fix
Verify the ONNX model's input specifications (data types, shapes) and ensure your input data is correctly pre-processed to match these expectations. You can inspect the model's input signature using the `onnx` Python API.
ValueError: Required inputs (['X']) are missing from input feed (['misspelled'])
The input names provided to the ONNX Runtime session for inference do not match the input names expected by the ONNX model, often due to a typo or misunderstanding of the model's input signature.
fix
Inspect your ONNX model to identify the exact input names (e.g., using `model.graph.input` in the `onnx` Python API) and ensure the dictionary or feed used for inference provides data with these matching names.
TypeError: Descriptors cannot be created directly. If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
This error typically indicates an incompatibility between the `protobuf` library version and the generated Python files (`_pb2.py`) used by `onnx` (which `onnxscript` relies on), meaning the generated code is out of date for the installed `protobuf` version.
fix
Downgrade the `protobuf` package to a compatible version (e.g., `protobuf<=3.20.x`) or, if possible, regenerate the `_pb2.py` files with `protoc >= 3.19.0`.
pattern rewrite error: Unexpected onnxscript value type '<class 'onnxscript.ir.Value'>'
This error occurs when attempting to use the `onnxscript.rewriter` functionality with an unexpected or incompatible value type during pattern matching or replacement.
fix
Review the rewrite pattern and replacement function to ensure that all intermediate values and operations are correctly handled as `onnxscript` types (e.g., `onnxscript.ir.Value`) and that the pattern correctly identifies the nodes and attributes it intends to manipulate.
Upgrade
Version history
0.7.1latest on PyPI · released Jun 29, 2026
Audit
Dependencies
onnxrequiredCore dependency for ONNX graph representation and manipulation. ONNX Script builds upon the ONNX standard.
numpyrequiredCommonly used for array operations in Python functions that are then converted to ONNX.
onnx-irrequiredUtilized for the Abstract Syntax Tree (AST) conversion and intermediate representation in newer versions.
ml-dtypesoptionalHandles machine learning specific data types.
Agent activity
5 hits · last 30 days
node
4
Resources
onnxscript — pip install onnxscript · libregistry