Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Onnx Onnx Op Schema Python API

From Leeroopedia


Knowledge Sources
Domains Operator Definitions, Python API
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete tool for accessing ONNX operator schema definitions from Python provided by the ONNX library.

Description

The onnx/defs/__init__.py module is the Python API entry point for ONNX operator schema definitions. It bridges C++ operator definitions to Python by importing and wrapping the C++ bindings from onnx.onnx_cpp2py_export.defs.

The module defines three domain constants:

  • ONNX_DOMAIN ("") - the default ONNX domain
  • ONNX_ML_DOMAIN ("ai.onnx.ml") - the ML operator domain
  • AI_ONNX_PREVIEW_TRAINING_DOMAIN ("ai.onnx.preview.training") - preview training domain

It exposes several key functions:

  • has() - checks if a schema exists (wraps C.has_schema)
  • get_schema() - retrieves an operator schema by name and domain
  • get_all_schemas() - returns all registered schemas for the current opset version
  • get_all_schemas_with_history() - returns all schemas including historical versions
  • register_schema() - registers a user-provided OpSchema, automatically extending the domain version range if needed
  • deregister_schema() - removes a registered schema
  • onnx_opset_version() - returns the current opset version for the default "ai.onnx" domain
  • onnx_ml_opset_version() - returns the current opset version for the "ai.onnx.ml" domain
  • get_function_ops() - returns all operators defined as functions (those with function_body or context_dependent_function)

The module also enhances the OpSchema class with additional properties:

  • function_body - deserializes the function body from bytes to FunctionProto
  • non_deterministic - checks if the operator is non-deterministic
  • Attribute.default_value - deserializes attribute default values from bytes to AttributeProto

Custom __repr__ methods are added to OpSchema, OpSchema.FormalParameter, OpSchema.TypeConstraintParam, and OpSchema.Attribute for informative string representations.

Usage

Use this module to programmatically inspect ONNX operator definitions, retrieve schema information for validation or code generation, register custom operator schemas, and query the current opset versions. Import functions directly from onnx.defs for a clean interface.

Code Reference

Source Location

Signature

ONNX_DOMAIN: str = ""
ONNX_ML_DOMAIN: str = "ai.onnx.ml"
AI_ONNX_PREVIEW_TRAINING_DOMAIN: str = "ai.onnx.preview.training"

has = C.has_schema
get_schema = C.get_schema
get_all_schemas = C.get_all_schemas
get_all_schemas_with_history = C.get_all_schemas_with_history
deregister_schema = C.deregister_schema

def onnx_opset_version() -> int: ...
def onnx_ml_opset_version() -> int: ...
def get_function_ops() -> list[OpSchema]: ...
def register_schema(schema: OpSchema) -> None: ...

OpSchema = C.OpSchema
SchemaError = C.SchemaError

Import

from onnx.defs import (
    get_schema,
    get_all_schemas,
    get_all_schemas_with_history,
    has,
    register_schema,
    deregister_schema,
    onnx_opset_version,
    get_function_ops,
    OpSchema,
    SchemaError,
    ONNX_DOMAIN,
    ONNX_ML_DOMAIN,
)

I/O Contract

Inputs

Name Type Required Description
name str Yes (for get_schema, has) The operator name to look up
domain str No The operator domain (defaults to ONNX_DOMAIN "")
version int No The opset version to query (for get_schema)
schema OpSchema Yes (for register_schema) The operator schema to register

Outputs

Name Type Description
OpSchema OpSchema An operator schema object with name, domain, type_constraints, inputs, outputs, attributes
has result bool Whether a schema exists for the given operator name and domain
onnx_opset_version result int The current opset version number for the default ONNX domain
get_function_ops result list[OpSchema] All operators that have function body definitions
get_all_schemas result list[OpSchema] All registered operator schemas for the current version

Usage Examples

from onnx.defs import get_schema, get_all_schemas, has, onnx_opset_version, get_function_ops

# Check the current opset version
version = onnx_opset_version()
print(f"Current ONNX opset version: {version}")

# Check if an operator exists
if has("Add"):
    print("Add operator is registered")

# Get a specific operator schema
schema = get_schema("Add")
print(schema.name)             # "Add"
print(schema.domain)           # ""
print(schema.since_version)    # version when Add was introduced
print(schema.doc)              # documentation string
print(schema.inputs)           # list of FormalParameter
print(schema.outputs)          # list of FormalParameter
print(schema.type_constraints) # list of TypeConstraintParam
print(schema.attributes)       # dict of Attribute

# Get a schema for a specific opset version
schema_v13 = get_schema("Add", 13)

# List all operator schemas
all_schemas = get_all_schemas()
for s in all_schemas:
    print(f"{s.domain}::{s.name} (since v{s.since_version})")

# Find operators defined as functions
function_ops = get_function_ops()
for op in function_ops:
    print(f"Function op: {op.name}")

Related Pages

Page Connections

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