From 53ab1fd08653c5b6355daba477884dd46e47351a Mon Sep 17 00:00:00 2001 From: chenhuawang-04 Date: Thu, 26 Feb 2026 15:13:24 +0800 Subject: [PATCH] fix: improve final image selection in stream processor When n=1, the stream processor locks onto the first image received as target. If a different image gets its final version first, completed >= n breaks the WebSocket loop, but the processor saves the target's medium-quality data as "final". Now checks if the target actually has is_final=True before selecting it. If not, falls back to the highest quality image available. Co-Authored-By: Claude Opus 4.6 --- app/services/grok/services/image.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/app/services/grok/services/image.py b/app/services/grok/services/image.py index ebcbc0d5..18dbf73c 100644 --- a/app/services/grok/services/image.py +++ b/app/services/grok/services/image.py @@ -541,22 +541,21 @@ class ImageWSStreamProcessor(ImageWSBaseProcessor): ) if self.n == 1: - if self._target_id and self._target_id in images: - selected = [(self._target_id, images[self._target_id])] + target_item = images.get(self._target_id) if self._target_id else None + if target_item and target_item.get("is_final", False): + selected = [(self._target_id, target_item)] + elif images: + selected = [ + max( + images.items(), + key=lambda x: ( + x[1].get("is_final", False), + x[1].get("blob_size", 0), + ), + ) + ] else: - selected = ( - [ - max( - images.items(), - key=lambda x: ( - x[1].get("is_final", False), - x[1].get("blob_size", 0), - ), - ) - ] - if images - else [] - ) + selected = [] else: selected = [ (image_id, images[image_id])