Skip to main content
GGUF is the binary file format used by ggml to store models. A GGUF file contains a header, an arbitrary set of typed key-value metadata pairs, tensor metadata, and optionally the raw tensor data blob.

File structure

A GGUF file is laid out as follows:
  1. File magic "GGUF" (4 bytes)
  2. File version (uint32_t)
  3. Number of tensors (int64_t)
  4. Number of key-value pairs (int64_t)
  5. Key-value pairs (keys are length-prefixed strings; values are typed)
  6. Tensor metadata (name, shape, type, data offset)
  7. Tensor data blob (optional, alignment-padded)

Constants

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

Creates an empty GGUF context with no keys or tensors.
Use this when building a new GGUF file from scratch. Free with gguf_free.
Reads a GGUF file and populates a context with its metadata and (optionally) tensor data.
const char *
required
Path to the GGUF file to open.
struct gguf_init_params
required
Initialization parameters:
  • no_alloc (bool) — when true, tensor data is not loaded into memory; only metadata is read.
  • ctx (struct ggml_context **) — when non-NULL, a new ggml_context is created and tensor data is allocated into it.
Returns a context on success, or NULL on failure. Free with gguf_free.
Frees a GGUF context and all memory it owns.
struct gguf_context *
required
The context to free.

Key-value getters

Returns the total number of key-value pairs in the context.
Looks up a key by name and returns its integer ID.
const struct gguf_context *
required
The GGUF context to search.
const char *
required
The key name to look up.
Returns the key ID (>= 0) if found, or -1 if the key does not exist.
Returns the key name for a given key ID.
const struct gguf_context *
required
The GGUF context.
int64_t
required
A valid key ID in [0, gguf_get_n_kv(ctx)).
Returns the type of the value stored at the given key ID.
For array-typed keys, returns the element type of the array.
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.
Always call gguf_get_kv_type first and verify the type before calling a typed getter. Calling with a mismatched type aborts 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.
Creates or replaces an array key with n elements of a primitive type.
struct gguf_context *
required
The GGUF context to modify.
const char *
required
The key name.
enum gguf_type
required
Element type. Must not be GGUF_TYPE_ARRAY or GGUF_TYPE_STRING.
const void *
required
Raw data. The function copies n * sizeof(element) bytes.
size_t
required
Number of elements in the array.
Creates or replaces an array key with n string elements.
struct gguf_context *
required
The GGUF context to modify.
const char *
required
The key name.
const char **
required
Array of n null-terminated C strings. The function copies all strings.
size_t
required
Number of strings in the array.

Tensor operations

Returns the total number of tensors registered in the context.
Looks up a tensor by name and returns its integer ID.
const struct gguf_context *
required
The GGUF context to search.
const char *
required
The tensor name to look up.
Returns the tensor ID (>= 0) if found, or -1 if not found.
Returns the name of the tensor at the given index.
Returns the ggml_type of the tensor at the given index.
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.
Changes the stored type of a tensor. All tensor offsets following this tensor are recalculated immediately to keep the data contiguous.
struct gguf_context *
required
The GGUF context.
const char *
required
Name of the tensor to update.
enum ggml_type
required
New data type for the tensor.
Sets the tensor data by copying from the provided pointer. The source must contain at least gguf_get_tensor_size(ctx, id) bytes.
struct gguf_context *
required
The GGUF context.
const char *
required
Name of the tensor to update.
const void *
required
Source data. Must be at least gguf_get_tensor_size bytes.

Writing GGUF files

Writes the entire context (metadata and optionally tensor data) to a binary file.
const struct gguf_context *
required
The GGUF context to serialize.
const char *
required
Output file path. The file is created or overwritten.
bool
required
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.
Returns true on success.

Write patterns

There are three supported ways to write a GGUF file:
Write everything in one call.
Write metadata first, then append tensor data separately.
Reserve space for metadata at the front, write tensor data, then write metadata. Useful when tensor data is produced incrementally.

Metadata helpers

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).
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.
Serializes the metadata into a caller-provided buffer.
const struct gguf_context *
required
The GGUF context to serialize.
void *
required
Output buffer. Must be at least gguf_get_meta_size(ctx) bytes.