Scholars have different understandings from this hadiths.
Some points about this:
1- The Imams haven been given the knowledge of the future and unseen events. So they do know when they would die.
2- The question then comes: if they know when they’ll die, then how do they eat something poisonous (like Imam Redha (a) eating the poisonous grapes)? One answer is that in that very moment, either Allah blocks them from knowing this will kill them, or they “choose not to know” in that moment so no one considers it suicide.
But most scholars believe the Imams knew that. There many hadiths that state they knew. And it was a sacrifice they had to make, so it wasn’t suicide.