What is YAML (YAML Ain’t Markup Language): Python For AI Explained




A python snake wrapped around a computer displaying a yaml file

YAML, an acronym for “YAML Ain’t Markup Language”, is a human-readable data serialization standard that can be used in conjunction with all programming languages. While it’s often used to write configuration files, YAML has many other exciting applications, such as sharing data between languages with different data structures or setting up multi-language environments. This article will delve into the depths of YAML, its uses, and its role in Python for AI.

YAML is a versatile language, capable of handling complex nested data structures, yet remaining simple enough to be read and written by humans. Its simplicity and readability make it a popular choice for configuration files, where clarity and ease of use are paramount. However, YAML’s potential extends far beyond this, particularly when it comes to Python and AI.

Understanding YAML

YAML is a data serialization language, which means it’s used to translate data structures into a format that can be stored or transmitted and then reconstructed later. In other words, it’s a way of writing down data in a way that a computer can understand. Unlike markup languages like HTML or XML, which are designed to annotate text with metadata, YAML is designed to represent data structures like lists, arrays, and objects.

YAML’s syntax is designed to be intuitive and easy to read, with a focus on visual clarity. It uses indentation to represent nested data structures, similar to Python, and allows for the use of comments, making it easier to understand the purpose and structure of the data.

YAML Syntax

YAML syntax is straightforward and easy to understand. It uses simple punctuation marks to denote different data structures. For instance, a colon (:) is used to create key-value pairs, a dash (-) is used to create list items, and a hash (#) is used for comments. Indentation is used to denote hierarchy and structure, similar to how it’s used in Python.

Here’s an example of a simple YAML document:

name: John Doe
age: 30
married: True
  - Jane
  - Jim

In this example, ‘name’, ‘age’, and ‘married’ are keys, and ‘John Doe’, ’30’, and ‘True’ are their respective values. ‘children’ is a list, denoted by the dash before each item.


YAML and JSON (JavaScript Object Notation) are both data serialization formats, and while they have a lot in common, there are some key differences. JSON is a subset of JavaScript and was designed to work well within that language. YAML, on the other hand, was designed to work well with all programming languages. JSON is less human-readable compared to YAML but is faster for a machine to parse and generate.

One of the key differences between YAML and JSON is the ability to write comments in YAML. This feature is particularly useful when writing configuration files or any other data that will be read and modified by humans. JSON, on the other hand, does not support comments.

YAML in Python

Section Image

Python has a built-in library for handling YAML files, called PyYAML. This library allows you to parse YAML files into Python objects, and vice versa, serialize Python objects into YAML files. This makes it easy to work with YAML files in Python, whether you’re reading configuration data, storing data, or sharing data between different parts of your program.

Here’s an example of how you can use PyYAML to read a YAML file:

import yaml

with open('config.yaml', 'r') as file:
    data = yaml.safe_load(file)


In this example, the ‘yaml.safe_load’ function is used to parse the YAML file into a Python object. The ‘with open’ statement is used to ensure that the file is properly closed after it’s no longer needed.

Writing YAML files in Python

Writing YAML files in Python is just as easy as reading them. You can use the ‘yaml.dump’ function to serialize a Python object into a YAML string, and then write that string to a file. Here’s an example:

import yaml

data = {
    'name': 'John Doe',
    'age': 30,
    'married': True,
    'children': ['Jane', 'Jim']

with open('config.yaml', 'w') as file:
    yaml.dump(data, file)

In this example, a Python dictionary is serialized into a YAML string using the ‘yaml.dump’ function, and then that string is written to a file.

YAML and Artificial Intelligence

YAML’s simplicity and human-readability make it a popular choice for configuration files, including those used in AI projects. For instance, in machine learning projects, YAML can be used to store and share hyperparameters for models. This allows for easy replication of experiments and sharing of results.

Additionally, YAML’s ability to represent complex data structures makes it a good choice for storing and sharing data used in AI. For instance, a YAML file could be used to store a dataset for a machine learning project, or to store the weights and biases of a neural network.

YAML in AI Configuration

YAML is often used in AI projects to store configuration data. This could include hyperparameters for machine learning models, settings for data preprocessing, or parameters for evaluation metrics. By storing this data in a YAML file, it can be easily shared and reused, making it easier to replicate experiments and share results.

Here’s an example of what a YAML configuration file for a machine learning project might look like:

  type: 'neural network'
  layers: 3
  neurons: [64, 128, 64]
  activation: 'relu'

  epochs: 100
  batch_size: 32
  learning_rate: 0.001

In this example, the configuration file contains information about the model and the training process. This information can be easily read and modified by humans, and can be parsed into a Python object using PyYAML.

YAML in AI Data Storage

YAML can also be used to store and share data used in AI projects. For instance, a YAML file could be used to store a dataset for a machine learning project. This could include the raw data, as well as any metadata, such as the names of the features or the target variable.

Similarly, a YAML file could be used to store the weights and biases of a neural network. This would allow the model to be easily saved and loaded, making it easier to share and reuse models.


YAML is a powerful tool for data serialization, and its simplicity and human-readability make it a popular choice for configuration files and data storage, particularly in the field of AI. Whether you’re working on a machine learning project, developing a neural network, or just need a way to store and share complex data structures, YAML is a great choice.

With Python’s built-in support for YAML through the PyYAML library, working with YAML files in Python is a breeze. Whether you’re reading data from a YAML file, writing data to a YAML file, or serializing Python objects into YAML, PyYAML makes it easy.

Share this content

Latest posts