Skip to content

Discriminated union with simple types (str) #480

@agoddijn-fern

Description

@agoddijn-fern

The following

Content:
      oneOf:
        - type: string
        - $ref: "#/components/schemas/ImageUrl"
        - $ref: "#/components/schemas/AudioUrl"
        - $ref: "#/components/schemas/DocumentUrl"
        - $ref: "#/components/schemas/BinaryContent"
      discriminator:
        propertyName: kind
        mapping:
          image_url: "#/components/schemas/ImageUrl"
          audio_url: "#/components/schemas/AudioUrl"
          document_url: "#/components/schemas/DocumentUrl"
          binary_content: "#/components/schemas/BinaryContent"
      description: Union type representing different content formats

Produces

class Content(RootModel[Union[str, ImageUrl, AudioUrl, DocumentUrl, BinaryContent]]):
    root: Union[str, ImageUrl, AudioUrl, DocumentUrl, BinaryContent] = Field(
        ...,
        description='Union type representing different content formats',
        discriminator='kind',
    )

Which is not supported by pydantic

exception=TypeError("'str' is not a valid discriminated union variant; should be a 'BaseModel' or 'dataclass'")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions