{"id":24708,"library":"tflite","title":"tflite","description":"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.","status":"active","version":"2.18.0","language":"python","source_language":"en","source_url":"https://github.com/zhenhuaw-me/tflite","tags":["tensorflow","lite","machine-learning","flatbuffers","parser"],"install":[{"cmd":"pip install tflite","lang":"bash","label":"PyPI"}],"dependencies":[{"reason":"Required for deserializing TFLite FlatBuffer files","package":"flatbuffers","optional":false}],"imports":[{"note":"Model is a class within the module, not a top-level import; use 'import tflite' and access via tflite.Model","wrong":"from tflite import Model","symbol":"tflite.Model","correct":"import tflite\nmodel = tflite.Model.GetRootAsModel(buf, 0)"},{"note":"BuiltinCode is a method on OperatorCode object, not on the opcode integer. The import is 'import tflite'.","wrong":"opcode.BuiltinCode()","symbol":"OperatorCode.BuiltinCode","correct":"tflite.OperatorCode.BuiltinCode(opcode)"}],"quickstart":{"code":"import tflite\n\n# Load a TFLite model file (must be a .tflite file)\nwith open('/path/to/model.tflite', 'rb') as f:\n    buf = f.read()\n\n# Parse the model\nmodel = tflite.Model.GetRootAsModel(buf, 0)\n\n# Access version\nprint('Model version:', model.Version())\n\n# Access operator codes\nfor i in range(model.OperatorCodesLength()):\n    opcode = model.OperatorCodes(i)\n    code = opcode.BuiltinCode()\n    print('Opcode', i, ':', tflite.BUILTIN_OPCODE2NAME.get(code, 'UNKNOWN'))","lang":"python","description":"Parses a TFLite file and prints each operator's name."},"warnings":[{"fix":"Align tflite version with the TensorFlow version used to create the model.","message":"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).","severity":"gotcha","affected_versions":"all"},{"fix":"Use 'rb' mode: open(path, 'rb').read()","message":"The model buffer must be read as bytes (binary mode). Passing a text string or reading as text will lead to parsing errors.","severity":"gotcha","affected_versions":"all"},{"fix":"Use tflite.OperatorCode.BuiltinCode() as documented, but watch for direct attribute access in newer flatbuffers.","message":"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.","severity":"deprecated","affected_versions":">=2.4.0"},{"fix":"Wrap usage in try/except blocks when dealing with untrusted files.","message":"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.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Use 'import tflite' and then 'tflite.Model.GetRootAsModel(...)'.","cause":"The user tried 'from tflite import Model' or accessed it differently. The correct import is 'import tflite' and then use tflite.Model.","error":"AttributeError: module 'tflite' has no attribute 'Model'"},{"fix":"Open the file in binary mode: open(path, 'rb').read()","cause":"Reading the file with open(path, 'r') returns a string, but the parser expects bytes.","error":"TypeError: expected bytes, str found"},{"fix":"Verify the file is a valid .tflite file; ensure it exists and is not empty.","cause":"The model buffer is empty or corrupted, so length checks fail.","error":"IndexError: list index out of range (or segfault in native code)"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}