Fix/OpenAI embedding dimensions 4153 (#4481)

This commit is contained in:
Himanshu
2026-03-25 19:59:28 +05:30
committed by GitHub
parent f9e30304d7
commit f06e2d744d
2 changed files with 30 additions and 14 deletions
+19 -4
View File
@@ -24,7 +24,7 @@ def test_embed_default_model(mock_openai_client):
result = embedder.embed("Hello world")
mock_openai_client.embeddings.create.assert_called_once_with(
input=["Hello world"], model="text-embedding-3-small", dimensions=1536, encoding_format="float"
input=["Hello world"], model="text-embedding-3-small", encoding_format="float"
)
assert result == [0.1, 0.2, 0.3]
@@ -54,7 +54,7 @@ def test_embed_removes_newlines(mock_openai_client):
result = embedder.embed("Hello\nworld")
mock_openai_client.embeddings.create.assert_called_once_with(
input=["Hello world"], model="text-embedding-3-small", dimensions=1536, encoding_format="float"
input=["Hello world"], model="text-embedding-3-small", encoding_format="float"
)
assert result == [0.7, 0.8, 0.9]
@@ -69,7 +69,7 @@ def test_embed_without_api_key_env_var(mock_openai_client):
result = embedder.embed("Testing API key")
mock_openai_client.embeddings.create.assert_called_once_with(
input=["Testing API key"], model="text-embedding-3-small", dimensions=1536, encoding_format="float"
input=["Testing API key"], model="text-embedding-3-small", encoding_format="float"
)
assert result == [1.0, 1.1, 1.2]
@@ -85,7 +85,7 @@ def test_embed_uses_environment_api_key(mock_openai_client, monkeypatch):
result = embedder.embed("Environment key test")
mock_openai_client.embeddings.create.assert_called_once_with(
input=["Environment key test"], model="text-embedding-3-small", dimensions=1536, encoding_format="float"
input=["Environment key test"], model="text-embedding-3-small", encoding_format="float"
)
assert result == [1.3, 1.4, 1.5]
@@ -107,3 +107,18 @@ def test_embed_passes_encoding_format_float(mock_openai_client):
call_kwargs = mock_openai_client.embeddings.create.call_args
assert call_kwargs.kwargs.get("encoding_format") == "float" or call_kwargs[1].get("encoding_format") == "float"
def test_embed_passes_dimensions_only_when_explicit(mock_openai_client):
"""Matryoshka / truncated embeddings: dimensions sent only if user sets embedding_dims (#4153)."""
config = BaseEmbedderConfig(embedding_dims=256)
embedder = OpenAIEmbedding(config)
mock_response = Mock()
mock_response.data = [Mock(embedding=[0.1] * 256)]
mock_openai_client.embeddings.create.return_value = mock_response
embedder.embed("truncate me")
mock_openai_client.embeddings.create.assert_called_once_with(
input=["truncate me"], model="text-embedding-3-small", dimensions=256, encoding_format="float"
)