pythonflaskamazon-ec2amazon-elastic-beanstalkopenai-api

Nginx worker fails to boot flask app on Elasticbeanstalk after installing OpenAI


Environment: Flask app running on python3.8 with AWS ElasticBeanstalk on an AL2 EC2 instance. After installing the OpenAI python package, there is an error in the EB instance on boot with the following error in the logs:

 from openai import OpenAI
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/openai/__init__.py", line 8, in <module>
web: from . import types
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/openai/types/__init__.py", line 5, in <module>
web: from .batch import Batch as Batch
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/openai/types/batch.py", line 7, in <module>
web: from .._models import BaseModel
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/openai/_models.py", line 25, in <module>
web: from ._types import (
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/openai/_types.py", line 21, in <module>
web: import httpx
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/httpx/__init__.py", line 2, in <module>
web: from ._api import delete, get, head, options, patch, post, put, request, stream
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/httpx/_api.py", line 6, in <module>
web: from ._client import Client
web: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
web: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
web: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/newrelic/api/import_hook.py", line 174, in exec_module
web: self.loader.exec_module(module)
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/httpx/_client.py", line 32, in <module>
web: from ._transports.default import AsyncHTTPTransport, HTTPTransport
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/httpx/_transports/default.py", line 32, in <module>
web: import httpcore
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/httpcore/__init__.py", line 1, in <module>
web: from ._api import request, stream
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/httpcore/_api.py", line 5, in <module>
web: from ._sync.connection_pool import ConnectionPool
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/httpcore/_sync/__init__.py", line 1, in <module>
web: from .connection import HTTPConnection
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/httpcore/_sync/connection.py", line 12, in <module>
web: from .._synchronization import Lock
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/httpcore/_synchronization.py", line 11, in <module>
web: import trio
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/trio/__init__.py", line 26, in <module>
web: from . import abc, from_thread, lowlevel, socket, to_thread
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/trio/socket.py", line 16, in <module>
web: from . import _socket
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/trio/_socket.py", line 526, in <module>
web: class SocketType:
web: File "/var/app/venv/staging-LQM1lest/lib64/python3.8/site-packages/trio/_socket.py", line 719, in SocketType
web: @_wraps(_stdlib_socket.socket.sendmsg, assigned=(), updated=())
web: AttributeError: type object 'GreenSocket' has no attribute 'sendmsg'
web: [2024-07-29 21:44:08 +0000] [25719] [INFO] Worker exiting (pid: 25719)

I've tried changing versions of OpenAI and the relevant dependencies and as soon as the OpenAI package is removed the worker successfully runs.


Solution

  • Here's what fixed my issue

    Wherever the environment is being hosted navigate to it, enter the venv, and pip uninstall trio. Trio gets installed as a dependency of httpx which is a dependency of openai and messes with stdlib packages while not actually being used.