There is a Japanese proverb that goes something like, "The bamboo that bends is stronger than the oak that resists". In music there is a real strength that comes from adapting to what is going on around you as opposed to trying to insist on your way, regardless of whether you are right or wrong.
A big part of the joy of playing jazz is dealing with the unknown. No matter how much you practice, listen, and play with people, there will always be situations where you will be genuinely surprised. In these types of situations there are a few helpful things that you can do related to this idea of musical flexibility.