Implementation:Onnx Onnx Printer To Text
| Knowledge Sources | |
|---|---|
| Domains | Debugging, Model_Analysis |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for converting ONNX protobuf structures to human-readable text format, wrapping a C++ printer engine.
Description
The to_text function converts ModelProto, FunctionProto, or GraphProto objects into their ONNX text format representation. It is a Python wrapper around the C++ printer implementation (onnx_cpp2py_export.printer), which serializes the proto to bytes and then generates the text output. The function dispatches to model_to_text, function_to_text, or graph_to_text based on the input type.
Usage
Import this function when you need to inspect the structure of an ONNX model in a human-readable format. It is particularly useful after shape inference (to verify inferred types) or after model transformations (to verify correctness).
Code Reference
Source Location
- Repository: onnx
- File: onnx/printer.py
- Lines: 10-17
Signature
def to_text(proto: onnx.ModelProto | onnx.FunctionProto | onnx.GraphProto) -> str:
"""Convert an ONNX protobuf to its text representation.
Args:
proto: ModelProto, FunctionProto, or GraphProto to convert.
Returns:
Human-readable text representation of the protobuf.
Raises:
TypeError: If proto is not a supported type.
"""
Import
from onnx import printer
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| proto | ModelProto or FunctionProto or GraphProto | Yes | The protobuf structure to convert to text |
Outputs
| Name | Type | Description |
|---|---|---|
| return | str | Human-readable ONNX text format string |
Usage Examples
Print Model Structure
import onnx
from onnx import printer
model = onnx.load_model("model.onnx")
# Convert to text and print
text = printer.to_text(model)
print(text)
Print Graph Only
import onnx
from onnx import printer
model = onnx.load_model("model.onnx")
# Print just the graph (without model-level metadata)
graph_text = printer.to_text(model.graph)
print(graph_text)