Registry / serialization / strictyaml

strictyaml

JSON →
library1.7.3pypypi✓ verified 24d ago

StrictYAML is a type-safe YAML parser and validator for Python, focusing on a restricted, unambiguous subset of the YAML specification. It prioritizes a clear API, strict validation, human-readable exceptions, and the ability to round-trip (read, modify, and write) YAML while preserving comments. The current version is 1.7.3, with an active but irregular release cadence of patches and minor versions.

pip install strictyaml
INSTALL
IMPORT
SIG · STRICTYAML
S
strictyaml
serializationpythonv1.7.3
Install
1.8s avg
Import
86ms
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.7.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.090s · 19.7MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.8s · import 0.082s · 20MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

load
from strictyaml import load
Map
from strictyaml import Map
Str
from strictyaml import Str
Int
from strictyaml import Int
Seq
from strictyaml import Seq
YAMLError
from strictyaml import YAMLError

This quickstart demonstrates how to parse a YAML string using a schema for type validation and casting, access parsed data, modify values, and handle potential YAMLError exceptions. It also shows the default behavior of parsing without a schema where all scalar values are treated as strings.

from strictyaml import load, Map, Str, Int, Seq, YAMLError yaml_snippet = """ name: Ford Prefect age: 42 possessions: - Towel - 'No. 2 pencil' """ # Define a schema for validation and type casting schema = Map({"name": Str(), "age": Int(), "possessions": Seq(Str())}) try: # Load YAML with the defined schema document = load(yaml_snippet, schema) print("Parsed data:", document.data) print(f"Name (string): {document['name'].data}") print(f"Age (int): {document['age'].data}") print(f"First possession: {document['possessions'][0].data}") # Modify a value and output YAML (comments are preserved) document['age'] = 43 print("\nModified YAML:\n", document.as_yaml()) except YAMLError as e: print(f"YAML Error: {e}") # Example without a schema (all scalars are strings by default) yaml_no_schema = load(yaml_snippet) print("\nParsed without schema (age is string):", yaml_no_schema.data['age'])
Debug
Known issues
breakingIn version 0.5, the default parse result of `load()` changed from directly returning a Python dict/list to returning a `YAML` object. To get the dict/list representation, users must now access the `.data` attribute of the returned object.
fix
Access the `.data` attribute (e.g., `load(yaml_str, schema).data`) to retrieve the Python dict/list.
affects: <0.5
gotchaStrictYAML intentionally refuses implicit typing (e.g., '42' is a string by default). Values are only type-cast according to an explicitly provided schema (e.g., `Int()`). This prevents common YAML surprises and security issues, but means it behaves differently from other YAML parsers.
fix
Always define a `schema` using `strictyaml` validators (e.g., `Int()`, `Float()`, `Bool()`) if you require typed data beyond strings, lists, and dicts.
affects: All versions
gotchaStrictYAML parses only a restricted subset of the full YAML 1.2 specification. Features like duplicate keys, explicit tags, anchors/references, and flow-style YAML (embedded JSON) are intentionally disallowed or unsupported to enhance security and readability.
fix
Adhere to the StrictYAML subset of YAML. Avoid features like `!!str` explicit tags, `&anchor` references, and compact JSON-like syntax within your YAML files.
affects: All versions
gotchaStrictYAML, by design, only parses YAML from strings, not directly from file paths or file-like objects. This is a deliberate choice for explicitness and security.
fix
Read the content of your YAML file into a string first, then pass that string to `strictyaml.load()`. Example: `with open('config.yaml', 'r') as f: yaml_string = f.read(); doc = load(yaml_string, schema)`.
affects: All versions
gotchaWhen no schema is provided to `strictyaml.load()`, all scalar values (numbers, booleans, dates) are interpreted as strings. Automatic type inference, common in other YAML libraries, is disabled.
fix
Provide a schema (`strictyaml.load(yaml_str, schema)`) with appropriate validators (`Str()`, `Int()`, `Bool()`, `Datetime()`, etc.) to enable type conversion.
affects: All versions
Errors
Common errors & fixes
MarkedYAMLError: Expected 'Str()' but got 'Map()'
The YAML document provides a mapping (object) where the StrictYAML schema expects a string, indicating a type mismatch during validation.
fix
Adjust the YAML content's structure or a specific value's type to conform to the defined schema, or modify the schema to accept the given YAML structure/type.

```python
from strictyaml import load, Map, Str

yaml_string_correct = "user_name: Alice"
schema = Map({"user_name": Str()})
loaded_yaml = load(yaml_string_correct, schema)

# Error case example:
# yaml_string_error = "user_name: {first: Alice, last: Smith}"
# loaded_yaml = load(yaml_string_error, schema) # This would raise the error
```
KeyError: 'non_existent_key' not found in document
An attempt was made to access a key within the parsed StrictYAML document that does not exist, either because it's missing from the YAML input or not defined in the schema.
fix
Ensure the key exists in the YAML document and is allowed by the schema. Use `get()` with a default value to handle optional keys safely, or define the key in the schema using `Optional()`.

```python
from strictyaml import load, Map, Str, Int, Optional

yaml_string = "name: Bob\nage: 30"
schema = Map({"name": Str(), "age": Int(), Optional("email"): Str()})
doc = load(yaml_string, schema)

# Correct access:
print(doc["name"].data)

# Accessing an optional key safely:
email_node = doc.get("email")
if email_node:
    print(email_node.data)
else:
    print("Email not provided.")

# Error case example:
# print(doc["phone"].data) # Assuming 'phone' is not in schema or YAML
```
MarkedYAMLError: while scanning an unquoted scalar
The input YAML string contains a scalar value that is not properly quoted or contains special characters (like a colon followed by a space) that require quoting, leading to a low-level parsing error.
fix
Correct the YAML syntax by ensuring that strings containing special characters are enclosed in single or double quotes, and that indentation and other YAML syntax rules are followed.

```python
from strictyaml import load, Map, Str

# Corrected YAML: quoting a string with a colon
yaml_string_correct = "message: 'This contains a colon: and other stuff'"
schema = Map({"message": Str()})
loaded_yaml = load(yaml_string_correct, schema)

# Error case example (unquoted string with special characters):
# yaml_string_error = "message: This contains a colon: and other stuff"
# loaded_yaml = load(yaml_string_error, schema) # This would raise the error
```
TypeError: load() missing 1 required positional argument: 'yaml_string'
The `strictyaml.load()` function was called without providing the necessary `yaml_string` argument, which is the YAML content to be parsed.
fix
Always provide the YAML content as the first argument to the `strictyaml.load()` function.

```python
from strictyaml import load, Map, Str

# Correct usage with yaml_string
yaml_string = "item: Apple"
schema = Map({"item": Str()})
doc = load(yaml_string, schema)

# Error case example:
# doc = load(schema=schema) # This would raise the TypeError
```
Upgrade
Version history
1.7.3latest on PyPI · released Mar 10, 2023
Audit
Dependencies
python-dateutilrequiredUsed for datetime parsing and validation within schemas.
Agent activity
28 hits · last 30 days
node
16
OpenAI (training)
1
Resources
strictyaml — pip install strictyaml · libregistry