Skip to content

msgspec.json.decode fails with KeyError when decoding dataclasses on Python 3.14 #876

Closed

Description

@mbsantiago

Description

Description:

When using Python 3.14, decoding a simple dataclass with msgspec.json.decode raises a KeyError. This occurs even with a minimal example.

Reproduction:

Save the following as main.py and run with: uv run main.py

# /// script
# requires-python = ">=3.14"
# dependencies = [
#     "msgspec",
# ]
# ///
from dataclasses import dataclass
import msgspec.json

@dataclass
class Dummy:
    field: str = "value"

c = Dummy()
msg = msgspec.json.encode(c)
msgspec.json.decode(msg, strict=True, type=Dummy)

Error Message:

Traceback (most recent call last):
  File "/xxxx/main.py", line 13, in <module>
    msgspec.json.decode(msg, strict=True, type=Dummy)
  File "/xxxx/.venv/lib/python3.14/site-packages/msgspec/_utils.py", line 254, in get_dataclass_info
    typ = hints[name]
          ~~~~~^^^^^^
KeyError: 'field'

Expected behavior:

msgspec.json.decode should successfully decode the dataclass instance, matching behavior on Python 3.13 and earlier versions.

Additional context:

This seems to stem from msgspec._utils.get_class_annotations, which returns an empty dict under Python 3.14:

import msgspec._utils as utils

assert utils.get_class_annotations(Dummy) == {}

Environment:

  • Python: 3.14.0
  • msgspec: 0.19.0
  • Installed via: uv
  • OS: Linux

Activity

  1. mbsantiago commented on Oct 13, 2025

    @mbsantiago
    Author

    I’m sorry, I just noticed that an existing PR (#852 ) already addresses this issue.
    I tested my minimal reproduction against that branch and can confirm that the proposed fix resolves the problem.

    I’ll leave this issue open for now since the PR hasn’t been merged yet — but once it is, this should be fully resolved.

  2. return42 commented on Oct 17, 2025

    @return42

    I’ll leave this issue open for now since the PR hasn’t been merged yet — but once it is, this should be fully resolved.

    Duplicate of #651

    I tested my minimal reproduction against that branch and can confirm that the proposed fix resolves the problem.

    I think its better when you add your review to #852

  3. ofek commented on Oct 19, 2025

    @ofek
    Member

    Resolved by #852

  4. ofek commented on Nov 24, 2025

    @ofek
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions