ragflow/api/apps
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
..
auth Fix: use jwks_uri from OIDC metadata for JWKS client (#8136) 2025-06-10 10:16:58 +08:00
sdk Fix KeyError in session listing endpoint when accessing conversation reference (#9419) 2025-08-13 09:23:52 +08:00
__init__.py Fix: Authentication Bypass via predictable JWT secret and empty token validation (#7998) 2025-06-05 12:10:24 +08:00
api_app.py Feat: add advanced document filter (#8723) 2025-07-09 09:33:11 +08:00
canvas_app.py Refa: list canvas (#9341) 2025-08-12 10:58:06 +08:00
chunk_app.py fix: use tenant_id of kb to get index name in rm chunk func (#8760) 2025-07-10 10:30:56 +08:00
conversation_app.py Fix: citation issue. (#9424) 2025-08-12 18:53:34 +08:00
dialog_app.py Feat: add meta data filter. (#9405) 2025-08-12 14:12:56 +08:00
document_app.py Feat: add meta data filter. (#9405) 2025-08-12 14:12:56 +08:00
file2document_app.py Feat: add advanced document filter (#8723) 2025-07-09 09:33:11 +08:00
file_app.py Refa: better MIME content type (#8801) 2025-07-11 18:47:19 +08:00
kb_app.py Feat: add meta data filter. (#9405) 2025-08-12 14:12:56 +08:00
langfuse_app.py Feat: add project_id and project_name to Langfuse API (#6481) 2025-03-25 10:36:34 +08:00
llm_app.py Refa: migrate chat models to LiteLLM (#9394) 2025-08-12 10:59:20 +08:00
mcp_server_app.py Perf: Enhance timeout handling. (#8826) 2025-07-15 09:36:45 +08:00
plugin_app.py Feat: Support tool calling in Generate component (#7572) 2025-05-16 16:32:19 +08:00
search_app.py Feat: add MCP dashboard operations (#8460) 2025-06-25 09:26:04 +08:00
system_app.py Feat: Add user registration toggle feature (#6327) 2025-03-21 09:38:15 +08:00
tenant_app.py Update team invite message (#4085) 2024-12-18 14:20:09 +08:00
user_app.py Fix: Authentication Bypass via predictable JWT secret and empty token validation (#7998) 2025-06-05 12:10:24 +08:00