Repository navigation
fix(py/openai): split large embed batches into 2048-input requests - #7079
pcbeingused33 wants to merge 1 commit into
Conversation
The OpenAI embedder sent every document in one embeddings.create call, which the API rejects above 2048 inputs. Send the inputs in chunks of 2048 and concatenate the results in order.
There was a problem hiding this comment.
Code Review
This pull request introduces batching for OpenAI embedding requests, splitting inputs into chunks of at most 2048 to comply with the API limit, and adds a unit test to verify this behavior. The reviewer suggested optimizing performance by executing these chunked requests concurrently using asyncio.gather instead of sequentially.
| client = self._embed_client() | ||
| data: list[Any] = [] | ||
| try: | ||
| if dimensions is not None and encoding_format is not None: | ||
| response = await client.embeddings.create( | ||
| model=name, | ||
| input=texts, | ||
| dimensions=dimensions, | ||
| encoding_format=encoding_format, | ||
| ) | ||
| elif dimensions is not None: | ||
| response = await client.embeddings.create( | ||
| model=name, | ||
| input=texts, | ||
| dimensions=dimensions, | ||
| ) | ||
| elif encoding_format is not None: | ||
| response = await client.embeddings.create( | ||
| model=name, | ||
| input=texts, | ||
| encoding_format=encoding_format, | ||
| ) | ||
| else: | ||
| for start in range(0, len(texts), _MAX_EMBED_INPUTS): | ||
| response = await client.embeddings.create( | ||
| model=name, | ||
| input=texts, | ||
| input=texts[start : start + _MAX_EMBED_INPUTS], | ||
| **extra, | ||
| ) | ||
| data.extend(response.data) |
There was a problem hiding this comment.
Since the embedding requests for different chunks are completely independent, we can run them concurrently using asyncio.gather instead of sequentially in a loop. This can significantly reduce the total latency for large batches of documents.
import asyncio
client = self._embed_client()
data: list[Any] = []
try:
chunks = [texts[i : i + _MAX_EMBED_INPUTS] for i in range(0, len(texts), _MAX_EMBED_INPUTS)]
tasks = [
client.embeddings.create(
model=name,
input=chunk,
**extra,
)
for chunk in chunks
]
responses = await asyncio.gather(*tasks)
for response in responses:
data.extend(response.data)|
Hi @pcbeingused333 — heads up that Genkit Python SDK development has moved to https://github.com/genkit-ai/genkit-python! Could you re-open this PR over there (file paths map 1:1 after stripping the leading py/ prefix), and link/close this PR once moved? cc @huangjeff5 |
|
Moved to genkit-ai/genkit-python#141 as requested. Closing this one. |
The OpenAI embedder in
genkit-openaipassed every document to a singleembeddings.createcall. The OpenAI embeddings endpoint accepts at most 2048 inputs per request, so embedding a larger batch fails. The Google GenAI embedder already splits requests by a batch size.Fix: send the inputs in chunks of 2048 and concatenate the returned embeddings in order. The
dimensionsandencodingFormatoptions go with every chunk. The four-wayif/elifover those options is replaced by a kwargs dict so the loop does not repeat it.Test:
test_embedder_splits_large_batches_into_chunksembeds 5000 documents and expects calls of 2048, 2048 and 904 inputs, the option on each call, and the embeddings back in order. It fails before the change and passes after; the other 45 tests inopenai_plugin_test.pystill pass.The JS
compat-oaiembedder has the same shape and is not touched here.Disclosure: this change was prepared with AI assistance (Claude Code).