Yeah, ran into this exact same issue when I started with flashrank and langchain. It’s a pydantic validation problem - it tries to validate before flashrank loads. Just import flashrank first, before everything else. That’s it - no rebuilds or other workarounds needed.
I encountered the same issue when working with a RAG pipeline. The FlashrankRerank class has a forward reference to the Ranker class, which must be imported first. To resolve this, ensure the import order is correct:
from flashrank import Ranker # This must be first
from langchain.retrievers.document_compressors import FlashrankRerank
# This should work without errors now
reranker = FlashrankRerank()
This occurs because Pydantic requires the Ranker class to exist for validating the FlashrankRerank model. By importing flashrank.Ranker first, it registers correctly, allowing Pydantic’s validation to proceed without errors. There’s no need to call model_rebuild() manually when the imports are in the proper order.
This dependency issue caught me off guard too. LangChain’s FlashrankRerank class references the Ranker type, but Python can’t find it until flashrank loads into memory.
Pydantic tries validating the model before the Ranker class exists. The flashrank import doesn’t just bring in the class—it registers the type with Python’s system so Pydantic can validate properly.
Even with the right import, watch your import order. Put flashrank imports before any LangChain imports that use FlashrankRerank. I’ve seen cases where the type annotation still breaks if you don’t do this.