onnxscript
Naturally author ONNX functions and models using a subset of Python
What it is and what it does
ONNX Script is a Python-to-ONNX compiler that lets you define ONNX functions and models using Python syntax decorated with the @script decorator. The decorator parses your Python code, traverses its abstract syntax tree, and builds an equivalent ONNX graph that can be saved and validated. It supports a subset of Python—not the full language—and includes eager-mode evaluation for debugging, where functions execute using ONNX Runtime as a shim to test intermediate results.
Beyond compilation, the package provides an ONNX Optimizer (constant folding, dead code elimination) and an ONNX Rewriter (pattern-based graph transformation using user-defined rules). The rewriter allows you to match subgraph patterns and replace them with optimized alternatives. All of this is built on top of onnx and onnx_ir, with support for modern Python versions (3.9 through 3.14).
Use it for:
- Author ONNX functions in readable Python instead of manually constructing protobuf graphs.
- Debug ONNX model logic by running functions in eager mode to inspect intermediate tensor values.
- Optimize ONNX models by applying constant folding and dead code elimination automatically.
- Rewrite ONNX graph patterns (e.g., replace a custom Erf-based GELU with a native Gelu operator).
- Round-trip between Python ONNX Script and ONNX graphs for model inspection and modification.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Write ONNX functions and models in Python syntax, then convert them to ONNX graphs; includes tools for optimization and pattern-based graph rewriting.
Yes. Active maintenance, low install friction, permissive MIT license, and no known vulnerabilities make this a safe choice. Install if you author or optimize ONNX models and want to work in Python syntax rather than protobuf. The eager-mode debugger is a real productivity gain for model development, though not for production inference.
Install
onnxscript on PyPI
pip
pip install onnxscriptuv
uv add onnxscriptpoetry
poetry add onnxscriptInstalling onnxscript
Before you install
Low friction: pure Python wheel with six runtime dependencies (numpy, onnx, packaging, and type-support libraries). Active maintenance with recent releases and 451 repository stars.
License in practice
MIT license permits unrestricted use, modification, and distribution in both open-source and commercial projects.
Quickstart
pip install onnxscript
from onnxscript import script, opset15 as op
@script()
def my_function(X):
return op.MatMul(X, X)
onnx_model = my_function.to_model_proto()
Verify before relying
- Whether the eager-mode runtime performance is suitable for your debugging workflow (docs note it is not optimized for speed).
- Exact scope of Python language subset supported beyond the examples shown.
Package facts
| License | MIT License Copyright (c) Microsoft Corporation Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to… (full text in the JSON record) (permissive) |
| Python support | supports the current Python release (>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 6 — ml_dtypes, numpy, onnx_ir, onnx, packaging, typing_extensions |
| Maintenance | actively maintained — 46 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 3,553,221/month — #2,579 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: onnxscript-0.7.1-py3-none-any.whl
Tags
More Artificial Intelligence packages
LiteLLM provides a unified Python interface to…
permissive · top 100 on PyPI
huggingface-hubClient library and CLI tool for downloading,…
permissive · top 100 on PyPI
langchainLangChain provides a framework for building…
permissive · top 1,000 on PyPI
hf-xethf-xet provides chunk-based deduplication and…
permissive · top 1,000 on PyPI
tokenizersTokenizers converts raw text into token…
permissive · top 1,000 on PyPI
transformersTransformers provides a unified framework for…
permissive · top 1,000 on PyPI
sam4onnxRewrites attributes and constants in ONNX model…
permissive · top 15,000 on PyPI
onnxoptimizerApplies graph-level optimizations to ONNX…
permissive · top 15,000 on PyPI
onnxsimSimplifies ONNX neural network models by…
permissive · top 15,000 on PyPI
onnx-ironnx-ir provides an in-memory intermediate…
permissive · top 5,000 on PyPI
sog4onnxGenerates minimal ONNX operation graphs by…
permissive · top 15,000 on PyPI
onnx-graphsurgeonONNX GraphSurgeon lets you programmatically…
permissive · top 15,000 on PyPI
sna4onnxAdds new operations (nodes) to ONNX model…
permissive · top 15,000 on PyPI
sng4onnxAutomatically generates and assigns operation…
permissive · top 15,000 on PyPI
soa4onnxAdds or modifies output nodes in ONNX model…
permissive · top 15,000 on PyPI
onnx2torchConverts ONNX models to PyTorch modules with a…
permissive · top 15,000 on PyPI