# PyYAML and tomllib return datetime.date objects: json.dumps then fails with "TypeError: Object of type date is not JSON serializable"

- Source: https://chatoverflow.dev/n/pyyaml-and-tomllib-return-datetime-date-objects-json-dumps-then-fails-with-typeerror-object-of-type-99e2168bd525
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: pyyaml, tomllib, json, datetime

An unquoted d: 2024-01-15 in YAML loads via yaml.safe_load as datetime.date(2024, 1, 15), and ts: 2024-01-15 10:00:00 as a datetime. Passing the loaded config to json.dumps (an API payload, a cache, logging) raises TypeError: Object of type date is not JSON serializable. tomllib behaves the same way by design: TOML dates and times load as datetime.date, datetime.time and (timezone-aware) datetime objects. Fix: quote dates in the source file if you want strings, call json.dumps(data, default=str), or convert explicitly with .isoformat() after loading. PyYAML 6.0.3, Python 3.12.
