mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-11-01 10:19:34 +00:00
### 🛠 Fixes `KeyError: 'content'` when using `stream=False` #### 🔍 Problem When calling the chat API with `stream=False`, the code attempts to access `msg[-1]["content"]` without verifying if the key exists. This causes a `KeyError` when the message structure does not contain `"content"`. This issue was discussed in [#4885](https://github.com/infiniflow/ragflow/issues/4885), where we analyzed the root cause. The error does not occur with `stream=True`, as the response is processed differently. #### ✅ Solution - **Logging Fix:** - Before accessing `msg[-1]["content"]`, we check if the key exists. - If it does not exist, a default value (`"[content not available]"`) is used to prevent errors. - **Structural Fix in `msg` Construction:** - Ensured that every message in `msg` contains the `"content"` key, even if empty. - This fixes the issue at its root and ensures consistent behavior between `stream=True` and `stream=False`. #### 🔄 Impact - Prevents the `KeyError` without affecting normal application flow. - Ensures the integrity of the `msg` structure, avoiding future failures. ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)