Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Microsoft Onnxruntime Model Metadata Inspection

From Leeroopedia


Metadata

Field Value
Principle Name Model_Metadata_Inspection
Repository Microsoft_Onnxruntime
Source Repository https://github.com/microsoft/onnxruntime
Domain ML_Inference, Model_Optimization
Last Updated 2026-02-10
Workflow Python_Inference_Pipeline
Pair 3 of 6

Overview

Programmatic inspection of an ONNX model's input and output specifications at runtime.

Description

After loading a model into an InferenceSession, inspecting its metadata reveals the expected input and output names, shapes, and data types. This is essential for constructing correctly-shaped input data and interpreting outputs. The metadata inspection API provides access to NodeArg objects that describe each input and output tensor.

The relevant methods are:

  • session.get_inputs() -- Returns a list of NodeArg objects describing each model input.
  • session.get_outputs() -- Returns a list of NodeArg objects describing each model output.

Each NodeArg object exposes:

  • .name: str -- The symbolic name of the input or output tensor.
  • .shape: list -- The expected tensor shape, where None indicates a dynamic dimension.
  • .type: str -- The ONNX tensor type string (e.g., "tensor(float)").

This metadata is derived from the ONNX model's graph definition as recorded in docs/python/examples/plot_load_and_predict.py:L30-45.

Theoretical Basis

ONNX models declare typed inputs and outputs as NodeArg objects containing name, shape, and type information. This metadata is part of the ONNX specification's graph definition and is stored in the model protobuf.

Querying this metadata at runtime enables dynamic data preparation -- code that adapts to different models without hard-coding input specifications. This is particularly valuable in production systems that serve multiple models or when model inputs change across versions.

Dynamic dimensions (represented as None in the shape) indicate that the model supports variable-size inputs along that axis. The most common dynamic dimension is the batch dimension, allowing the model to process varying numbers of samples in a single inference call.

Usage

Model metadata is inspected immediately after session creation to determine data preparation requirements:

input_name = sess.get_inputs()[0].name
input_shape = sess.get_inputs()[0].shape
input_type = sess.get_inputs()[0].type
output_name = sess.get_outputs()[0].name

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment