Skip to content

Annotated[Union..., strawberry.union(...)] fails #4207

@xo0de

Description

@xo0de

Describe the Bug

Using the new union syntax with Annotated[Union[...], strawberry.union("...")] behaves differently across Strawberry versions:

In versions < 0.289.3, the schema fails with:

ValueError: Invalid Schema. Errors:
❌ Union type ItemResponse must define one or more member types.

In versions >= 0.289.3, the empty-union error is gone, but schema still fails when the union is used inside list[T] or a generic like Listing[T]:

TypeError: Unexpected type 'typing.Annotated[...]'

So the new syntax still cannot be used reliably in generic list contexts.

Minimal Repro:

import strawberry
from typing import Annotated, Generic, TypeVar, Union

T = TypeVar("T")

@strawberry.type
class Listing(Generic[T]):
    items: list[T]

@strawberry.type
class Item:
    id: int

@strawberry.type
class ItemNotFound:
    item_id: int

ItemResponse = Annotated[
    Union[Item, ItemNotFound],
    strawberry.union("ItemResponse"),
]

@strawberry.type
class Items(Listing[ItemResponse]):
    my_items: list[ItemResponse]

@strawberry.type
class Query:
    @strawberry.field
    def items(self) -> Items:
        return Items(items=[], my_items=[])

schema = strawberry.Schema(query=Query)

Behavior in <0.289.3 (e.g. 0.283.3):

ValueError: Invalid Schema. Errors:
❌ Union type ItemResponse must define one or more member types.

Behavior in >=0.289.3:

TypeError: Unexpected type 'typing.Annotated[typing.Union[__main__.Item, __main__.ItemNotFound], <strawberry.types.union.StrawberryUnion object at 0x2809ea8>]'

TypeError: Items fields cannot be resolved. Unexpected type 'typing.Annotated[typing.Union[__main__.Item, __main__.ItemNotFound], <strawberry.types.union.StrawberryUnion object at 0x2809ea8>]'

Workaround -> use old‑style union declaration:

ItemResponse = strawberry.union("ItemResponse", (Item, ItemNotFound))

or

ItemResponse = Annotated[
    Union[Item, ItemNotFound],
    strawberry.union("ItemResponse", (Item, ItemNotFound)),
]

System Information

  • Python version: 3.11 / 3.13 (Pyodide)
  • Strawberry version (if applicable): strawberry-graphql==0.289.3 (also observed in <0.289.3)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions