Skip to main content
The InferenceSession class is the main interface for loading and running ONNX models in JavaScript environments.

Importing

Browser (ES Modules)

Node.js

Creating Sessions

create()

Creates an inference session from a model.
Parameters:
  • path: Model file path, URL, or binary data
  • options: Optional session configuration
Returns: Promise resolving to InferenceSession

From URL

From ArrayBuffer

From Uint8Array

With Options

Session Properties

inputNames

Gets array of input names.
Example:

outputNames

Gets array of output names.
Example:

Running Inference

run()

Runs inference on the model.
Parameters:
  • feeds: Object mapping input names to tensors
  • options: Optional run configuration
Returns: Promise resolving to output tensors

Basic Usage

With Specific Outputs

With Run Options

SessionOptions

Configuration options for creating sessions.

executionProviders

Specifies execution providers in priority order.
Example:

graphOptimizationLevel

Sets graph optimization level.
Example:

executionMode

Controls sequential vs parallel execution.

Thread Configuration

Example:

Memory Options

Example:

Logging

Example:

Extra Configuration

Example:

Complete Examples

Image Classification (Browser)

Text Processing (Node.js)

Batch Processing

Error Handling

Performance Tips

  1. Reuse sessions: Create once, use many times
  2. Choose right EP: WebGPU for modern browsers, WASM for compatibility
  3. Enable optimizations: Use ‘all’ graph optimization level
  4. Batch when possible: Process multiple inputs together
  5. Pre-allocate tensors: Reuse tensor buffers for repeated inference

Browser Compatibility

See Also