diff --git a/src/backend/base/langflow/memory.py b/src/backend/base/langflow/memory.py index c6ec20348..cfa83147f 100644 --- a/src/backend/base/langflow/memory.py +++ b/src/backend/base/langflow/memory.py @@ -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) diff --git a/src/backend/tests/unit/test_messages.py b/src/backend/tests/unit/test_messages.py index b1eb7b606..70f7dce6f 100644 --- a/src/backend/tests/unit/test_messages.py +++ b/src/backend/tests/unit/test_messages.py @@ -195,26 +195,27 @@ async def test_aupdate_multiple_messages(created_messages): @pytest.mark.usefixtures("client") -async def test_aupdate_nonexistent_message(): +async def test_aupdate_nonexistent_message_generates_a_new_message(): # Create a message with a non-existent UUID + nonexistent_uuid = uuid4() message = MessageRead( - id=uuid4(), # Generate a random UUID that won't exist in the database + id=nonexistent_uuid, # Generate a random UUID that won't exist in the database text="Test message", sender="User", sender_name="User", session_id="session_id", flow_id=uuid4(), ) - - updated = await aupdate_messages(message) - assert len(updated) == 0 + with pytest.raises(ValueError, match=f"Message with id {nonexistent_uuid} not found"): + await aupdate_messages(message) @pytest.mark.usefixtures("client") async def test_aupdate_mixed_messages(created_messages): # Create a mix of existing and non-existing messages + nonexistent_uuid = uuid4() nonexistent_message = MessageRead( - id=uuid4(), # Generate a random UUID that won't exist in the database + id=nonexistent_uuid, # Generate a random UUID that won't exist in the database text="Test message", sender="User", sender_name="User", @@ -225,7 +226,11 @@ async def test_aupdate_mixed_messages(created_messages): messages_to_update = created_messages[:1] + [nonexistent_message] created_messages[0].text = "Updated existing message" - updated = await aupdate_messages(messages_to_update) + with pytest.raises(ValueError, match=f"Message with id {nonexistent_uuid} not found"): + await aupdate_messages(messages_to_update) + + # Update just the existing message + updated = await aupdate_messages(created_messages[:1]) assert len(updated) == 1 assert updated[0].text == "Updated existing message"