burr icon indicating copy to clipboard operation
burr copied to clipboard

Streaming Event type, type hint, should support union type

Open skrawcz opened this issue 11 months ago • 12 comments

Current behavior

When it's a union type it complains:

union = MyModel1 | MyModel2

@streaming_action.pydantic(
    reads=[...],
    writes=[...],
    state_input_type=..., 
    state_output_type=...
    stream_type=union, # type hint says this is invalid
)

because it is typed with :

stream_type: Union[Type["BaseModel"], Type[dict]],

Library & System Information

Latest burr

Expected behavior

It should be fine.

Additional context

The fix I think is to expand that stream type to handle a union of BaseModel types.

skrawcz avatar Dec 11 '25 19:12 skrawcz

@skrawcz if its open for external contributors, can i work on it!

AryanBagade avatar Dec 11 '25 19:12 AryanBagade

Labels migrated to new taxonomy (#692).

andreahlert avatar Mar 28 '26 19:03 andreahlert

So this I think doesn't work because it says it could be either or but the type that is "either of them" doesn't satisfy this. Union[A, B] is not a subtype of A or B.

Rather it should accept Union types directly -- does it actually detect per-instance rather than assume they're all the same?

elijahbenizzy avatar May 17 '26 17:05 elijahbenizzy

Hi,

I can work on this issue if it's not resolved yet.

Thank you!

ThyTran1402 avatar May 30 '26 00:05 ThyTran1402

Hi,

I can work on this issue if it's not resolved yet.

Thank you!

Hello! Feel free to give a try.

@Ghraven are you still workin on this?

Can you elaborate together?

andreahlert avatar Jun 02 '26 10:06 andreahlert

Thanks for checking in. I'm not actively working on this at the moment, so I don't want to block anyone else from taking it.

@ThyTran1402 please feel free to pick it up. My current understanding is that the fix should focus on accepting/recognizing Union[...] annotations directly instead of treating a union as needing to satisfy one concrete member type globally. I'd be happy to review or sanity-check an approach if that helps, but I'm fine stepping back so you can move it forward.

Ghraven avatar Jun 03 '26 00:06 Ghraven

Thanks @Ghraven. @ThyTran1402 assigned to you :)

andreahlert avatar Jun 03 '26 13:06 andreahlert

Hi @andreahlert,

I was able to find the root cause of this issue that stream_type parameter was defined with a restrictive type of annotation: On burr/core/action.py:

stream_type: Union[Type["BaseModel"], Type[dict]]

On burr/integrations/pydantic.py:

PartialType = Union[Type[pydantic.BaseModel], Type[dict]]

It only accepted:

  • A single Type[BaseModel] which is a class that is a BaseModel
  • The Type[dict] which is the dict type itself

But NOT union types like Model1 | Model2 or Union[Model1, Model2], which are represented as types.UnionType objects at runtime.

So, I think we need to expand the type definitions to accept union types by adding object to Union. I opened PR for this. Let me know what your thoughts about it.

Thank you!

ThyTran1402 avatar Jun 20 '26 10:06 ThyTran1402

Hi @skrawcz @andreahlert,

Can you please review the PR if you have times?

Thank you!

ThyTran1402 avatar Jul 02 '26 00:07 ThyTran1402

@ThyTran1402, this issue has been inactive for 20 days. Are you still working on it? Drop a comment to let us know.

If there is no update within 7 days, the assignment will be removed so someone else can pick it up (per the assignment policy).

github-actions[bot] avatar Jul 22 '26 10:07 github-actions[bot]

Removing assignment from @ThyTran1402 after 27 days of inactivity (per the assignment policy).

Feel free to comment if you want to pick this back up or if someone else wants to take it.

github-actions[bot] avatar Jul 29 '26 10:07 github-actions[bot]

I dug into where stream_type actually flows, and I think this is the missing piece here — there are really two parts, and the annotation is only one.

stream_type becomes the action's intermediate_result_type, which is used at runtime, in Application._validate_result (burr/core/application.py):

result_type = schema.intermediate_result_type()
if not isinstance(result, result_type):
    raise ValueError(...)

So to @elijahbenizzy's question — yes, it does an isinstance per result, and that's the part that matters for a union. Widening the annotation (what the open PRs do) makes stream_type = A | B type-check, but this isinstance(result, result_type) is where a union needs handling:

  • isinstance(result, A | B) (PEP 604) works on 3.10+.
  • isinstance(result, typing.Union[A, B]) isn't reliable across versions (TypeError: Subscripted generics cannot be used with class and instance checks).

The robust, version-independent way — which also gives the per-instance detection — is to check against the union's members:

import typing, types

def _matches(result, result_type):
    if typing.get_origin(result_type) in (typing.Union, types.UnionType):
        return isinstance(result, tuple(typing.get_args(result_type)))
    return isinstance(result, result_type)

i.e. a result is valid if it's any member of the union. As far as I can tell none of the current PRs touch application.py, so they'd accept the union annotation but leave this check unchanged.

Happy to put up a focused PR doing both — widen stream_type to accept unions and make _validate_result union-aware, with tests. Or if you'd rather fold the runtime part into one of the existing PRs, glad to help there. Which would you prefer?

adarshsm avatar Jul 31 '26 06:07 adarshsm