Skip to content

Python: return empty embeddings for empty input batches - #14855

Open
Twiink wants to merge 1 commit into
microsoft:mainfrom
Twiink:fix/empty-embedding-input-20261122
Open

Twiink wants to merge 1 commit into
microsoft:mainfrom
Twiink:fix/empty-embedding-input-20261122

Conversation

@Twiink

@Twiink Twiink commented Nov 22, 2026

Copy link
Copy Markdown

Passing an empty document batch to OpenAITextEmbedding.generate_raw_embeddings([]) or generate_embeddings([]) raises ValueError: range() arg 3 must not be zero with the default batch size. Supplying a positive batch_size already returns an empty result, so behavior depends on whether batching is configured.

Return an empty list before calculating the batch size. The ndarray API consequently returns an empty array, and no provider request is made. Azure embeddings inherit the same base implementation.

Validation:

  • Original source: both default-size regressions fail; explicit-size controls pass.
  • All OpenAI connector unit tests pass after the fix, including Azure embedding tests.
  • Ruff check, Ruff format and git diff --check pass.

@Twiink
Twiink requested a review from a team as a code owner November 22, 2026 05:56
Copilot AI balanced review requested due to automatic review settings November 22, 2026 05:56
@Twiink
Twiink temporarily deployed to github-app-auth November 22, 2026 05:56 — with GitHub Actions Inactive

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

There are no active review issues for the change that standardizes empty-input handling in the shared embedding implementation.

No open issues 🎉

What changed in this PR

This PR makes empty-batch handling consistent in the shared OpenAI embedding implementation, also used by Azure embeddings.

Changes:

  • Return an empty list before calculating the batch size or making a provider request.
  • Document empty-input behavior and add regression tests for raw and ndarray results with default and explicit batch sizes.
File Description
python/​tests/​unit/​connectors/​ai/​open_ai/​services/​test_openai_text_embedding.py Tests empty results across both APIs and batch-size options, with no provider call.
python/​semantic_kernel/​connectors/​ai/​open_ai/​services/​open_ai_text_embedding_base.py Adds and documents the early return for empty input.

Review effort: Balanced

This branch was previously deployed

1 inactive deployment
github-app-auth — 57d08336 Deployed Nov 22, 2026 by Twiink via add_label #29791
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