Implementation:Microsoft Onnxruntime Ort Tensor Nodejs
Appearance
| Field | Value |
|---|---|
| Implementation Name | Ort_Tensor_Nodejs |
| Overview | Construction of typed multi-dimensional tensors from JavaScript TypedArrays for ONNX Runtime inference. |
| Type | API Doc |
| Language | JavaScript |
| Domains | ML_Inference, JavaScript_Integration |
| Source Repository | microsoft/onnxruntime |
| Last Updated | 2026-02-10 |
Overview
Construction of typed multi-dimensional tensors from JavaScript TypedArrays for ONNX Runtime inference. The ort.Tensor constructor wraps a TypedArray or string array with type and shape metadata to create the fundamental data container used for both inputs and outputs of ONNX model inference.
API
new ort.Tensor(type?: string, data: TypedArray | string[], dims?: number[])
Import
const ort = require('onnxruntime-node');
// or
const { Tensor } = require('onnxruntime-node');
Source Code Reference
- Repository: microsoft/onnxruntime
- Primary Source: samples/nodejs/02_create-tensor/index.js:L9-37
Key Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| type | string | No | Tensor element type: 'float32', 'float64', 'int32', 'uint8', 'bool', 'string', etc. Can be omitted if inferable from data. |
| data | TypedArray / string[] | Yes | The underlying data buffer. Must match the declared type. |
| dims | number[] | No | Tensor shape as an array of dimension sizes. Use [] for scalar, omit for 1-D tensor. |
I/O Contract
| Direction | Name | Type | Description |
|---|---|---|---|
| Input | type | string (optional) | Element type identifier for the tensor data |
| Input | data | TypedArray / string[] | Raw data buffer with numerical or string values |
| Input | dims | number[] (optional) | Shape dimensions; product must equal data length |
| Output | tensor | ort.Tensor | Tensor object with .data, .dims, .type, and .size properties |
Usage Examples
Float32 Tensor with Explicit Type
const ort = require('onnxruntime-node');
// Create a [2, 3, 4] float32 tensor (24 elements total)
const buffer = new Float32Array(24);
buffer[0] = 0.1; // fill buffer data
const tensor = new ort.Tensor('float32', buffer, [2, 3, 4]);
console.log(tensor.type); // 'float32'
console.log(tensor.dims); // [2, 3, 4]
console.log(tensor.size); // 24
Type Inferred from TypedArray
const ort = require('onnxruntime-node');
// Type 'float32' is inferred from Float32Array
const tensor = new ort.Tensor(new Float32Array(24), [2, 3, 4]);
console.log(tensor.type); // 'float32'
Boolean Tensor
const ort = require('onnxruntime-node');
// Boolean tensor requires explicit type since both 'bool' and 'uint8' use Uint8Array
const boolData = new Uint8Array(2);
boolData[0] = 1; // true
boolData[1] = 0; // false
const tensor = new ort.Tensor('bool', boolData, [1, 2]);
Scalar Tensor
const ort = require('onnxruntime-node');
// Scalar tensor with empty dims array
const scalar = new ort.Tensor(new Float64Array(1), []);
scalar.data[0] = 1.0; // setting data after creation is allowed
console.log(scalar.dims); // []
console.log(scalar.size); // 1
1-D Tensor (Dims Omitted)
const ort = require('onnxruntime-node');
// When dims are omitted, a 1-D tensor is created
const tensor = new ort.Tensor(new Float32Array(100));
console.log(tensor.dims); // [100]
String Tensor
const ort = require('onnxruntime-node');
// String tensors use a JavaScript string array as data
const strTensor = new ort.Tensor('string', ['hello', 'world'], [1, 2]);
// Type 'string' can also be inferred
const strTensor2 = new ort.Tensor(['hello', 'world'], [1, 2]);
Key Details
- The constructor validates that data size matches the product of dimensions.
- Type/data mismatches (e.g., 'float64' type with Float32Array data) throw an error.
- Negative dimension values are not allowed and will throw an error.
- The .data property of the resulting tensor provides direct access to the underlying TypedArray.
- Setting values via .data[index] after construction is permitted.
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment