Skip to content

Support Literal[bool] #859

Closed
Closed

Description

@peku33

Description

Many APIs use something like {"success": true, data: ...}.

It will be really nice to be able to validate such boolean field against constant value when deserializing:

from typing import Literal

import msgspec


class ResponseData(msgspec.Struct):
    pass


class Response(msgspec.Struct):
    success: Literal[True]
    data: ResponseData


response = msgspec.convert(
    {
        "success": True,
        "data": {},
    },
    Response,
)

At the moment this raises:

TypeError: Literal may only contain None/integers/strings - typing.Literal[True] is not supported

Thank you 👍

Activity

  1. jacopoabramo commented on Jun 24, 2025

    @jacopoabramo
    Contributor

    why not just use bool directly?

    class Response(msgspec.Struct):
        success: bool
        data: ResponseData

    EDIT: I don't really see the point in using a literal for a boolean; perhaps there's a reason for when an API always sets a response to True, but then again using bool should be more than enough

  2. jack-mcivor commented on Jun 24, 2025

    @jack-mcivor

    Using post-init-processing could help here https://jcristharif.com/msgspec/structs.html#post-init-processing

    class Response(msgspec.Struct):
        success: bool
        data: ResponseData
        def __post_init__(self):
            if self.success is not True:
                raise ValueError("Expected success==true")
  3. rafalkrupinski commented on Jun 24, 2025

    @rafalkrupinski

    And if success is False, you'd rather get a validation error from msgspec? I'd find that misleading

  4. peku33 commented on Jun 24, 2025

    @peku33
    Author

    I'm coming straight from pydantic v2, in which I was able to model api response like this:

    class ApiResponseOk(BaseModel):
        success: Literal[True]
        data: SomeDataModel
    
    class ApiResponseError(BaseModel):
        success: Literal[False]
        errors: list[ApiResponseErrorItem]
    
    ApiResponse = RootModel[ApiResponseOk | ApiResponseError]

    I understand that msgspec does not allow to create union types, like in pydantic.

    And if success is False, you'd rather get a validation error from msgspec? I'd find that misleading

    If i receive error response I will then get "data field not found" error from msgspec, which is even worse then something like "expected success to be True, but is False".

    I assumed this would be easy, as we already have string and numeric literals.

  5. Siyet commented on Apr 8, 2026

    @Siyet
    Contributor

    Resolved in the community fork msgspec-arise: issue #6, released in v0.20.2.

  6. added 2 commits that reference this issue on Apr 9, 2026
    9b70de6
    dbdee05
  7. added a commit that references this issue on Apr 20, 2026
    3b2543b
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