Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: cannot import name 'UJSONResponse' #2193

Closed
creatorrr opened this issue May 26, 2021 · 7 comments
Closed

ImportError: cannot import name 'UJSONResponse' #2193

creatorrr opened this issue May 26, 2021 · 7 comments
Labels
bug Something isn't working

Comments

@creatorrr
Copy link

Version

cli version: 0.35.0

Description

Using starlette.responses.Response throws error on local debug run

Configuration

relevant cortex.yaml:

- name: chitchat
  kind: RealtimeAPI
  handler:
    type: python
    path: chitchat_service/__init__.py
    models:
      path: s3://cortex-examples/sklearn/mpg-estimator/linreg/

Stack traces

(error output from cortex logs <api name>)

Traceback (most recent call last):
  File "/src/cortex/serve/start/server.py", line 46, in <module>
    main()
  File "/src/cortex/serve/start/server.py", line 40, in main
    log_config=log_config,
  File "/opt/conda/envs/env/lib/python3.6/site-packages/uvicorn/main.py", line 362, in run
    server.run()
  File "/opt/conda/envs/env/lib/python3.6/site-packages/uvicorn/main.py", line 390, in run
    loop.run_until_complete(self.serve(sockets=sockets))
  File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
  File "/opt/conda/envs/env/lib/python3.6/site-packages/uvicorn/main.py", line 397, in serve
    config.load()
  File "/opt/conda/envs/env/lib/python3.6/site-packages/uvicorn/config.py", line 278, in load
    self.loaded_app = import_from_string(self.app)
  File "/opt/conda/envs/env/lib/python3.6/site-packages/uvicorn/importer.py", line 23, in import_from_string
    raise exc from None
  File "/opt/conda/envs/env/lib/python3.6/site-packages/uvicorn/importer.py", line 20, in import_from_string
    module = importlib.import_module(module_str)
  File "/opt/conda/envs/env/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "/opt/conda/envs/env/lib/python3.6/site-packages/cortex_internal/serve/wsgi.py", line 15, in <module>
    from cortex_internal.serve.serve import start
  File "/opt/conda/envs/env/lib/python3.6/site-packages/cortex_internal/serve/serve.py", line 30, in <module>
    from fastapi import FastAPI
  File "/opt/conda/envs/env/lib/python3.6/site-packages/fastapi/__init__.py", line 23, in <module>
    from .responses import Response
  File "/opt/conda/envs/env/lib/python3.6/site-packages/fastapi/responses.py", line 10, in <module>
    from starlette.responses import UJSONResponse  # noqa
ImportError: cannot import name 'UJSONResponse'

Suggested solution

This bug is due to an older version of starlette. Was fixed in tiangolo/fastapi#2342

@creatorrr creatorrr added the bug Something isn't working label May 26, 2021
@deliahu
Copy link
Member

deliahu commented May 26, 2021

@creatorrr thanks for opening this issue.

I was unable to recreate the error. I tried a simple handler like this:

from starlette.responses import Response

class Handler:
    def __init__(self, config):
        pass

    def handle_post(self, payload, query_params):
        return Response("ok")

Do you mind creating a minimal example which reproduces the error? Perhaps it has to do with the contents of your requirements.txt?

@creatorrr
Copy link
Author

It’s happening with starlette==0.14.2 but goes away with starlette==0.14.0

@deliahu
Copy link
Member

deliahu commented May 27, 2021

Yes, and it also seems to work by default if starlette is not specified in requirements.txt (the pre-installed version is 0.13.6). It is generally not recommended to change the version of cortex's internal dependencies (such as starlette); is there a feature that you need access to in starlette 0.14?

@creatorrr
Copy link
Author

Using a library that depends on ^0.14.2, I was able to pin to a lower version but it'd be great if it was possible to update cortex deps.

@deliahu
Copy link
Member

deliahu commented May 28, 2021

Yes, that makes sense. Have you attempted pinning startlette and fastapi to the appropriate versions? E.g.

# requirements.txt

fastap==0.65.1
starlette==0.14.2

@creatorrr
Copy link
Author

Yes that works (pinning starlette==0.14.0). It’d be great to have it documented somewhere if possible.

@deliahu
Copy link
Member

deliahu commented Jun 3, 2021

Awesome! Yes, we will look into documenting this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants