ONNX Protobuf Definitions for JavaScript
This `onnx-proto` package provides pre-compiled Protobuf definitions for the Open Neural Network Exchange (ONNX) format, specifically derived from the upstream `onnx/onnx.proto3` file. It enables JavaScript and TypeScript projects to interact with ONNX models by offering type-safe access to the ONNX IR (Intermediate Representation) structures, encompassing messages like `ModelProto`, `GraphProto`, `NodeProto`, and `TensorProto`. The current stable version is 8.0.1, reflecting the latest ONNX IR_VERSION 8 (corresponding to ONNX v1.10.2). Releases are automatically published to npm from the master branch whenever the underlying ONNX definition is updated or significant maintenance occurs, ensuring developers have timely access to the latest ONNX specification. Its key differentiator is being a direct, pre-compiled, and type-safe representation of the official ONNX protobuf schema, saving users the complexity of generating Protobuf code themselves for JavaScript and TypeScript environments.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'create')
cause Attempting to access a protobuf message constructor (e.g., `TensorProto`) that is nested within the `onnx` namespace directly, rather than through `onnx.TensorProto`.fixEnsure you are accessing the specific protobuf message via the `onnx` namespace, like `onnx.TensorProto.create()`. -
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax in a CommonJS environment without proper transpilation or configuration, indicating the runtime does not recognize ES modules.fixConfigure your `package.json` with `"type": "module"` if targeting a pure ES module environment, or use a bundler (e.g., Webpack, Rollup) with Babel or TypeScript to transpile to CommonJS if targeting older Node.js or specific browser environments. -
TypeError: onnx.TensorProto is not a constructor
cause Incorrect instantiation of a Protobuf message by trying to use `new onnx.TensorProto()` instead of the static `create` method.fixAlways instantiate Protobuf messages using their static `create` method: `onnx.TensorProto.create({ ... })`.
Warnings
- breaking Major version 8.0.0 updates the internal ONNX IR_VERSION to 8, corresponding to ONNX v1.10.2. This may introduce breaking changes if your application relies on specific older IR_VERSION structures or expects backward compatibility with models built against prior ONNX specifications.
- breaking Older major versions (e.g., 4.0.0, 3.1.0) also updated the underlying ONNX definition, potentially introducing breaking changes in the protobuf structures. Users should always check the upstream ONNX changelog relevant to the `IR_VERSION` noted in the `onnx-proto` version changelog.
- gotcha This package provides only the ONNX Protobuf *definitions* (types, enums, message constructors), not an ONNX runtime or inference engine. Attempting to run or evaluate ONNX models directly with this package will result in errors.
- gotcha Protobuf message instances are typically created using static factory methods (e.g., `create()`) rather than direct constructor calls (`new`). Using `new` can lead to runtime errors or unexpected behavior.
Install
-
npm install onnx-proto -
yarn add onnx-proto -
pnpm add onnx-proto
Imports
- onnx
const onnx = require('onnx-proto');import { onnx } from 'onnx-proto'; - TensorProto
import { TensorProto } from 'onnx-proto';import { onnx } from 'onnx-proto'; // Access via onnx.TensorProto - ModelProto
import ModelProto from 'onnx-proto';
import { onnx } from 'onnx-proto'; // Access via onnx.ModelProto
Quickstart
import { onnx } from 'onnx-proto';
// Create a simple ONNX TensorProto object
const tensorProto = onnx.TensorProto.create({
dims: [2, 2],
dataType: onnx.TensorProto.DataType.FLOAT,
floatData: [1.0, 2.0, 3.0, 4.0]
});
console.log('Created ONNX TensorProto:');
console.log(JSON.stringify(tensorProto.toJSON(), null, 2));
// Access an enum value from the ONNX definition
const floatDataType = onnx.TensorProto.DataType.FLOAT;
console.log(`\nONNX TensorProto.DataType.FLOAT value: ${floatDataType}`);
// This package provides type definitions and constructors for ONNX structures.
// It does not include an ONNX runtime. For model inference, a separate
// runtime library (e.g., onnxruntime-web) is required.