Skip to content

Exception in ASGI application with newest Fastapi #45

New issue

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

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

Already on GitHub? # to your account

Closed
Sieboldianus opened this issue Jul 30, 2019 · 5 comments
Closed

Exception in ASGI application with newest Fastapi #45

Sieboldianus opened this issue Jul 30, 2019 · 5 comments

Comments

@Sieboldianus
Copy link

I've successfully used the Fullstack fastapi template in the past. With the recent version of fastapi installed through the Dockerfile, I get the following error when I log on to docker-compose logs -f backend and open up https://localhost/:

backend_1        | INFO: ('172.22.0.2', 39320) - "GET /api/v1/users/me HTTP/1.1" 500
backend_1        | ERROR: Exception in ASGI application
backend_1        | Traceback (most recent call last):
backend_1        |   File "/usr/local/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 368, in run_asgi
backend_1        |     result = await app(self.scope, self.receive, self.send)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/applications.py", line 133, in __call__
backend_1        |     await self.error_middleware(scope, receive, send)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/middleware/errors.py", line 122, in __call__
backend_1        |     raise exc from None
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/middleware/errors.py", line 100, in __call__
backend_1        |     await self.app(scope, receive, _send)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/middleware/base.py", line 25, in __call__
backend_1        |     response = await self.dispatch_func(request, self.call_next)
backend_1        |   File "./app/main.py", line 34, in db_session_middleware
backend_1        |     response = await call_next(request)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/middleware/base.py", line 45, in call_next
backend_1        |     task.result()
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/middleware/base.py", line 38, in coro
backend_1        |     await self.app(scope, receive, send)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/middleware/cors.py", line 76, in __call__
backend_1        |     await self.app(scope, receive, send)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/exceptions.py", line 73, in __call__
backend_1        |     raise exc from None
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/exceptions.py", line 62, in __call__
backend_1        |     await self.app(scope, receive, sender)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 585, in __call__
backend_1        |     await route(scope, receive, send)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 207, in __call__
backend_1        |     await self.app(scope, receive, send)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 40, in app
backend_1        |     response = await func(request)
backend_1        |   File "/usr/local/lib/python3.7/site-packages/fastapi/routing.py", line 122, in app
backend_1        |     skip_defaults=response_model_skip_defaults,
backend_1        |   File "/usr/local/lib/python3.7/site-packages/fastapi/routing.py", line 54, in serialize_response
backend_1        |     raise ValidationError(errors)
backend_1        | pydantic.error_wrappers.ValidationError: 1 validation error
backend_1        | response
backend_1        |   value is not a valid dict (type=type_error.dict)

There's a small chance that this error relates to some code edits I have introduced, but I suspect this is related to the latest changes to how pydantic validates models in newer Fastapi versions. Anyone else experiencing this? I haven't tested it with a fresh full-stack-fastapi-postgresql, but will check as soon as possible..

Just a note: login still works, perhaps this is just a backend error with no consequences, it is still annoying seeing this error every time the login screen is requested.

@Sieboldianus
Copy link
Author

Sieboldianus commented Jul 30, 2019

Just noticed that this seems already addressed in PR 43. I've tested the PR and it works! This can be closed..

@jf---
Copy link

jf--- commented Aug 16, 2019

@Sieboldianus I ran into this issue, it still comes up running the current master. Can I suggest to keep this issue open until #43 is closed?

@Sieboldianus Sieboldianus reopened this Aug 17, 2019
@ghost
Copy link

ghost commented Aug 21, 2019

I can confirm I have the same issue running the current master. Thanks.

Edit:
I can also confirm that #43 seems to address the "Exception in ASGI".

This is awesome! Thanks.

@tiangolo
Copy link
Member

Thanks for the report and discussion here everyone!

This should be fixed in the current master. 🎉 🚀

So I guess we could close this issue, right @Sieboldianus ?

@ajithyb225
Copy link

Can any one explain where to add the changes ??
i am little bit confused :)
P.S: I am a complete rookie into fastapi
I am using POSTman to upload an image to the server ..but showing internal server error..can anyone help me with this!!
using the latest version of every library .

Error Log:

Started server process [4532]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://localhost:8000 (Press CTRL+C to quit)
INFO: ::1:49810 - "GET /ping HTTP/1.1" 200 OK
INFO: ::1:49811 - "GET /predict HTTP/1.1" 405 Method Not Allowed
INFO: ::1:49819 - "POST /predict HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "C:\Users\ajith\AppData\Local\Programs\Python\Python310\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 366, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "C:\Users\ajith\AppData\Local\Programs\Python\Python310\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 75, in call
return await self.app(scope, receive, send)
File "C:\Users\ajith\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\applications.py", line 261, in call
await super().call(scope, receive, send)
File "C:\Users\ajith\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\applications.py", line 112, in call
await self.middleware_stack(scope, receive, send)
File "C:\Users\ajith\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\middleware\errors.py", line 181, in call
raise exc
File "C:\Users\ajith\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\middleware\errors.py", line 159, in call
await self.app(scope, receive, _send)
File "C:\Users\ajith\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\exceptions.py", line 82, in call
raise exc
File "C:\Users\ajith\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\exceptions.py", line 71, in call
await self.app(scope, receive, sender)
File "C:\Users\ajith\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 21, in call
raise e
File "C:\Users\ajith\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 18, in call
await self.app(scope, receive, send)
File "C:\Users\ajith\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 656, in call
await route.handle(scope, receive, send)
File "C:\Users\ajith\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 259, in handle
await self.app(scope, receive, send)
File "C:\Users\ajith\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 61, in app
response = await func(request)
File "C:\Users\ajith\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\routing.py", line 227, in app
raw_response = await run_endpoint_function(
File "C:\Users\ajith\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\routing.py", line 160, in run_endpoint_function
return await dependant.call(**values)
File "C:/Users/ajith/Desktop/Project/api/main.py", line 22, in predict
image = read_file_as_image( file.read())
File "C:/Users/ajith/Desktop/Project/api/main.py", line 16, in read_file_as_image
image = np.array(Image.open(BytesIO(data)))
TypeError: a bytes-like object is required, not 'coroutine'

CODE:

import uvicorn
from fastapi import FastAPI,File,UploadFile
import numpy as np
from io import BytesIO
from PIL import Image
import tensorflow as tf

app = FastAPI()

MODEL = tf.keras.models.load_model("C:/Users/ajith/Desktop/Project/Models/1")
['Healthy', 'Kole Roga']
@app.get("/ping")
async def ping():
return "Hello World:)"
def read_file_as_image(data) -> np.ndarray:
image = np.array(Image.open(BytesIO(data)))
return image

@app.post("/predict")
async def predict( file:UploadFile=File(...) ):

image = read_file_as_image( file.read())
img_batch = np.expand_dims(image, 0)

predictions = MODEL.predict(img_batch)
pass

if name=="main":
uvicorn.run(app, host="localhost", port=8000)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants