Skip to content

fix: handle string input in _get_further_suggestion#1218

Open
JasonOA888 wants to merge 1 commit intoMemTensor:mainfrom
JasonOA888:fix/suggestion-typeerror-string-input
Open

fix: handle string input in _get_further_suggestion#1218
JasonOA888 wants to merge 1 commit intoMemTensor:mainfrom
JasonOA888:fix/suggestion-typeerror-string-input

Conversation

@JasonOA888
Copy link

Fixes #1215

Root Cause

_get_further_suggestion expected MessageList but received string, causing TypeError.

Solution

  • Add isinstance check for backward compatibility
  • Convert string to MessageList format with warning log

Changes

  • src/memos/api/handlers/suggestion_handler.py

Test plan

  • String input converted to [{"role": "user", "content": ...}]
  • MessageList input still works
  • No more TypeError

…atibility

Fixes MemTensor#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: Title: _get_further_suggestion TypeError: string indices must be integers, not 'str'

1 participant