tflite

raw JSON →
2.18.0 verified Fri May 01 auth: no python

A pure Python parser for TensorFlow Lite models (*.tflite) that extracts model metadata, operator codes, subgraphs, and tensors without requiring TensorFlow runtime. Current version 2.18.0 corresponds to TensorFlow 2.18.0 schema. Releases follow TensorFlow's major.minor versions, with occasional patches.

pip install tflite
error AttributeError: module 'tflite' has no attribute 'Model'
cause The user tried 'from tflite import Model' or accessed it differently. The correct import is 'import tflite' and then use tflite.Model.
fix
Use 'import tflite' and then 'tflite.Model.GetRootAsModel(...)'.
error TypeError: expected bytes, str found
cause Reading the file with open(path, 'r') returns a string, but the parser expects bytes.
fix
Open the file in binary mode: open(path, 'rb').read()
error IndexError: list index out of range (or segfault in native code)
cause The model buffer is empty or corrupted, so length checks fail.
fix
Verify the file is a valid .tflite file; ensure it exists and is not empty.
gotcha The package version must match the TensorFlow version that generated the model. Using tflite 2.10.0 to parse a model from TF 2.18.0 may fail due to schema changes (e.g., new operator codes).
fix Align tflite version with the TensorFlow version used to create the model.
gotcha The model buffer must be read as bytes (binary mode). Passing a text string or reading as text will lead to parsing errors.
fix Use 'rb' mode: open(path, 'rb').read()
deprecated OperatorCode.BuiltinCode() may be deprecated in future releases; check the README for compatibility handling. In TF 2.4.0+, the method exists but the schema may expose 'builtin_code' directly on the operator code object.
fix Use tflite.OperatorCode.BuiltinCode() as documented, but watch for direct attribute access in newer flatbuffers.
gotcha The package does not validate that the input file is a valid TFLite model. If the buffer is malformed, the GetRootAsModel call may succeed but subsequent accesses can raise exceptions.
fix Wrap usage in try/except blocks when dealing with untrusted files.

Parses a TFLite file and prints each operator's name.

import tflite

# Load a TFLite model file (must be a .tflite file)
with open('/path/to/model.tflite', 'rb') as f:
    buf = f.read()

# Parse the model
model = tflite.Model.GetRootAsModel(buf, 0)

# Access version
print('Model version:', model.Version())

# Access operator codes
for i in range(model.OperatorCodesLength()):
    opcode = model.OperatorCodes(i)
    code = opcode.BuiltinCode()
    print('Opcode', i, ':', tflite.BUILTIN_OPCODE2NAME.get(code, 'UNKNOWN'))