I understood my mistake. In my case, Langchain, langchain-core and langchain-community versions were incompatible.
pip install langchain
showed that incompatibility as warnings while installing langchain. Installing the compatible versions accordingly solved the problem.