PydanticUserError when setting up FlashrankRerank in LangChain

I’m running into a problem when I try to create a FlashrankRerank instance. The error message says:

PydanticUserError: FlashrankRerank is not fully defined; you should define Ranker, then call FlashrankRerank.model_rebuild()

Here’s what I’m trying to do:

reranker = FlashrankRerank()
compression_ret = ContextualCompressionRetriever(
    base_compressor=reranker, 
    base_retriever=my_retriever
)

When I attempted to fix it by calling reranker = FlashrankRerank.model_rebuild(), I got another error:

PydanticUndefinedAnnotation: name 'Ranker' is not defined

How can I properly initialize FlashrankRerank without these errors?

Had this exact issue last month when migrating some search functionality. FlashrankRerank needs the Ranker class imported first or it breaks.

Install and import flashrank before initializing:

from flashrank import Ranker
from langchain.retrievers.document_compressors import FlashrankRerank

reranker = FlashrankRerank()
compression_ret = ContextualCompressionRetriever(
    base_compressor=reranker,
    base_retriever=my_retriever
)

If you don’t have flashrank:

pip install flashrank

Import Ranker before creating the FlashrankRerank instance. Without it, Pydantic can’t resolve the type annotation and throws that undefined error.

Once I added the flashrank import, everything worked. No need to call model_rebuild() manually.

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.