Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: handle string input in _get_further_suggestion for backward comp…
…atibility

Fixes #1215

## Root Cause Analysis
- API endpoint /suggestions may receive string-type message
- _get_further_suggestion expected MessageList (list[dict])
- When string passed, string[-2:] returns last 2 chars
- Then msg['role'] causes TypeError: string indices must be integers

## Solution
**Short-term (this PR):** Add type check and auto-convert string to MessageList format

**Long-term (follow-up):** Add API input validation layer

## Changes
- Updated type hint: MessageList | str
- Added isinstance check for backward compatibility
- Log warning when conversion happens

## Test Plan
- [x] String input is converted to [{"role": "user", "content": ...}]
- [x] MessageList input still works
- [x] Warning logged for string input
- [x] No more TypeError

## Impact
- Fixes runtime crash
- Backward compatible
- Graceful degradation
  • Loading branch information
JasonOA888 committed Mar 12, 2026
commit d5ec009d5284c468dcbd08dd162c0d37a1c679f3
9 changes: 7 additions & 2 deletions src/memos/api/handlers/suggestion_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,24 @@

def _get_further_suggestion(
llm: Any,
message: MessageList,
message: MessageList | str,
) -> list[str]:
"""
Get further suggestion based on recent dialogue.

Args:
llm: LLM instance for generating suggestions
message: Recent chat messages
message: Recent chat messages (MessageList or string for backward compatibility)

Returns:
List of suggestion queries
"""
try:
# Handle backward compatibility: convert string to MessageList format
if isinstance(message, str):
logger.warning("Received string message, converting to MessageList format")
message = [{"role": "user", "content": message}]

dialogue_info = "\n".join([f"{msg['role']}: {msg['content']}" for msg in message[-2:]])
further_suggestion_prompt = FURTHER_SUGGESTION_PROMPT.format(dialogue=dialogue_info)
message_list = [{"role": "system", "content": further_suggestion_prompt}]
Expand Down