I've come to realize that if someone is talking bad about another person (in their absence) in front of you, this person will also talk bad about you in your absence.
If you recognize this, don't start talking bad as well. Rather change the topic and do not invest more time with that person.