Skip to content

fix(py/openai): split large embed batches into 2048-input requests - #141

Merged
huangjeff5 merged 2 commits into
genkit-ai:mainfrom
pcbeingused33:fix/openai-embedder-batching
Dec 1, 2026
Merged

huangjeff5 merged 2 commits into
genkit-ai:mainfrom
pcbeingused33:fix/openai-embedder-batching

Conversation

@pcbeingused33

Copy link
Copy Markdown
Contributor

Moved from genkit-ai/genkit#7079, as the maintainers asked after the Python SDK moved to this repo. The change is the same, rebased onto the current main (it now also passes ctx.context to _embed_client).

The OpenAI embedder in genkit-openai passed every document to a single embeddings.create call. 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 dimensions and encodingFormat options go with every chunk. The four-way if/elif over those options is replaced by a kwargs dict so the loop does not repeat it.

Test: test_embedder_splits_large_batches_into_chunks embeds 5000 documents and expects calls of 2048, 2048 and 904 inputs, the option on each call, and the embeddings back in order. It fails on main and passes with the change; the other 48 tests in openai_plugin_test.py still pass.

The JS compat-oai embedder has the same shape and is not touched here.


Disclosure: this change was prepared with AI assistance (Claude Code).

# Conflicts:
#	packages/genkit-openai/src/genkit_openai/_openai_plugin.py
@pcbeingused33

Copy link
Copy Markdown
Contributor Author

Merged the latest main into this branch to resolve the conflict in _openai_plugin.py (kept the embedder chunking along with the Mistral/extra_body kwargs handling). The openai plugin tests pass.

This comment was drafted with Claude Code.

@huangjeff5 huangjeff5 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified unit tests in genkit-openai (49 passed). Cleanly chunks batches larger than OpenAI's 2048-input limit while preserving order and passing dimensions/encoding_format on each chunk. Looks good!

@huangjeff5
huangjeff5 enabled auto-merge (squash) November 30, 2026 21:22
@huangjeff5
huangjeff5 merged commit 3d68682 into genkit-ai:main Dec 1, 2026
20 checks passed
@huangjeff5 huangjeff5 mentioned this pull request Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants