Skip to content

fix(embeddings): check for numpy once per response - #3754

Open
lazerg wants to merge 2 commits into
openai:mainfrom
lazerg:fix/issue-3753-embeddings-has-numpy-loop
Open

fix(embeddings): check for numpy once per response#3754
lazerg wants to merge 2 commits into
openai:mainfrom
lazerg:fix/issue-3753-embeddings-has-numpy-loop

Conversation

@lazerg

@lazerg lazerg commented Aug 28, 2026

Copy link
Copy Markdown
  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

parse_embedding_response calls has_numpy() once per embedding, and that function does a fresh import numpy every time. Python does not cache failed imports, so when numpy isn't installed each call walks the path finders again and builds an ImportError. A 1500-embedding response pays that 1500 times. Hoisting the check above the loop keeps the behaviour identical and does the import work once.

Decoding 1500 vectors without numpy goes from 96ms to 63ms on my machine, where sys.path has six entries. The reporter is on Windows and sees the same call take seconds.

src/openai/lib/ is hand-written and the generator does not touch it, per CONTRIBUTING.md.

Additional context & links

Fixes #3753

@lazerg
lazerg requested a review from a team as a code owner August 28, 2026 11:58

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b175a3cce4

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/openai/lib/_parsing/_embeddings.py Outdated
if not obj.data:
raise ValueError("No embedding data received")

use_numpy = has_numpy()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Defer the NumPy probe until decoding is needed

When an OpenAI-compatible endpoint ignores the automatically requested base64 format and returns only ordinary float vectors, this unconditional call now imports the optional NumPy package even though every item is skipped below. Previously has_numpy() was never called for such a response, so an installed NumPy package that fails during import with a non-ImportError can now prevent an otherwise valid float response from being returned, and even a healthy first import adds unnecessary latency. Cache the result lazily when the first string vector is encountered instead.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, that was a real behavior change. If an endpoint ignores the base64 request and sends plain float vectors, the old code never reached has_numpy() at all, and my hoist made it run on every response.

c519bd1 moves the check to the first string vector and caches it in the local, so a base64 response still checks once and a float-only response never checks. Added a test that fails a decoder call for a response with no string vectors.

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.

bulk embeddings very slow

1 participant