Skip to content

Commit c020b2d

Browse files
octo-patchocto-patch
andauthored
fix: yield content from final streaming chunk before stopping (#1198)
* fix: use configured midjourney_temp_folder instead of hardcoded ./tmp (fixes #1100) * fix: yield content from final streaming chunk before stopping (fixes #1169) When finish_reason is "stop", some third-party APIs include content in the same chunk. The previous code broke out of the loop before yielding that content, causing truncated responses. Also fix KeyError when neither finish_details nor finish_reason is present in chunk["choices"][0] — fall back to chunk.get("finish_details") instead of chunk["finish_details"] to avoid crashing. Co-Authored-By: Octopus <liyuan851277048@icloud.com> --------- Co-authored-by: octo-patch <octo-patch@github.com>
1 parent 6de06a5 commit c020b2d

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

‎modules/models/OpenAIVision.py‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,21 @@ def _decode_chat_response(self, response):
267267
elif "finish_reason" in chunk["choices"][0]:
268268
finish_reason = chunk["choices"][0]["finish_reason"]
269269
else:
270-
finish_reason = chunk["finish_details"]
271-
if finish_reason == "stop":
272-
break
270+
finish_reason = chunk.get("finish_details")
273271
try:
274-
if "reasoning_content" in chunk["choices"][0]["delta"]:
275-
reasoning_content = chunk["choices"][0]["delta"]["reasoning_content"]
272+
delta = chunk["choices"][0]["delta"]
273+
if "reasoning_content" in delta:
274+
reasoning_content = delta["reasoning_content"]
276275
else:
277276
reasoning_content = None
278-
yield chunk["choices"][0]["delta"]["content"], reasoning_content
277+
content = delta.get("content")
278+
if content is not None:
279+
yield content, reasoning_content
279280
except Exception as e:
280281
# logging.error(f"Error: {e}")
281-
continue
282+
pass
283+
if finish_reason == "stop":
284+
break
282285
except Exception:
283286
traceback.print_exc()
284287
print(f"ERROR: {chunk}")

0 commit comments

Comments
 (0)