Skip to content

Commit 6620a3c

Browse files
committed
fix(embedding): raise Gemini max_embedding_tokens cap from 2048 to 8192
Gemini's embedding models actually support up to 8192 input tokens, not 2048. The artificial 2048 cap was truncating/chunking text more aggressively than necessary for Gemini-backed embedding configs. Added a unit test asserting the client honors the 8192 cap (and still respects a lower max_input_tokens when configured below the cap). Signed-off-by: Corey Hemminger <hemminger@hotmail.com>
1 parent 4f9a413 commit 6620a3c

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

src/embedding_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ def __init__(
191191
api_key=config.api_key,
192192
http_options=http_options,
193193
)
194-
# Gemini has a 2048 token limit
195-
self.max_embedding_tokens: int = min(max_input_tokens, 2048)
194+
# Gemini's embedding models support up to 8192 input tokens.
195+
self.max_embedding_tokens: int = min(max_input_tokens, 8192)
196196
# Gemini batch size is not documented, using conservative estimate
197197
self.max_batch_size: int = 100
198198
else: # openai

tests/llm/test_embedding_client.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,46 @@ def __init__(self, *, api_key: str | None, base_url: str | None) -> None:
9292
await client.embed("hello world")
9393

9494

95+
def test_gemini_embedding_client_honors_8192_token_cap(
96+
monkeypatch: pytest.MonkeyPatch,
97+
) -> None:
98+
"""Gemini's max_embedding_tokens should be capped at 8192, not 2048."""
99+
100+
class FakeGeminiClient:
101+
def __init__(self, *, api_key: str, http_options: Any) -> None:
102+
self.api_key: str = api_key
103+
104+
monkeypatch.setattr("src.embedding_client.genai.Client", FakeGeminiClient)
105+
106+
# When max_input_tokens is above the provider cap, it should be clamped to 8192.
107+
client_above_cap = _EmbeddingClient(
108+
EmbeddingModelConfig(
109+
transport="gemini",
110+
model="gemini-embedding-001",
111+
api_key="test-key",
112+
),
113+
vector_dimensions=8,
114+
max_input_tokens=20_000,
115+
max_tokens_per_request=300_000,
116+
send_dimensions=True,
117+
)
118+
assert client_above_cap.max_embedding_tokens == 8192
119+
120+
# When max_input_tokens is below the provider cap, it should pass through unchanged.
121+
client_below_cap = _EmbeddingClient(
122+
EmbeddingModelConfig(
123+
transport="gemini",
124+
model="gemini-embedding-001",
125+
api_key="test-key",
126+
),
127+
vector_dimensions=8,
128+
max_input_tokens=1024,
129+
max_tokens_per_request=300_000,
130+
send_dimensions=True,
131+
)
132+
assert client_below_cap.max_embedding_tokens == 1024
133+
134+
95135
@pytest.mark.asyncio
96136
async def test_gemini_embedding_client_uses_output_dimensionality(
97137
monkeypatch: pytest.MonkeyPatch,

0 commit comments

Comments
 (0)