Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
fix: request float embeddings explicitly and drop the dimensions param on provider rejection instead of failing the pipeline (supersedes #352)
Murtaza Nasirff63ec716d39
diff --git a/src/services/embeddings.py b/src/services/embeddings.pyindex 0422aef4..325e14e5 100644--- a/src/services/embeddings.py+++ b/src/services/embeddings.py@@ -137,6 +137,19 @@ def _is_transient_embedding_error(exc):return any(hint in msg for hint in _TRANSIENT_ERROR_HINTS)+# Set to True the first time the provider rejects the 'dimensions' parameter+# (models without Matryoshka support, e.g. BAAI/bge-m3, return a 400). Later+# calls then omit the parameter instead of failing the whole pipeline. Probing+# the provider beats a hardcoded list of dimension-capable model names, which+# would silently disable a setting that newer models do support.+_dimensions_rejected = False+++def _is_dimensions_rejection(exc):+ """True when the error looks like the model rejecting the 'dimensions' kwarg."""+ return 'dimension' in str(exc).lower()++def _api_embed(texts, user_id=None):"""Call the OpenAI-compatible embeddings endpoint and return numpy vectors.@@ -154,12 +167,17 @@ def _api_embed(texts, user_id=None):When ``user_id`` is provided and the response includes a ``usage``block, record the call against the daily token-usage aggregate."""+ global _dimensions_rejected+client = get_embedding_api_client()if client is None or not texts:return []- kwargs = {'input': texts, 'model': EMBEDDING_MODEL}- if EMBEDDING_DIMENSIONS is not None:+ # encoding_format='float' explicitly: OpenAI SDK v2 defaults to base64,+ # which some OpenAI-compatible providers cannot produce; float is the+ # format every compatible provider accepts.+ kwargs = {'input': texts, 'model': EMBEDDING_MODEL, 'encoding_format': 'float'}+ if EMBEDDING_DIMENSIONS is not None and not _dimensions_rejected:kwargs['dimensions'] = EMBEDDING_DIMENSIONSlast_exc = None@@ -194,6 +212,16 @@ def _api_embed(texts, user_id=None):except Exception as e:last_exc = e+ if 'dimensions' in kwargs and _is_dimensions_rejection(e):+ _dimensions_rejected = True+ kwargs.pop('dimensions')+ current_app.logger.warning(+ f"Embedding model '{EMBEDDING_MODEL}' rejected the 'dimensions' "+ f"parameter (EMBEDDING_DIMENSIONS={EMBEDDING_DIMENSIONS}): {e}. "+ "Retrying without it; the setting will be ignored for the rest "+ "of this process."+ )+ continuetransient = _is_transient_embedding_error(e)if not transient or attempt == _API_EMBED_MAX_ATTEMPTS:current_app.logger.error(diff --git a/tests/test_cov_embeddings.py b/tests/test_cov_embeddings.pyindex cf1f46f2..cb1ef439 100644--- a/tests/test_cov_embeddings.py+++ b/tests/test_cov_embeddings.py@@ -377,6 +377,73 @@ def test_api_embed_forwards_dimensions(api_emb):_restore_env(saved)+def test_api_embed_sends_float_encoding_format(api_emb):+ # OpenAI SDK v2 defaults to base64, which some OpenAI-compatible+ # providers cannot produce; float must be requested explicitly (PR #352).+ client = _client_returning([[0.1, 0.2]])+ with app.app_context():+ with patch.object(api_emb, "get_embedding_api_client", return_value=client):+ api_emb._api_embed(["x"])+ assert client.embeddings.create.call_args.kwargs["encoding_format"] == "float"+++def test_api_embed_drops_dimensions_on_provider_rejection(api_emb):+ # Models without Matryoshka support (e.g. BAAI/bge-m3) 400 on the+ # 'dimensions' parameter. The call must retry without it and remember+ # the rejection for later calls instead of failing the pipeline (PR #352).+ emb, saved = _reload_embeddings(+ EMBEDDING_BASE_URL="http://localhost:9999/v1",+ EMBEDDING_MODEL="bge-m3",+ EMBEDDING_DIMENSIONS="8",+ EMBEDDING_API_BACKOFF_SECONDS="0",+ )+ try:+ resp = MagicMock()+ resp.data = [MagicMock(embedding=[0.0] * 8)]+ resp.usage = None+ client = MagicMock()+ client.embeddings.create.side_effect = [+ Exception("400: extra_forbidden - dimensions is not supported"),+ resp,+ ]+ with app.app_context():+ with patch.object(emb, "get_embedding_api_client", return_value=client):+ out = emb._api_embed(["x"])+ assert len(out) == 1+ first, second = client.embeddings.create.call_args_list+ assert "dimensions" in first.kwargs+ assert "dimensions" not in second.kwargs++ # A later call must skip dimensions from the start.+ client2 = _client_returning([[0.0] * 8])+ with app.app_context():+ with patch.object(emb, "get_embedding_api_client", return_value=client2):+ emb._api_embed(["y"])+ assert "dimensions" not in client2.embeddings.create.call_args.kwargs+ finally:+ _restore_env(saved)+++def test_api_embed_dimensions_kept_when_supported(api_emb):+ # A provider that accepts dimensions keeps receiving it on every call.+ emb, saved = _reload_embeddings(+ EMBEDDING_BASE_URL="http://localhost:9999/v1",+ EMBEDDING_MODEL="text-embedding-3-large",+ EMBEDDING_DIMENSIONS="8",+ EMBEDDING_API_BACKOFF_SECONDS="0",+ )+ try:+ client = _client_returning([[0.0] * 8])+ with app.app_context():+ with patch.object(emb, "get_embedding_api_client", return_value=client):+ emb._api_embed(["x"])+ emb._api_embed(["y"])+ for call in client.embeddings.create.call_args_list:+ assert call.kwargs["dimensions"] == 8+ finally:+ _restore_env(saved)++def test_api_embed_retries_then_succeeds(api_emb):resp = MagicMock()resp.data = [MagicMock(embedding=[1.0, 2.0])]