You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow omit_defaults to exclude fields when encoded value is {} (empty dict) #645
Although it is clear why the empty dict is being encoded, it is still desirable to avoid that, given that decoding absent fields does not cause any errors when the empty constructor (i.e. empty dict) is producing the expected default.
It would seem that recursive check with matches_default on subclasses of msgspec.Struct might do the job, but this would probably mean some hit on perfomance (?).
Perhaps there is a way to check the resulting '{}' value and omit it instead?
omit_defaults does not recursively match nested struct defaults, and it does not call a default_factory (here default_factory=Sub) during detection. This is by design: the check stays O(1) on the encode hot path and never invokes user code or walks nested structures. Recursively encoding a value and comparing it against the default would add real per-encode cost, which is exactly what this optimization avoids (same constraint as #1032).
Clarifying the default_factory limitation in the docs in #1076, closing as working-as-intended.
Activity
Siyet commentedon Jun 16, 2026
omit_defaultsdoes not recursively match nested struct defaults, and it does not call adefault_factory(heredefault_factory=Sub) during detection. This is by design: the check stays O(1) on the encode hot path and never invokes user code or walks nested structures. Recursively encoding a value and comparing it against the default would add real per-encode cost, which is exactly what this optimization avoids (same constraint as #1032).Clarifying the
default_factorylimitation in the docs in #1076, closing as working-as-intended.