Implementation:Onnx Onnx Checker Check Model
| Knowledge Sources | |
|---|---|
| Domains | Validation, Model_Integrity |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for validating ONNX model correctness provided by the ONNX checker module, wrapping a C++ validation engine.
Description
The check_model function validates an ONNX model against the specification. It is a Python wrapper around the C++ checker implementation (onnx_cpp2py_export.checker) exposed via nanobind. The function accepts either a ModelProto object (for models under 2GB) or a file path string (required for models over 2GB). When a ModelProto is passed, it is serialized to bytes and forwarded to the C++ checker. When a file path is passed, the C++ engine reads the file directly, avoiding the 2GB protobuf size limit.
The function raises checker.ValidationError on any validation failure, making it suitable for use in assertion-style checks.
Usage
Import this function whenever you need to verify model correctness. Call it after model construction, after version conversion, after model composition, or before deployment. Use full_check=True when you also want shape inference validation. For models with external data exceeding 2GB, pass the file path instead of the ModelProto.
Code Reference
Source Location
- Repository: onnx
- File: onnx/checker.py
- Lines: 121-169
Signature
def check_model(
model: onnx.ModelProto | str | bytes | os.PathLike,
full_check: bool = False,
skip_opset_compatibility_check: bool = False,
check_custom_domain: bool = False,
) -> None:
"""Check the consistency of a model.
Args:
model: Model to check. If model is a path, the function checks
via the path-based C++ API. For models >2GB, path is required.
full_check: If True, also runs shape inference check.
skip_opset_compatibility_check: If True, skip opset validation.
check_custom_domain: If True, validate all domains (not just built-in).
Raises:
ValidationError: If the model fails validation.
ValueError: If the serialized model exceeds 2GB without using a path.
"""
Import
from onnx import checker
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | ModelProto or str or bytes or PathLike | Yes | Model object or file path (path required for >2GB) |
| full_check | bool | No | Also run shape inference check (default: False) |
| skip_opset_compatibility_check | bool | No | Skip opset validation (default: False) |
| check_custom_domain | bool | No | Validate all domains (default: False) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | None | Returns nothing on success; raises ValidationError on failure |
Usage Examples
Basic Validation
from onnx import checker
# Validate an in-memory model
checker.check_model(model)
# Validate with shape inference
checker.check_model(model, full_check=True)
Path-based Validation for Large Models
from onnx import checker
# Required for models >2GB with external data
checker.check_model("large_model.onnx")
# With full shape inference check
checker.check_model("large_model.onnx", full_check=True)
Error Handling
from onnx import checker
try:
checker.check_model(model)
print("Model is valid")
except checker.ValidationError as e:
print(f"Validation failed: {e}")