Skip to content

msgspec.Raw appears not to work with msgspec.yaml.decode #1136

Closed

Description

@pzarczynski

Question

I couldn't find any note in documentation suggesting msgspec.Raw cannot be used with YAML for decoding, so i assumed it is supported, but i get ValidationError. Is this expected behavior?

Minimal reproduction:

import msgspec

class Repro(msgspec.Struct):
    tools: dict[str, msgspec.Raw]

for value in (
    b"tools:\n  x: 1\n",
    b"tools:\n  x: hello\n",
    b"tools:\n  x: [a, b]\n",
    b"tools:\n  x: {a: 1}\n",
    b"tools:\n  x:\n    a: 1\n",
):
    msgspec.yaml.decode(value, type=Repro)

Result:

msgspec.ValidationError: Expected `any`, got `int` - at `$.tools[...]``
msgspec.ValidationError: Expected `any`, got `str` - at `$.tools[...]`
msgspec.ValidationError: Expected `any`, got `array` - at `$.tools[...]`
msgspec.ValidationError: Expected `any`, got `object` - at `$.tools[...]`
msgspec.ValidationError: Expected `any`, got `object` - at `$.tools[...]`

Environment:

  • Python: 3.12.13 (main, May 10 2026, 19:30:01) [Clang 22.1.3 ]
  • msgspec: 0.21.1

Activity

  1. Brohammad commented on Sep 2, 2026

    @Brohammad

    This is expected with the current architecture: msgspec.Raw needs access to original serialized bytes, which only the native json and msgpack C decoders provide. yaml.decode parses fully via PyYAML before validation, so byte spans are lost.

    Opened #1168 with:

    • A clearer error message instead of Expected \any`, got `int``
    • Documentation noting Raw delayed-decode is json/msgpack-only
  2. Brohammad commented on Sep 2, 2026

    @Brohammad

    Correction: PR is #1170 (not #1168).

  3. added a commit that references this issue on Sep 9, 2026
    8be1966
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