Jina Reranker Integration#
The Jina reranker plugin (models/jina/models/rerank/rerank.py) implements the RerankModel interface from the Dify plugin SDK to call Jina's /v1/rerank endpoint. It supports both text-only reranking (_invoke) and multimodal reranking (_invoke_multimodal).
Request Formatting#
The request payload includes :
modelβ the model name passed inqueryβ the search query stringdocumentsβ each document is passed throughtransform_jina_input_text, which wraps the text as{"text": ...}for models that require it (e.g.jina-clip-v1,jina-clip-v2,jina-embeddings-v4,jina-reranker-m0) or passes the raw string otherwisetop_nβ optional limit on returned resultsreturn_documents:Falseβ explicitly set; see below
For multimodal invocations, transform_jina_input_multi_modal is used instead, transforming each document into {"text": ...} or {"image": ...} depending on content type .
The base URL defaults to https://api.jina.ai/v1 but can be overridden via a base_url credential .
The return_documents: false Decision#
This is the critical design choice in this plugin. Jina changed the default value of return_documents to false in their API . Before PR #1800, the plugin relied on result["document"]["text"] from the API response to populate RerankDocument.text. When Jina stopped returning document bodies by default, the plugin threw string indices must be integers, not 'str' .
The fix (merged 2025-10-08, plugin version bumped 0.0.8 β 0.0.9) makes two complementary changes:
- Explicitly sets
"return_documents": Falsein the request payload β the plugin no longer asks Jina to return document text at all. - Uses the local
docsinput as the source of truth for document text β after getting back a ranked list of indices, the plugin readsdocs[original_index]directly :
original_index = result["index"]
RerankDocument(text=docs[original_index], score=result["relevance_score"], ...)
This approach is more robust and cheaper (less data transferred over the wire), and avoids any future drift between API-returned text and the locally held document content.
Response Handling#
The API response is expected under results["results"], where each entry has :
indexβ the original position of the document in the inputdocslistrelevance_scoreβ the ranking score from Jina
The plugin applies score_threshold filtering client-side: only documents whose relevance_score >= score_threshold are included in the final RerankResult . If docs is empty, it short-circuits and returns an empty result immediately .
HTTP errors are mapped to Dify's unified error types: InvokeConnectionError, InvokeServerUnavailableError, InvokeAuthorizationError, and InvokeBadRequestError .
Credential Validation#
validate_credentials exercises the full _invoke path with a hardcoded geography question and two sample documents at score_threshold=0.8 . Any exception is re-raised as CredentialsValidateFailedError.
Key Files#
| File | Purpose |
|---|---|
models/jina/models/rerank/rerank.py | Core rerank implementation |
models/jina/models/shared/input.py | transform_jina_input_text / transform_jina_input_multi_modal helpers |
| Issue #1799 | Bug report: return_documents default change broke plugin |
| PR #1800 | Fix: use local document text, set return_documents: false explicitly |