Repository navigation
Add rerank support to Cohere provider - #72501
Conversation
potiuk
left a comment
There was a problem hiding this comment.
Thanks for this — the shape of the change is right. It mirrors CohereEmbeddingOperator closely (same cached_property hook, same common.compat.sdk imports, same example-DAG and docs layout), provider.yaml and the generated get_provider_info.py agree, and the test file lands where the provider-structure check expects it. Two things need fixing first.
1. The default rerank model is an Azure deployment name, and nothing can override it
In hooks/cohere.py:
model: str = "Cohere-rerank-v4.0-pro",That capitalised Cohere- prefix is the Azure AI Foundry catalog naming convention (Cohere-rerank-v3.5, Cohere-embed-v3-english), not a model ID on api.cohere.com, where they are lowercase (rerank-v3.5, rerank-multilingual-v3.0). The description confirms the end-to-end testing was against an Azure connection (conn_id="cohere_azure"), which is how this slipped through. Two corroborating signals: the PR's own unit test uses model="rerank-v3.5", the canonical form, and the sibling create_embeddings defaults to embed-multilingual-v3.0, a real Cohere ID. As it stands, a user on a plain cohere_default connection gets a bad model name on the primary path.
Compounding it: the operator has no model parameter at all. CohereRerankOperator.__init__ doesn't accept one and execute() doesn't forward one, so every task is pinned to that default with no escape hatch. The new doc page says:
The model is configured by the hook.
but CohereHook.__init__ has no model parameter either — it exists only as a per-call kwarg the operator never passes. So the documented configuration path doesn't exist in any form.
Suggested fix: default the hook to a canonical Cohere model ID, add model: str | None = None to the operator and forward it when set, and correct that sentence in rerank.rst.
(CohereEmbeddingOperator has the same hook-only-model limitation, so there is precedent — but the docs here actively promise otherwise, which makes it worth closing rather than repeating.)
2. top_n / max_tokens_per_doc as template fields will send strings to the API
template_fields: Sequence[str] = ("query", "documents", "top_n", "max_tokens_per_doc")and rerank.rst states "both limits are templated fields". Airflow's renderer passes non-str/list/dict values through untouched, so a literal top_n=2 is unaffected — but as soon as someone uses the advertised templating (top_n="{{ params.n }}") the rendered value stays a str. The Cohere client does no validation on these; V2Client.rerank puts them straight into the request body:
json={"model": model, "query": query, "documents": documents,
"top_n": top_n, "return_documents": return_documents,
"max_tokens_per_doc": max_tokens_per_doc},so "top_n": "3" reaches the API as a JSON string. Either drop the two ints from template_fields (and the claim from the docs), or coerce with int(...) at the top of execute() after rendering.
3. Minor: None is serialised as JSON null rather than omitted
The client uses omit=OMIT where OMIT is Ellipsis, so the hook's unconditional top_n=top_n, max_tokens_per_doc=max_tokens_per_doc sends "top_n": null / "max_tokens_per_doc": null instead of omitting the keys. The Azure run in the description had max_tokens_per_doc=None and succeeded, so the API tolerates it — low risk, but building the kwargs conditionally is more robust against other Cohere-compatible endpoints.
The version floor is fine, for what it's worth: max_tokens_per_doc is present on ClientV2.rerank in cohere==5.13.4, the provider's minimum pin, so no dependency bump is needed.
Nits
CohereHook.rerankhas no docstring, andCohereRerankOperatoris missing the.. seealso:: :ref:`howto/operator:CohereRerankOperator`block thatCohereEmbeddingOperatorcarries.- Neither test exercises the hook's default model — both pass
model=explicitly. Covering the default would likely have surfaced point 1 during authoring. test_rerank.pymocks with a bareMagicMock/patched attribute; the project's testing standards ask forspec/autospec.- The operator test covers
execute()but not thathookis constructed with the givenconn_id/timeout/request_options— that plumbing is currently untested. - The example DAG's
from airflow import DAGmatches the existingexample_cohere_embedding_operator.py, so that's pre-existing provider style, not something to change here.
Drafted-by: Claude Code (Opus 5); reviewed by @potiuk before posting
|
Thanks for the detailed review. I’ve addressed the feedback:
|
Why
Cohere provider only supports embedding and does not provide an operator for the Rerank API.
What
CohereHook.rerankto rank documents by relevance to a query.CohereRerankOperatorwithtop_nandmax_tokens_per_docsupport.Test
Use the testing Dag below with a Cohere connection (Azure).
log
Was generative AI tooling used to co-author this PR?
Generated-by: [GPT 5.6 sol] following the guidelines
{pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.