ragflow/api
Mckennasora 7ee5e0d152
Fix KeyError in session listing endpoint when accessing conversation reference (#9419)
- Add type and boundary checks for conv["reference"] access
- Prevent KeyError: 0 when reference list is empty or malformed
- Ensure reference is list type before indexing
- Handle cases where reference items are None or missing chunks
- Maintains backward compatibility with existing data structures

This resolves crashes in /api/v1/agents/<agent_id>/sessions endpoint
when conversation reference data is not properly structured.

### What problem does this PR solve?

This PR fixes a critical `KeyError: 0` that occurs in the
`/api/v1/agents/<agent_id>/sessions` endpoint when the system attempts
to access conversation reference data that is not properly structured.

**Background Context:**
The `list_agent_session` method in `api/apps/sdk/session.py` assumes
that `conv["reference"]` is always a properly indexed list with valid
dictionary structures. However, in real-world scenarios, this data can
be:
- Not a list type (could be None, string, or other types)
- An empty list when `chunk_num` tries to access index 0
- Contains None values or malformed dictionary structures
- Missing expected "chunks" keys in reference items

**Impact Before Fix:**
When malformed reference data is encountered, the API crashes with:
```json
{
    "code": 100,
    "data": null,
    "message": "KeyError(0)"
}
```
**Solution:**
Added comprehensive safety checks including type validation, boundary
checking, null safety, and structure validation to ensure the API
gracefully handles all reference data formats while maintaining backward
compatibility.

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
- [ ] New Feature (non-breaking change which adds functionality)
- [ ] Documentation Update
- [ ] Refactoring
- [ ] Performance Improvement
- [ ] Other (please describe):
2025-08-13 09:23:52 +08:00
..
apps Fix KeyError in session listing endpoint when accessing conversation reference (#9419) 2025-08-13 09:23:52 +08:00
db Fix: remove doc error. (#9413) 2025-08-12 15:55:04 +08:00
utils Refactor:Improve the logic so that it does not decode base 64 for the test image each time (#9264) 2025-08-06 11:42:25 +08:00
__init__.py Update comments (#4569) 2025-01-21 20:52:28 +08:00
constants.py Refa: Implement centralized file name length limit using FILE_NAME_LEN_LIMIT constant (#8318) 2025-06-17 18:01:30 +08:00
ragflow_server.py Refa: change mcp_client module to rag/utils/conn (#8578) 2025-07-01 09:29:19 +08:00
settings.py Fix: bytes style image issue. (#9304) 2025-08-07 15:20:01 +08:00
validation.py Fix errors detected by Ruff (#3918) 2024-12-08 14:21:12 +08:00
versions.py Fix VERSION 2024-12-07 16:56:34 +08:00