fix(pydantic): allow union of BaseModel types as stream_type#754
fix(pydantic): allow union of BaseModel types as stream_type#754Ghraven wants to merge 2 commits into
Conversation
Fixes apache#607 Broadens the `stream_type` parameter in `streaming_action.pydantic()` and `pydantic_streaming_action()` to accept a union of pydantic BaseModel types (e.g. `ModelA | ModelB`) in addition to a single `Type[BaseModel]` or `Type[dict]`. The internal validation logic already handles this at runtime; this change removes the overly strict static type annotation that rejected union types at the call site.
Fixes apache#607 Broadens the `stream_type` parameter in `streaming_action.pydantic()` and `pydantic_streaming_action()` to accept a union of pydantic BaseModel types (e.g. `ModelA | ModelB`) in addition to a single `Type[BaseModel]` or `Type[dict]`. The internal validation logic already handles this at runtime; this change removes the overly strict static type annotation that rejected union types at the call site.
|
Hi! Just checking in on this one. If the union type approach doesn't fit how you'd like to handle this, or if you'd prefer a different fix, I'm happy to adjust. Thanks so much for your work on Burr! |
|
I don't think loosening it to accept everything is correct? See the comment in the original ticket I just added |
|
You're absolutely right @elijahbenizzy — loosening to Happy to rework this PR along those lines — adding |
Fixes #607
What
Broadens the
stream_typeparameter type annotation instreaming_action.pydantic()andpydantic_streaming_action()to accept a union of pydanticBaseModeltypes in addition to a singleType[BaseModel]orType[dict].Why
Users who pass a union of models (e.g.
MyModel1 | MyModel2) hit a static type error at the call site because the parameter was typed asUnion[Type[BaseModel], Type[dict]], which only accepts a single concrete model type. The runtime validation logic in_validate_and_extract_signature_types_streaming()already handles union types correctly — this was a type annotation gap only.Changes
burr/core/action.py:stream_typeparameter instreaming_action.pydantic()widened to acceptobject(covers union types, which are not expressible asType[X]).burr/integrations/pydantic.py: Same change toPartialTypealias and the validator function signature.Testing
No behaviour change — this is a type annotation fix only. Existing tests continue to pass. Users can now write:
without a type error.