Keras does not publish a separate format document; this page follows its saving
code: keras/src/saving/saving_lib.py
for .keras files, and
legacy_h5_format.py
with legacy/saving/saving_utils.py for the older .h5 model
files, all read 2026-11-12.
.keras archive
A .keras file is a ZIP archive with
these entries. The same files can also be written to a folder unzipped.
| Entry | Holds |
|---|---|
metadata.json | A JSON object with keras_version and date_saved, the local time in the form %Y-%m-%d@%H:%M:%S. |
config.json | The model as JSON, made by serialize_keras_object: records of the model, its layers and other saveables’ configuration, each with keys such as class_name, module and config. |
model.weights.h5 | The state of every saveable (layers, optimiser, loss and metrics), in HDF5. |
assets/ | Files a saveable writes as assets, for state that is not variables. |
Saveables are found through the model’s attributes, including lists, tuples and
dictionaries held in attributes. In model.weights.h5, each saveable’s
path is an HDF5 group holding a vars group; the variables are datasets
in it, and the model’s own variables are in a top-level vars group.
A bfloat16 variable is stored with a dtype attribute of
bfloat16.
When loading, Keras checks the sizes of ZIP members against expansion limits as a guard against ZIP bombs.
config.json records
From keras/src/saving/serialization_lib.py, models/functional.py,
models/sequential.py, layers/layer.py,
layers/core/dense.py, ops/operation.py and
trainers/trainer.py (Keras master, read 2026-11-13). The whole model is one
serialize_keras_object record, written with json.dumps.
| Value kind | JSON form |
|---|---|
Plain types, null | Unchanged. A list or tuple becomes an array of serialised items, a dict a dict with serialised values. |
Object with get_config() | {"module", "class_name", "config", "registered_name"}. For a class with a public Keras name such as keras.optimizers.Adam: module is keras.optimizers, class_name Adam, registered_name null. For a custom or registered class: module is the Python module (null instead of __main__), class_name its name, registered_name the registration name (for example my_package>MyLayer). config is the serialised get_config() dict. An object that appears more than once in the model gets the same integer shared_object_id in each record. |
| Extra keys | build_config (from get_build_config(); for a layer {"input_shape": …}, or a dict of input shapes) and compile_config (from get_compile_config(): for a compiled model the serialised compile settings, otherwise an empty dict, which is then written) are added to the record when the object has them. |
| Function | {"module", "class_name": "function", "config": name, "registered_name": name}; a lambda is {"class_name": "__lambda__", "config": {"value": …}} with the dumped code (loading it needs safe mode off). |
| Special values | __bytes__ (config.value UTF-8 text), __slice__ (start, stop, step), __ellipsis__ (empty config), __keras_tensor__ (shape, dtype, keras_history = layer name, node index, tensor index), __tensor__ and __numpy__ (value as nested lists and dtype; a 0-d numpy scalar is stored as a plain number), __typespec__ (spec_name, module, config list, registered_name null); a tf.TensorShape becomes a list (or null if unknown) and a tf.DType its name. |
- Functional model
config:name,trainable,layers(one record per layer from the earliest, each with the layer record plusnameandinbound_nodes: a list of{"args", "kwargs"}where the tensor arguments are__keras_tensor__records with node indices renumbered to the nodes kept),input_layersandoutput_layers(each[layer name, node index, tensor index]or a structure of them). A node with no input tensors is not written. A subclassed model that is not "functional-like" uses the plainModel.get_configinstead. - Sequential model
config: theModel.get_configentries,nameandlayers(the layer records, including the input layer when present). The legacy H5/SavedModel path also addsbuild_input_shape. - Layer
config(Layer.get_configon top ofOperation.get_config'sname):trainable,dtype(a serialised dtype policy) andactivity_regularizerwhen set, then the subclass's own.Denseaddsunits,activation,use_bias,kernel_initializer,bias_initializer,kernel_regularizer,bias_regularizer,kernel_constraint,bias_constraint,quantization_configand, when a LoRA rank is set,lora_rankandlora_alpha. Other layers' configs are defined by each layer class; a class that does not implementget_configcan have one generated from its constructor arguments, else saving raises an error.
model.weights.h5, shards and assets
From keras/src/saving/saving_lib.py and layers/layer.py (Keras master,
read 2026-11-13).
- Traversal:
_save_statestarts at the model with the empty path. For each saveable that hassave_own_variablesit makes an HDF5 group at its path (the group is created only when something is written) holding a subgroupvars; the model's own variables go to a top-levelvarsgroup. A saveable already saved (by object identity) is skipped. It then visits children: for a Functional or Sequential model thelayerslist first, then every public attribute in sorted order (names not in a per-type skip set, such as the attributes a plainLayerhas). A child saveable is saved atpath/attribute; a list, tuple or dict of children is walked with each element atpath/attribute/snake_case_class_name, made unique by appending_1,_2… for repeats (the code says names are not used because auto-generated layer names differ between instances); a nested container is namedcontainer,container_1…. A path looks likelayers/dense_1/varsoroptimizer/vars. - Datasets: a layer's default
save_own_variablesstores its trainable variables followed by its non-trainable ones, each as dataset"0","1"… (the index). When a saveable has a stringnamethe groupvarsalso gets an attributename.bfloat16data is stored with an attributedtype=bfloat16on the dataset (HDF5 has no such type). Loading also accepts a file from TensorFlow 2.13, wherelayerswas named_layer_checkpoint_dependencies. - Sharded weights: a model saved with
max_shard_size(in GB, default 5 for the store) does not usemodel.weights.h5alone: the file name must end in.weights.json(otherwise Keras warns and renames it). The JSON file holds{"metadata": {"total_size": bytes}, "weight_map": {group path: shard file name or list of names}}. The shards are HDF5 files namedbase_NNNNN.weights.h5(five digits from 00000, the base being the file stem without.weights), each in the same layout as above. A group's weight map key is its HDF5 path, such as/layers/dense/vars; a group whose variables span shards lists all of them. A new shard starts when adding a variable would exceed the maximum; a single variable bigger than the maximum is an error. Sizes are computed as elements times dtype bits / 8.total_sizeis the sum of those sizes. Sharding applies tosave_weightsfiles; whether a full.kerasarchive can hold shards was not established. - Assets: a saveable with
save_assets(dir_path)writes files intoassets/path/(same path scheme). Paths that would leave the assets folder are rejected. The code read shows theIndexLookupfamily (StringLookup,IntegerLookup, andTextVectorizationthrough it) writing avocabulary.txtwith one token per line (special tokens included), only when the vocabulary was not given as an argument (then it is already inconfig.json).
Legacy .h5 model
Before .keras, a whole model was one HDF5 file:
| Where | Name | Holds |
|---|---|---|
| Root attributes | keras_version | The Keras version. |
backend | The backend name. | |
model_config | JSON: {"class_name": …, "config": …}, the model’s class and get_config(). | |
training_config | JSON compile settings and optimiser configuration, when the model was compiled and saved with its optimiser. | |
Group model_weights | Attributes | layer_names, backend, keras_version. |
| One group per layer | Named after the layer (in name order), with a weight_names attribute and one dataset per weight, named by the weight’s path. | |
top_level_model_weights | The model’s own variables, laid out the same way. | |
Group optimizer_weights | – | The optimiser’s variables, when the optimiser is included. |
HDF5 cannot hold attributes larger than 64,512 bytes in an object header, so a
long list attribute such as layer_names or weight_names is
split into numbered attributes: layer_names0,
layer_names1 and so on.