Openai python SDK Lambda Issues

I have been trying to get the openai python package in to my Lambda function. I keep running in to the error

Runtime.ImportModuleError: Unable to import module 'serverless_aws_lambda_sdk.internal_extension.wrapper': No module named 'pydantic_core._pydantic_core'

I have tried using Docker to make sure I am pip-ing the correct architecture both x86 and arm64 (while also changing my lambda arch). I am using the Serverless framework, along with a layer. From what I have found elsewhere it seems that using a Docker image should fix it, so maybe I am running into issues and not setting up my Docker correctly. I have tried using the pre-built layers found on GitHub (I can’t post a link), and this hasn’t worked either.

Has anyone found a good solution to trying to get openai package into Lambda?

1 Like

After a while I figureout how to solve this. At least for my specific project. What I did was:
Create a python folder and install the following:

  • pip install -t ./python fastapi==0.99.0 openai
  • Create a python layer using python 3.11 version, upload this python folder and select x86 and arm64 compatibility.
  • I also needed requests in my project, so I also did
    pip install --target ./packages requests==2.29.0
  • I upload my lambda function with the requests 2.29.0 libraries installed in my packages folder in the root level

Use the python layer you created in your lambda and it should work.

2 Likes

I was able to use pip to install the openai packages on my x86 Mac following the guidance in this AWS article: Working with .zip file archives for Python Lambda functions - AWS Lambda

I then zipped it and oploaded it to an AWS layer. The pip install parameters are critical to get the correct wheel files that are compatible with Lambda.

I have avoided installing the OpenAI Python SDK as a Lambda Layer, only because everything I do API-wise can be called by installing the requests layer. The cosine similarity stuff can be done on my own too without the SDK. I think the only thing you might need the SDK for is streaming.

1 Like

I have been trying to fix this issue for days. Your comment made my week. It worked! Thank you!

I’ve tried making the layers and running but still no dice. Any step by step or tip would really help.

Thank you very much! Your solution fixed my issue. Could you let me know how you figured out that using “fastapi” would solve this problem?