Haricharanpanjwani commented on code in PR #813: URL: https://github.com/apache/burr/pull/813#discussion_r3567115254
########## tests/integrations/test_streaming_union_types.py: ########## @@ -0,0 +1,398 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +Tests for union type support in @streaming_action.pydantic() decorator. + +This module tests that the stream_type parameter accepts union types, +including both | operator (Python 3.10+) and typing.Union syntax. +""" + +import asyncio +import sys +from typing import AsyncGenerator, Generator, List, Optional, Tuple, Union + +import pytest +from pydantic import BaseModel + +from burr.core.action import FunctionBasedAction, streaming_action +from burr.core.state import State +from burr.integrations.pydantic import PydanticTypingSystem, pydantic_streaming_action + + +# ============================================================================ +# Test Models +# ============================================================================ + + +class TextChunk(BaseModel): + """Represents a text chunk from a stream.""" + + text: str + chunk_id: int + + +class StructuredResult(BaseModel): + """Represents a structured result.""" + + summary: str + confidence: float + + +class Token(BaseModel): + """Represents a token from a stream.""" + + value: str + position: int + + +class StreamingInputState(BaseModel): + """Input state for streaming tests.""" + + prompt: str + mode: str = "default" + + +class StreamingOutputState(BaseModel): + """Output state for streaming tests.""" + + result: str + processing_time: float = 0.0 + + +# ============================================================================ +# Backward Compatibility Tests (Single Types) +# ============================================================================ + + +def test_streaming_action_single_model_type_backward_compat(): + """Test that single model types still work (backward compatibility).""" + + @streaming_action.pydantic( + reads=["prompt"], + writes=["result"], + state_input_type=StreamingInputState, + state_output_type=StreamingOutputState, + stream_type=TextChunk, # Single type (should still work) + ) + def act( + state: State, + ) -> Generator[Tuple[TextChunk, Optional[StreamingOutputState]], None, None]: + yield TextChunk(text="Hello", chunk_id=1), None + yield TextChunk(text="World", chunk_id=2), StreamingOutputState(result="Complete") + + # Verify the action was created successfully + assert hasattr(act, "bind") + assert hasattr(act, FunctionBasedAction.ACTION_FUNCTION) + + +def test_streaming_action_dict_type_backward_compat(): + """Test that dict type still works (backward compatibility).""" + + @streaming_action.pydantic( + reads=["prompt"], + writes=["result"], + state_input_type=StreamingInputState, + state_output_type=StreamingOutputState, + stream_type=dict, # dict type (should still work) + ) + def act( + state: State, + ) -> Generator[Tuple[dict, Optional[StreamingOutputState]], None, None]: + yield {"text": "Hello", "chunk_id": 1}, None + yield {"text": "World", "chunk_id": 2}, StreamingOutputState(result="Complete") + + # Verify the action was created successfully + assert hasattr(act, "bind") + assert hasattr(act, FunctionBasedAction.ACTION_FUNCTION) + + +# ============================================================================ +# Union Type Tests - typing.Union Syntax +# ============================================================================ + + +def test_streaming_action_typing_union_two_models(): Review Comment: The happy-path coverage here is good, but I think we’re still missing one or two failure-mode tests for the new validator. Adding invalid `stream_type` cases would make the review much easier because it would pin the exact boundary this PR is introducing. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
