Implementation:Microsoft Onnxruntime InferenceSession Create Nodejs
Appearance
| Field | Value |
|---|---|
| Implementation Name | InferenceSession_Create_Nodejs |
| Overview | Asynchronous creation of an ONNX Runtime inference session from a model file or buffer in Node.js. |
| Type | API Doc |
| Language | JavaScript |
| Domains | ML_Inference, JavaScript_Integration |
| Source Repository | microsoft/onnxruntime |
| Last Updated | 2026-02-10 |
Overview
Asynchronous creation of an ONNX Runtime inference session from a model file or buffer in Node.js using the InferenceSession.create() factory method. This async method loads an ONNX model, parses the graph, performs graph optimizations, and returns a ready-to-use inference session.
API
ort.InferenceSession.create(arg: string | Uint8Array | ArrayBuffer, options?: { intraOpNumThreads?: number }) -> Promise<InferenceSession>
Import
const ort = require('onnxruntime-node');
// or
const { InferenceSession } = require('onnxruntime-node');
Source Code Reference
- Repository: microsoft/onnxruntime
- Primary Source: samples/nodejs/04_create-inference-session/index.js:L15-31
Key Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| arg | string / Uint8Array / ArrayBuffer | Yes | ONNX model source: file path string, Uint8Array buffer, or ArrayBuffer with offset/length |
| options.intraOpNumThreads | number | No | Number of threads for intra-operator parallelism (default: system-determined) |
| byteOffset | number | No | Byte offset when using ArrayBuffer (third positional argument) |
| byteLength | number | No | Byte length when using ArrayBuffer (fourth positional argument) |
I/O Contract
| Direction | Name | Type | Description |
|---|---|---|---|
| Input | arg | string / Uint8Array / ArrayBuffer | ONNX model file path or binary buffer containing the serialized model |
| Input | options | object (optional) | Session configuration options including intraOpNumThreads |
| Output | session | Promise<InferenceSession> | Promise resolving to an initialized InferenceSession ready for inference |
Usage Examples
Create from File Path
const ort = require('onnxruntime-node');
async function main() {
// Create session from a file path
const session = await ort.InferenceSession.create('./model.onnx');
console.log('Session created successfully');
}
main();
Create with Session Options
const ort = require('onnxruntime-node');
async function main() {
const options = { intraOpNumThreads: 1 };
const session = await ort.InferenceSession.create('./model.onnx', options);
console.log('Session created with single thread');
}
main();
Create from Uint8Array Buffer
const ort = require('onnxruntime-node');
const fs = require('fs');
const util = require('util');
async function main() {
// Read model file into a Node.js Buffer (Uint8Array)
const buffer = await util.promisify(fs.readFile)('./model.onnx');
const session = await ort.InferenceSession.create(buffer);
console.log('Session created from buffer');
}
main();
Create from ArrayBuffer with Offset
const ort = require('onnxruntime-node');
const fs = require('fs');
const util = require('util');
async function main() {
const buffer = await util.promisify(fs.readFile)('./model.onnx');
const arrayBuffer = buffer.buffer;
const offset = buffer.byteOffset;
const length = buffer.byteLength;
const session = await ort.InferenceSession.create(arrayBuffer, offset, length);
console.log('Session created from ArrayBuffer with offset');
}
main();
Key Details
- The create() method is asynchronous to avoid blocking the Node.js event loop during model loading and graph optimization.
- The factory pattern is used because JavaScript constructors cannot be asynchronous.
- When loading from a file path, the native layer reads the file directly without JavaScript involvement.
- When loading from a buffer, the model bytes are passed to the native layer via N-API.
- The session retains the optimized graph in memory for the lifetime of the session object.
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment