File structure
A GGUF file is laid out as follows:- File magic
"GGUF"(4 bytes) - File version (
uint32_t) - Number of tensors (
int64_t) - Number of key-value pairs (
int64_t) - Key-value pairs (keys are length-prefixed strings; values are typed)
- Tensor metadata (name, shape, type, data offset)
- Tensor data blob (optional, alignment-padded)
Constants
| Constant | Value | Description |
|---|---|---|
GGUF_MAGIC | "GGUF" | Magic bytes at the start of every GGUF file |
GGUF_VERSION | 3 | Current format version |
GGUF_DEFAULT_ALIGNMENT | 32 | Default byte alignment for tensor data |
GGUF_KEY_GENERAL_ALIGNMENT | "general.alignment" | Optional metadata key that overrides the default alignment |
gguf_type enum
Types that can be stored as GGUF key-value data.All enum values are stored as
int32_t in the binary format. Booleans are stored as int8_t.Context lifecycle
gguf_init_empty
gguf_init_empty
Creates an empty GGUF context with no keys or tensors.Use this when building a new GGUF file from scratch. Free with
gguf_free.gguf_init_from_file
gguf_init_from_file
Reads a GGUF file and populates a context with its metadata and (optionally) tensor data.Returns a context on success, or
Path to the GGUF file to open.
Initialization parameters:
no_alloc(bool) — whentrue, tensor data is not loaded into memory; only metadata is read.ctx(struct ggml_context **) — when non-NULL, a newggml_contextis created and tensor data is allocated into it.
NULL on failure. Free with gguf_free.gguf_free
gguf_free
Key-value getters
gguf_get_n_kv
gguf_get_n_kv
Returns the total number of key-value pairs in the context.
gguf_find_key
gguf_find_key
gguf_get_key
gguf_get_key
gguf_get_kv_type
gguf_get_kv_type
Returns the type of the value stored at the given key ID.
gguf_get_arr_type
gguf_get_arr_type
For array-typed keys, returns the element type of the array.
gguf_get_arr_n
gguf_get_arr_n
Returns the number of elements in an array-typed key.
Typed value getters
Each getter reads a scalar value of the corresponding type. Calling a getter with the wrong type will abort the program.Common usage pattern
KV setters
Setters add a new key-value pair or overwrite an existing one. The new or updated pair is always placed at the end of the list.gguf_set_arr_data
gguf_set_arr_data
Creates or replaces an array key with
n elements of a primitive type.The GGUF context to modify.
The key name.
Element type. Must not be
GGUF_TYPE_ARRAY or GGUF_TYPE_STRING.Raw data. The function copies
n * sizeof(element) bytes.Number of elements in the array.
gguf_set_arr_str
gguf_set_arr_str
Tensor operations
gguf_get_n_tensors
gguf_get_n_tensors
Returns the total number of tensors registered in the context.
gguf_find_tensor
gguf_find_tensor
gguf_get_tensor_name
gguf_get_tensor_name
Returns the name of the tensor at the given index.
gguf_get_tensor_type
gguf_get_tensor_type
Returns the
ggml_type of the tensor at the given index.gguf_get_tensor_offset
gguf_get_tensor_offset
Returns the byte offset of the tensor’s data within the tensor data blob.Add
gguf_get_data_offset(ctx) to convert this to an offset from the start of the file.gguf_set_tensor_type
gguf_set_tensor_type
gguf_set_tensor_data
gguf_set_tensor_data
Sets the tensor data by copying from the provided pointer. The source must contain at least
gguf_get_tensor_size(ctx, id) bytes.The GGUF context.
Name of the tensor to update.
Source data. Must be at least
gguf_get_tensor_size bytes.Writing GGUF files
gguf_write_to_file
gguf_write_to_file
Writes the entire context (metadata and optionally tensor data) to a binary file.Returns
The GGUF context to serialize.
Output file path. The file is created or overwritten.
When
true, only the header, KV pairs, and tensor metadata are written — tensor data is omitted. Use this for the two-pass write patterns shown below.true on success.Write patterns
There are three supported ways to write a GGUF file:Single-pass write
Single-pass write
Write everything in one call.
Two-pass: metadata then data
Two-pass: metadata then data
Write metadata first, then append tensor data separately.
Pre-allocated header
Pre-allocated header
Reserve space for metadata at the front, write tensor data, then write metadata. Useful when tensor data is produced incrementally.
Metadata helpers
gguf_get_data_offset
gguf_get_data_offset
Returns the byte offset from the start of the file at which tensor data begins.Use this to seek to tensor data in the file:
fseek(f, gguf_get_data_offset(ctx), SEEK_SET).gguf_get_meta_size
gguf_get_meta_size
Returns the total size in bytes of the metadata section (header + KV pairs + tensor info + padding).This value equals
gguf_get_data_offset(ctx) for a fully populated context.