Files
lancedb/python/python/tests/test_cohere_embeddings.py
T
Xuanwo 726a6f77a5 feat(embeddings): support Cohere v2 endpoints for Foundry text retrieval (#5193)
Continues #2519 (originally contributed by @1jamesthompson1) to address
#2518 for text retrieval with Cohere Embed models deployed on Azure AI
Foundry.

## Summary

Adds an opt-in native Cohere Embed API v2 (`ClientV2`) path to the
existing `cohere` embedding registry entry (`CohereEmbeddingFunction`)
for text-only retrieval against custom endpoints such as Azure AI
Foundry (`https://<resource>.services.ai.azure.com/providers/cohere`,
where the Cohere SDK appends `/v2/embed`).

### Behavior and Defaults

- **Opt-in v2 route (`base_url`)**: When `base_url` is set, requests use
`cohere.ClientV2` (`POST <base_url>/v2/embed`) with
`embedding_types=["float"]`, split inputs into batches of up to 96
texts, disable SDK-level retries, and validate returned vector counts
and dimensions. Application retries use `retry_with_exponential_backoff`
in `embeddings/utils.py`, which adds an optional `do_not_retry`
predicate used only by Cohere v2 so `3xx` redirects fail immediately
without backoff while transient failures such as `429` retry only the
failed batch. Non-string inputs are rejected; this change covers text
retrieval only.
- **Fixed retrieval roles**: In `base_url` mode, document embeddings use
`search_document` and query embeddings use `search_query`. Setting other
`source_input_type` or `query_input_type` values is rejected.
- **Explicit endpoint parameters**: Setting `base_url` requires explicit
`name` (the deployed model name), `output_dimension` (`> 0`, used for
both the Arrow vector column width and the v2 request
`output_dimension`), and `api_key`. Setting `output_dimension` or
`api_key` without `base_url` is rejected.
- **Unchanged hosted path**: When `base_url` is omitted, existing
behavior is preserved — `cohere.Client` (`/v1/embed`), `COHERE_API_KEY`,
existing default model/role settings, legacy table metadata restoration,
and the existing `cohere>=4.0` package floor.
- **Runtime SDK requirement**: Opting into `base_url` checks
`cohere>=5.15.0` at runtime and raises an `ImportError` on older SDK
versions without raising the minimum requirement for hosted users.

### Credentials and Endpoint Trust

- `api_key` is declared in `sensitive_keys()` and must be a registry
`$var:` reference without an inline fallback default (`$var:<name>`
only). Table metadata persists only the variable reference.
- `base_url` must be an HTTP(S) URL without embedded credentials, query
strings, or fragments, and is exposed through `endpoints()` so reopening
a table in a new process requires explicit approval via
`registry.allow_endpoint(...)`.
- `ClientV2` is cached per embedding function instance
(`@cached_property`) with `httpx.Client(follow_redirects=False,
timeout=300)` so redirects cannot forward requests or credentials off
the approved endpoint. Because Cohere v2 classifies `3xx` responses as
non-retryable via `do_not_retry`, a redirected `table.add` or
`table.search` call makes one `POST` with zero application backoff and
no request to the `Location` target (`table.add` surfaces the `307`
inside the existing Arrow/Rust `RuntimeError` wrapper; `table.search`
surfaces the Cohere `ApiError`).

## Verification

- **Loopback HTTP SDK tests (`test_cohere_embeddings.py`)**: Passed
against local loopback HTTP fixtures with real Cohere SDK `5.15.0`
(opt-in minimum), `7.0.5`, and `7.3.2`. Verified exact
`/providers/cohere/v2/embed` path, bearer auth header, JSON payload,
256-dim vector schema, `table.add`/reopen, fresh-process
`UntrustedEmbeddingConfigError` followed by `allow_endpoint(...)`,
deterministic role-based ranking, 96-text batching with
failed-batch-only `429` retry (single backoff), `307` redirect handling
with retries enabled on both `table.add` (wrapped in Arrow/Rust
`RuntimeError`) and `table.search` (Cohere `ApiError`) with one `POST`,
zero application backoff, and no `Location` request, plus invalid
input/dimension rejection.
- **Adjacent embedding, retry, and trust suites (`cohere==7.3.2`)**:
Passed across embedding, registry, trust (including the SDK-independent
Cohere metadata restoration and endpoint authorization test in
`test_embeddings_trust.py`), field metadata, NVIDIA embedding, and retry
suites (OpenAI-key test skipped without a key).
- **No-`cohere` environment**: Cohere config/legacy unit tests and
SDK-independent `test_embeddings_trust.py` metadata restoration and
endpoint authorization tests passed without `cohere` installed (real-SDK
loopback tests skipped).
- **Legacy SDK (`cohere==4.57`)**: Loopback wire check confirmed hosted
document/query requests continue targeting `/v1/embed` and opt-in
`base_url` usage raises `Cohere base_url requires cohere>=5.15.0`.
- **Docs & lint**: Offline Foundry schema test
(`test_embedding_cohere_foundry`), byte-identical regenerated snippet
(`docs/web/snippets/integrations.mdx`), and `python/` Ruff format/lint
checks passed.

### Scope of Verification

All HTTP request, authentication, redirect, trust, and ranking checks
were exercised against local loopback HTTP fixtures modeled on [Cohere's
Azure Foundry
workshop](https://github.com/cohere-ai/cohere-azure-workshops/tree/ca1c4cb162e2/lab-1-embed)
and the [Cohere Embed v2 API
reference](https://docs.cohere.com/v2/reference/embed). Acceptance
against a live Azure AI Foundry service (real Foundry credentials and
specific `output_dimension` overrides) has not been verified.
2026-11-24 20:20:18 +08:00

320 lines
11 KiB
Python

# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The LanceDB Authors
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
import os
import subprocess
import sys
from types import SimpleNamespace
import lancedb
import pytest
from lancedb.embeddings import CohereEmbeddingFunction, get_registry
from lancedb.pydantic import LanceModel
from utils import start_server_thread
@pytest.fixture
def foundry_cohere(monkeypatch):
pytest.importorskip("cohere", minversion="5.15.0")
monkeypatch.setenv("NO_PROXY", "127.0.0.1")
monkeypatch.setenv("no_proxy", "127.0.0.1")
monkeypatch.setenv("COHERE_API_KEY", "test-hosted-key")
monkeypatch.setenv("TEST_FOUNDRY_COHERE_KEY", "test-foundry-key")
registry = get_registry()
monkeypatch.setattr(registry, "_allowed_endpoints", set())
monkeypatch.setattr(registry, "_variables", {})
registry.set_var("foundry-key", os.environ["TEST_FOUNDRY_COHERE_KEY"])
requests = []
statuses = {}
class EmbedHandler(BaseHTTPRequestHandler):
def do_POST(self):
body = json.loads(self.rfile.read(int(self.headers["Content-Length"])))
requests.append((self.path, self.headers.get("Authorization"), body))
status = statuses.pop(body["texts"][0], None)
if status is not None:
self.send_response(status)
if status == 307:
self.send_header("Location", "/redirect-target/v2/embed")
self.end_headers()
return
vectors = []
for text in body["texts"]:
matches = (body["input_type"], text) in {
("search_document", "matching document"),
("search_query", "matching query"),
}
if text == "wrong dimension":
vectors.append([0.0])
else:
vectors.append(
([1.0, 0.0] if matches else [0.0, 1.0]) + [0.0] * 254
)
response = json.dumps(
{
"id": "fixture",
"embeddings": {"float": vectors},
"texts": body["texts"],
}
).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(response)
def log_message(self, *args):
pass
with HTTPServer(("127.0.0.1", 0), EmbedHandler) as server:
thread = start_server_thread(server)
try:
options = {
"name": "my-embed-v4-deployment",
"base_url": f"http://127.0.0.1:{server.server_port}/providers/cohere",
"output_dimension": 256,
"api_key": "$var:foundry-key",
}
yield options, requests, statuses
finally:
server.shutdown()
thread.join()
def test_foundry_cohere_retrieval_and_endpoint_trust(
foundry_cohere, monkeypatch, tmp_path
):
import httpx
options, requests, _ = foundry_cohere
func = get_registry().get("cohere").create(**options)
request_timeouts = []
handle_request = httpx.HTTPTransport.handle_request
def record_request_timeout(transport, request):
request_timeouts.append(request.extensions.get("timeout"))
return handle_request(transport, request)
monkeypatch.setattr(httpx.HTTPTransport, "handle_request", record_request_timeout)
class Document(LanceModel):
text: str = func.SourceField()
vector: list[float] = func.VectorField()
db = lancedb.connect(tmp_path)
table = db.create_table("docs", schema=Document)
table.add([{"text": "distractor"}, {"text": "matching document"}])
reopened = lancedb.connect(tmp_path).open_table("docs")
assert reopened.schema.field("vector").type.list_size == 256
metadata = reopened.schema.metadata[b"embedding_functions"]
assert b"test-foundry-key" not in metadata
assert json.loads(metadata) == [
{
"name": "cohere",
"model": options,
"source_column": "text",
"vector_column": "vector",
}
]
assert reopened.search("matching query").limit(1).to_list()[0]["text"] == (
"matching document"
)
assert (
request_timeouts
== [{"connect": 300, "read": 300, "write": 300, "pool": 300}] * 2
)
# A fresh interpreter has the key but must separately trust the stored URL.
child = subprocess.run(
[
sys.executable,
"-c",
"""
import os
import sys
import lancedb
from lancedb.embeddings import UntrustedEmbeddingConfigError, get_registry
registry = get_registry()
registry.set_var("foundry-key", os.environ["TEST_FOUNDRY_COHERE_KEY"])
db = lancedb.connect(sys.argv[1])
try:
db.open_table("docs").search("matching query").limit(1).to_list()
except UntrustedEmbeddingConfigError as error:
assert sys.argv[2] in str(error)
else:
raise AssertionError("The stored endpoint needs explicit approval")
registry.allow_endpoint(sys.argv[2])
assert db.open_table("docs").search("matching query").limit(1).to_list()[0][
"text"
] == "matching document"
""",
str(tmp_path),
options["base_url"],
],
capture_output=True,
text=True,
timeout=30,
)
assert child.returncode == 0, child.stderr
assert requests == [
(
"/providers/cohere/v2/embed",
"Bearer test-foundry-key",
{
"model": "my-embed-v4-deployment",
"texts": texts,
"input_type": input_type,
"output_dimension": 256,
"embedding_types": ["float"],
},
)
for input_type, texts in [
("search_document", ["distractor", "matching document"]),
("search_query", ["matching query"]),
("search_query", ["matching query"]),
]
]
def test_foundry_cohere_retries_only_failed_batch(foundry_cohere, monkeypatch):
options, requests, statuses = foundry_cohere
backoffs = []
monkeypatch.setattr(
"lancedb.embeddings.utils.time", SimpleNamespace(sleep=backoffs.append)
)
func = CohereEmbeddingFunction.create(**options, max_retries=1)
texts = [f"document {index}" for index in range(96)] + ["matching document"]
statuses["matching document"] = 429
vectors = func.compute_source_embeddings_with_retry(texts)
assert [body["texts"] for _, _, body in requests] == [
texts[:96],
texts[96:],
texts[96:],
]
assert len(backoffs) == 1
assert [vector[:2] for vector in vectors] == [[0.0, 1.0]] * 96 + [[1.0, 0.0]]
def test_foundry_cohere_rejects_invalid_inputs_and_vectors(foundry_cohere, monkeypatch):
options, requests, _ = foundry_cohere
monkeypatch.setattr("lancedb.embeddings.utils.time.sleep", pytest.fail)
func = CohereEmbeddingFunction.create(**options)
with pytest.raises(ValueError, match="unexpected dimension"):
func.compute_query_embeddings_with_retry("wrong dimension")
with pytest.raises(ValueError, match="requires string inputs"):
func.compute_source_embeddings_with_retry([b"image"])
assert len(requests) == 1
@pytest.mark.parametrize(
"input_type", ["search_document", "search_query"], ids=["add", "search"]
)
def test_foundry_cohere_does_not_retry_or_follow_endpoint_redirect(
foundry_cohere, monkeypatch, tmp_path, input_type
):
from cohere.core.api_error import ApiError
options, requests, statuses = foundry_cohere
func = CohereEmbeddingFunction.create(**options, max_retries=2)
class Document(LanceModel):
text: str = func.SourceField()
vector: list[float] = func.VectorField()
table = lancedb.connect(tmp_path).create_table("docs", schema=Document)
table.add([{"text": "seed", "vector": [0.0] * 256}])
backoffs = []
monkeypatch.setattr(
"lancedb.embeddings.utils.time", SimpleNamespace(sleep=backoffs.append)
)
statuses["redirected text"] = 307
if input_type == "search_document":
with pytest.raises(RuntimeError, match=r"ApiError:.*status_code: 307"):
table.add([{"text": "redirected text"}])
else:
with pytest.raises(ApiError) as error:
table.search("redirected text").limit(1).to_list()
assert error.value.status_code == 307
assert backoffs == []
assert [
(path, body["input_type"], body["texts"]) for path, _, body in requests
] == [("/providers/cohere/v2/embed", input_type, ["redirected text"])]
@pytest.mark.parametrize(
"overrides, message",
[
({"name": ""}, "deployed Cohere model"),
({"output_dimension": None}, "requires output_dimension and api_key"),
({"output_dimension": 0}, "greater than 0"),
({"api_key": None}, "requires output_dimension and api_key"),
({"api_key": "test-plaintext-key"}, "cannot be set to a hardcoded value"),
({"api_key": "$var:foundry-key:test-plaintext-key"}, "without a default"),
({"query_input_type": "classification"}, "search_query for queries"),
({"base_url": None}, "require base_url"),
({"base_url": "http://user:password@127.0.0.1"}, "without credentials"),
],
)
def test_foundry_cohere_validates_config(monkeypatch, overrides, message):
registry = get_registry()
monkeypatch.setattr(registry, "_variables", {"foundry-key": "test-foundry-key"})
options = {
"name": "embed-v-4-0",
"base_url": "http://127.0.0.1/providers/cohere",
"output_dimension": 256,
"api_key": "$var:foundry-key",
}
with pytest.raises(ValueError, match=message):
registry.get("cohere").create(**(options | overrides))
def test_cohere_legacy_metadata_and_v1_client(monkeypatch):
requests = []
class LegacyClient:
def __init__(self, key):
assert key == "test-hosted-key"
def embed(self, **body):
requests.append(body)
return SimpleNamespace(embeddings=[[1.0, 2.0]])
monkeypatch.setitem(sys.modules, "cohere", SimpleNamespace(Client=LegacyClient))
monkeypatch.setenv("COHERE_API_KEY", "test-hosted-key")
monkeypatch.setattr(CohereEmbeddingFunction, "client", None)
registry = get_registry()
func = registry.get("cohere").create()
assert func.ndims() == 1024
assert func.safe_model_dump() == {}
assert func.compute_source_embeddings(["default document"]) == [[1.0, 2.0]]
# This schema predates the custom endpoint parameters.
stored = {
"name": "cohere",
"model": {"name": "embed-multilingual-v2.0", "query_input_type": "clustering"},
"source_column": "text",
"vector_column": "vector",
}
metadata = {b"embedding_functions": json.dumps([stored]).encode()}
old_func = registry.parse_functions(metadata)["vector"].function
assert old_func.ndims() == 768
assert old_func.safe_model_dump() == stored["model"]
assert old_func.compute_query_embeddings("legacy query") == [[1.0, 2.0]]
assert requests == [
{
"texts": ["default document"],
"model": "embed-multilingual-v3.0",
"input_type": "search_document",
},
{
"texts": ["legacy query"],
"model": "embed-multilingual-v2.0",
"input_type": "clustering",
},
]