Implementation:Microsoft Onnxruntime Session Run Nodejs
Appearance
| Field | Value |
|---|---|
| Implementation Name | Session_Run_Nodejs |
| Overview | Asynchronous execution of ONNX model inference using the session.run() method with named input tensors in Node.js. |
| Type | API Doc |
| Language | JavaScript |
| Domains | ML_Inference, JavaScript_Integration |
| Source Repository | microsoft/onnxruntime |
| Last Updated | 2026-02-10 |
Overview
Asynchronous execution of ONNX model inference using the session.run() method with named input tensors in Node.js. This method accepts a feeds dictionary mapping input names to ort.Tensor objects and returns a Promise resolving to a results dictionary mapping output names to output tensors.
API
session.run(feeds: Record<string, ort.Tensor>) -> Promise<Record<string, ort.Tensor>>
Source Code Reference
- Repository: microsoft/onnxruntime
- Primary Source: samples/nodejs/01_basic-usage/index.js:L24
The sample model in the basic usage example contains a single MatMul node with two inputs a (float32, 3x4) and b (float32, 4x3) and one output c (float32, 3x3).
Key Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| feeds | Record<string, ort.Tensor> | Yes | Object mapping model input names to ort.Tensor objects containing input data |
I/O Contract
| Direction | Name | Type | Description |
|---|---|---|---|
| Input | feeds | Record<string, ort.Tensor> | Dictionary of named input tensors; keys must match model input names |
| Output | results | Promise<Record<string, ort.Tensor>> | Promise resolving to a dictionary of named output tensors |
Usage Examples
Basic MatMul Inference
const ort = require('onnxruntime-node');
async function main() {
// Create session from model
const session = await ort.InferenceSession.create('./model.onnx');
// Prepare input tensors
const dataA = Float32Array.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
const dataB = Float32Array.from([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]);
const tensorA = new ort.Tensor('float32', dataA, [3, 4]);
const tensorB = new ort.Tensor('float32', dataB, [4, 3]);
// Build feeds object with model input names as keys
const feeds = { a: tensorA, b: tensorB };
// Run inference
const results = await session.run(feeds);
// Access output tensor
const dataC = results.c.data;
console.log(`data of result tensor 'c': ${dataC}`);
}
main();
Inspecting Output Metadata
const results = await session.run(feeds);
// Each output is an ort.Tensor with type, dims, data, and size
for (const [name, tensor] of Object.entries(results)) {
console.log(`Output '${name}': type=${tensor.type}, dims=${tensor.dims}, size=${tensor.size}`);
}
Key Details
- The feeds object keys must exactly match the input names defined in the ONNX model graph.
- The method is asynchronous; inference computation happens on native background threads via N-API.
- Input tensor shapes and types must match the model's expected input specifications.
- The returned results object maps output names to ort.Tensor instances containing the computed values.
- Each call to run() executes one complete forward pass through the model graph.
- The Node.js event loop remains responsive during inference computation.
Related Pages
- Principle:Microsoft_Onnxruntime_Nodejs_Inference_Execution
- Implementation:Microsoft_Onnxruntime_InferenceSession_Create_Nodejs
- Implementation:Microsoft_Onnxruntime_Ort_Tensor_Nodejs
- Implementation:Microsoft_Onnxruntime_Tensor_Data_Extraction_Nodejs
- Environment:Microsoft_Onnxruntime_Nodejs_Runtime_Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment