fix: Improve error handling in aupdate_messages function to prevent empty message error (#5655)

* refactor: Improve error handling in aupdate_messages function

* Update src/backend/base/langflow/memory.py

Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>

* fix(tests): updating a non-existing message adds a new message now

- Updated the test name from `test_aupdate_nonexistent_message` to `test_aupdate_nonexistent_message_generates_a_new_message` for better clarity.
- Modified the assertion to expect that a new message is generated when attempting to update a nonexistent message, changing the expected length of the updated messages from 0 to 1.

* [autofix.ci] apply automated fixes

* fix(tests): update message handling in tests for non-existent messages

- Refactored tests to ensure that attempting to update a non-existent message raises a ValueError instead of creating a new message.
- Updated test names and assertions for clarity and accuracy, ensuring they reflect the expected behavior when handling messages that do not exist in the database.

---------

Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
anovazzi1 2025-01-20 13:21:19 -03:00 committed by GitHub
commit 0cf4f03400
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 9 deletions

View file

@ -144,7 +144,9 @@ async def aupdate_messages(messages: Message | list[Message]) -> list[Message]:
await session.refresh(msg)
updated_messages.append(msg)
else:
logger.warning(f"Message with id {message.id} not found")
error_message = f"Message with id {message.id} not found"
logger.warning(error_message)
raise ValueError(error_message)
return [MessageRead.model_validate(message, from_attributes=True) for message in updated_messages]
@ -255,7 +257,12 @@ async def astore_message(
msg = "All of session_id, sender, and sender_name must be provided."
raise ValueError(msg)
if hasattr(message, "id") and message.id:
return await aupdate_messages([message])
# if message has an id and exist in the database, update it
# if not raise an error and add the message to the database
try:
return await aupdate_messages([message])
except ValueError as e:
logger.error(e)
if flow_id and not isinstance(flow_id, UUID):
flow_id = UUID(flow_id)
return await aadd_messages([message], flow_id=flow_id)