Registry / serialization / voluptuous

voluptuous

JSON →
library0.16.0pypypi✓ verified 29d ago

Voluptuous is a Python data validation library designed for validating data schemas. It allows defining a desired data structure and then validating input data against that structure, raising detailed exceptions for mismatches. The current version is 0.16.0, and it maintains an active release cadence with regular bug fixes and minor feature additions.

pip install voluptuous
INSTALL
IMPORT
SIG · VOLUPTUOUS
V
voluptuous
serializationpythonv0.16.0
Install
1.7s avg
Import
69ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.16.0 · 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.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.074s · 18MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 0.064s · 19MB
16MB installed
● package 16MB
Code
Verified usage

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

Schema
✓ from voluptuous import Schema
Required
✓ from voluptuous import Required
Optional
✓ from voluptuous import Optional
All
✓ from voluptuous import All
Any
✓ from voluptuous import Any
Coerce
✓ from voluptuous import Coerce
In
✓ from voluptuous import In
Match
✓ from voluptuous import Match
Invalid
✓ from voluptuous import Invalid

This quickstart demonstrates how to define a schema with required and optional fields, apply various validators (type coercion, regex matching, range checks, custom lambdas), and handle validation errors. It shows how Voluptuous automatically coerces types and handles default values for missing optional fields.

from voluptuous import Schema, Required, Optional, All, Coerce, In, Match, Invalid import datetime # Define a schema for user data user_schema = Schema({ Required('id'): All(Coerce(int), lambda n: n > 0, msg='ID must be a positive integer'), Required('username', default='guest'): All(str, Match(r'^[a-zA-Z0-9_]+$'), msg='Invalid username'), Optional('email'): All(str, Match(r'^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$'), msg='Invalid email format'), Optional('age', default=18): All(Coerce(int), In(range(18, 100)), msg='Age must be between 18 and 99'), Optional('roles', default=['user']): [str], 'is_active': Coerce(bool), Optional('created_at', default=lambda: datetime.datetime.now()): Coerce(datetime.datetime) }) # Valid data example valid_data = { 'id': '123', 'username': 'john_doe', 'email': 'john@example.com', 'age': 30, 'is_active': True } # Invalid data example invalid_data = { 'id': 0, 'username': 'john doe', 'age': 'twenty', 'is_active': 'yes' # Coerce(bool) is lenient, will be True } # Validate data try: validated_data = user_schema(valid_data) print("\n--- Valid Data Validation ---") print("Original data:", valid_data) print("Validated data:", validated_data) print(f"Created at (default):") # validated_data['created_at'] print("\n--- Invalid Data Validation ---") print("Original data:", invalid_data) user_schema(invalid_data) # This will raise an Invalid exception except Invalid as e: print(f"Validation failed: {e}")
Debug
Known issues
breakingVoluptuous dropped support for Python 3.8 in version 0.15.0, and previously dropped Python 3.7 in 0.14.0. The current version (0.16.0) requires Python 3.9 or higher.
fix
Ensure your project is running on Python 3.9 or a newer compatible version.
affects: 0.15.0+
gotchaThere were bugs affecting the interaction between `ALLOW_EXTRA` (or `REMOVE_EXTRA`) and the `Any` validator, where extra fields might not be handled as expected or could lead to errors.
fix
Upgrade to Voluptuous 0.15.2 or later to get fixes for these interactions.
affects: 0.15.0, 0.15.1
gotchaA bug existed where `In` and `NotIn` validators could fail when used with unsortable containers (e.g., sets containing mixed types or custom objects without a defined comparison).
fix
Upgrade to Voluptuous 0.14.2 or later to resolve this issue.
affects: 0.12.1 - 0.14.1
gotchaThe `Remove` marker did not correctly remove keys that failed validation in some scenarios, leading to potentially invalid data remaining after validation attempts.
fix
Upgrade to Voluptuous 0.15.1 or later to ensure `Remove` behaves as expected with invalid keys.
affects: 0.15.0
Errors
Common errors & fixes
voluptuous.MultipleInvalid: required key not provided @ data['field_name']
The input data is missing a key that was explicitly defined as `Required` in the schema.
fix
Provide the missing key in the input data or change the schema's key definition from `Required` to `Optional` if the field is not mandatory.
voluptuous.MultipleInvalid: extra keys not allowed @ data['unexpected_key']
The input data contains a key that is not defined in the schema, and the schema does not allow unspecified keys by default.
fix
Remove the unexpected key from the input data or configure the schema to allow extra keys by passing `extra=voluptuous.ALLOW_EXTRA` to the `Schema` constructor.
voluptuous.MultipleInvalid: expected a string for dictionary value @ data['field_name']
The value provided for a field in the input data does not match the expected data type or validator specified in the schema.
fix
Adjust the input data's value to conform to the type or validation rule defined in the schema (e.g., provide a string instead of an integer), or modify the schema's validator.
voluptuous.SchemaError: 'Key(some_key, optional=False)' is not a valid element of the schema
A `Key` object (often created with `Required()` or `Optional()`) was incorrectly used directly as a dictionary value or element in the schema definition instead of as a dictionary key or within a validator.
fix
When defining a dictionary schema, use `Key('some_key', required=True)` or `Key('some_key', default=some_value)` as the key in the schema dictionary (e.g., `{Key('some_key'): str}`).
Upgrade
Version history
0.16.0latest on PyPI · released Dec 18, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
12
OpenAI (training)
1
Resources
voluptuous — pip install voluptuous · libregistry