Create an int list feature to save as tfrecord in tensorflow?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
When storing training data in TFRecord format, integer arrays are usually written with an int64_list feature. This is common for token ids, class id sequences, and sparse categorical values. The main goal is creating examples that are easy to serialize, parse, and feed into a tf.data pipeline without type mismatches.
Build Integer Features for tf.train.Example
TensorFlow uses protocol buffer messages for TFRecord entries. For integer lists, wrap values in tf.train.Int64List.
Use Python int values or NumPy integer types that convert cleanly to int64.
Write TFRecord Files
Once examples are created, serialize each example and write bytes with TFRecordWriter.
This produces a binary file that is efficient for streaming during model training.
Parse Integer List Features During Input
Parsing is where many mistakes happen. FixedLenFeature is for fixed-length arrays, while VarLenFeature handles variable-length lists.
If list lengths vary, use padded_batch so tensors in a batch share shape.
Keep Schema Explicit and Stable
Treat feature names and types as a schema contract between writers and readers. If you rename a key or switch dtype without updating the parser, failures can be silent until training diverges.
A robust pattern is storing schema constants in one module used by both write and read code paths.
This reduces typo-driven bugs in larger pipelines.
Validate Round-Trip Data Integrity
After building a writer and parser, add a small round-trip check to confirm that values are preserved exactly. This is especially useful before launching expensive training jobs.
If parsed values differ from input, fix schema mismatches immediately. Round-trip tests catch feature key typos, dtype mismatches, and parsing bugs before they affect model metrics.
Production Pipeline Tip
For larger datasets, shard TFRecord outputs into multiple files such as train-00000-of-00010 style naming. Sharding improves parallel reads in distributed training jobs and reduces single-file bottlenecks. Keep shard counts proportional to dataset size so each file remains reasonably large but not unwieldy for upload and caching.
Common Pitfalls
One common issue is using tf.int32 in parsing while writing int64 lists. TensorFlow will raise type errors or require explicit casts.
Another pitfall is passing nested lists accidentally, for example a two-dimensional list into Int64List. That feature expects a flat sequence of integers.
People also use FixedLenFeature for variable-length sequences. This fails on examples with shorter or longer arrays.
A final issue is writing and reading from different feature names across scripts. Keep names centralized and test round-trip serialization early.
Summary
- Use
tf.train.Int64Listfor integer list features in TFRecord examples. - Serialize examples with
TFRecordWriterand parse with matching schema. - Use
VarLenFeaturefor variable-length integer sequences. - Convert sparse parsed values to dense tensors when needed.
- Keep feature names and dtypes stable across data pipeline stages.
Related reading
- Create keras callback to save model predictions and targets for each batch during training
- Creating a ragged tensor from a list of tensors
- Creating a tensorflow dataset that outputs a dict
- Creating BLEU loss method on tensorflow gives No gradient provided
- Create Bayesian Network and learn parameters with Python3.x
- Create MS COCO style dataset
- Create AWS Athena view programmatically
- Create new column based on values from other columns / apply a function of multiple columns, row-wise in Pandas

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.