Skip to content

fix: assert_has_numpy before using np.from_buffer - #524

Closed
zzstoatzz wants to merge 1 commit into
openai:mainfrom
zzstoatzz:patch-1
Closed

zzstoatzz wants to merge 1 commit into
openai:mainfrom
zzstoatzz:patch-1

Conversation

@zzstoatzz

@zzstoatzz zzstoatzz commented Jul 10, 2023 •

Copy link
Copy Markdown

it seems Embeddings.acreate should call assert_has_numpy before attempting to use np.frombuffer, as is done on L42 of Embeddings.create here

I encountered the following on 3.11, openai==0.27.8 using Embeddings.acreate in an env where numpy was not installed:

File ~/src/open-source/marvin/src/marvin/utilities/embeddings.py:11, in create_openai_embeddings(texts)
      8 async def create_openai_embeddings(texts: List[str]) -> List[List[float]]:
      9     """Create OpenAI embeddings for a list of texts."""
---> 11     embeddings = await openai.Embedding.acreate(
     12         input=[text.replace("\n", " ") for text in texts],
     13         engine=marvin.settings.embedding_engine,
     14     )
     16     return [
     17         r["embedding"] for r in sorted(embeddings["data"], key=lambda x: x["index"])
     18     ]

File ~/micromamba/envs/marvin/lib/python3.11/site-packages/openai/api_resources/embedding.py:82, in Embedding.acreate(cls, *args, **kwargs)
     78         for data in response.data:
     79
     80             # If an engine isn't using this optimization, don't do anything
     81             if type(data["embedding"]) == str:
---> 82                 data["embedding"] = np.frombuffer(
     83                     base64.b64decode(data["embedding"]), dtype="float32"
     84                 ).tolist()
     86     return response
     87 except TryAgain as e:

AttributeError: 'NoneType' object has no attribute 'frombuffer'

which appears to be because the "helper" is setting the module to None like

try:
    import numpy
except ImportError:
    numpy = None

calling assert_has_numpy before attempting to use np.frombuffer in acreate raises a more helpful error

File ~/micromamba/envs/marvin/lib/python3.11/site-packages/openai/api_resources/embedding.py:82, in Embedding.acreate(cls, *args, **kwargs)
     78     for data in response.data:
     79
     80         # If an engine isn't using this optimization, don't do anything
     81         if type(data["embedding"]) == str:
---> 82             assert_has_numpy()
     83             data["embedding"] = np.frombuffer(
     84                 base64.b64decode(data["embedding"]), dtype="float32"
     85             ).tolist()
     87 return response

File ~/micromamba/envs/marvin/lib/python3.11/site-packages/openai/datalib/numpy_helper.py:15, in assert_has_numpy()
     13 def assert_has_numpy():
     14     if not HAS_NUMPY:
---> 15         raise MissingDependencyError(NUMPY_INSTRUCTIONS)

MissingDependencyError:

OpenAI error:

    missing `numpy`

This feature requires additional dependencies:

    $ pip install openai[datalib]

@rattrayalex

Copy link
Copy Markdown
Contributor

Thanks for this!

We've since rewritten the library entirely, so I believe this change is no longer relevant. I'm sorry we didn't get to it sooner.

safa0 pushed a commit to safa0/openai-agents-python that referenced this pull request Apr 27, 2025
Small refactor for rest of stack.

---
[//]: # (BEGIN SAPLING FOOTER)
* openai#524
* openai#523
* __->__ openai#522
safa0 pushed a commit to safa0/openai-agents-python that referenced this pull request Apr 27, 2025
Small refactor.

---
[//]: # (BEGIN SAPLING FOOTER)
* openai#524
* __->__ openai#523
safa0 pushed a commit to safa0/openai-agents-python that referenced this pull request Apr 27, 2025
litellm is a library that abstracts away details/differences for a lot
of model providers. Adding an extension, so that any provider can easily
be integrated.

---
[//]: # (BEGIN SAPLING FOOTER)
* openai#532
* __->__ openai#524
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