diff --git a/litellm/litellm_core_utils/streaming_handler.py b/litellm/litellm_core_utils/streaming_handler.py index a9ef06c3b4f..a31400f9f6e 100644 --- a/litellm/litellm_core_utils/streaming_handler.py +++ b/litellm/litellm_core_utils/streaming_handler.py @@ -1129,6 +1129,7 @@ class CustomStreamWrapper: and model_response.choices[0].delta.role is not None ) or (getattr(model_response.choices[0].delta, "reasoning_items", None) is not None) + or (getattr(model_response.choices[0].delta, "refusal", None) is not None) ): return True else: diff --git a/tests/unit/litellm_core_utils/test_streaming_handler.py b/tests/unit/litellm_core_utils/test_streaming_handler.py index cf8de9c40f5..3be8813ba96 100644 --- a/tests/unit/litellm_core_utils/test_streaming_handler.py +++ b/tests/unit/litellm_core_utils/test_streaming_handler.py @@ -276,6 +276,31 @@ def test_is_chunk_non_empty(initialized_custom_stream_wrapper: CustomStreamWrapp ) +def test_is_chunk_non_empty_with_refusal( + initialized_custom_stream_wrapper: CustomStreamWrapper, +): + """Unit test if non-empty when the delta carries only a refusal""" + chunk: Final = { + "id": "chatcmpl-abc", + "object": "chat.completion.chunk", + "created": 1700000000, + "model": "m", + "choices": [ + { + "index": 0, + "delta": {"refusal": "I can't help"}, + "logprobs": None, + "finish_reason": None, + } + ], + } + assert initialized_custom_stream_wrapper.is_chunk_non_empty( + completion_obj=MagicMock(), + model_response=ModelResponseStream(**chunk), + response_obj=MagicMock(), + ) + + def test_is_chunk_non_empty_with_annotations( initialized_custom_stream_wrapper: CustomStreamWrapper, ):