Skip to content

Problem rendering numpy.float32 through ORJSONRESPONSE #1733

@yadavshashank

Description

@yadavshashank

First check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.
  • After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

Code

from fastapi.responses import ORJSONResponse

app = FastAPI()

# Payload type
class Tweet(BaseModel):
    text: str

predict = Predict()

@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.post("/sentiment", response_class=ORJSONResponse)
async def sentiment(tweet:Tweet):  
    data = predict.get_sentiment(tweet.text)

    # data contains the following keys
    # data = { "sentiment": type -> text, "polarity": type -> numpy.float32}
    return data

Description

I have exposed an endpoint which returns the sentiment of a text and its polarity (type ->numpy.float32).

This throws an error:

ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "c:\users\shash\anaconda3\envs\sentiment_analysis\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 388, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "c:\users\shash\anaconda3\envs\sentiment_analysis\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "c:\users\shash\anaconda3\envs\sentiment_analysis\lib\site-packages\fastapi\applications.py", line 183, in __call__
    await super().__call__(scope, receive, send)  # pragma: no cover
  File "c:\users\shash\anaconda3\envs\sentiment_analysis\lib\site-packages\starlette\applications.py", line 102, in __call__
    await self.middleware_stack(scope, receive, send)
  File "c:\users\shash\anaconda3\envs\sentiment_analysis\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
    raise exc from None
  File "c:\users\shash\anaconda3\envs\sentiment_analysis\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "c:\users\shash\anaconda3\envs\sentiment_analysis\lib\site-packages\starlette\exceptions.py", line 82, in __call__
    raise exc from None
  File "c:\users\shash\anaconda3\envs\sentiment_analysis\lib\site-packages\starlette\exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "c:\users\shash\anaconda3\envs\sentiment_analysis\lib\site-packages\starlette\routing.py", line 550, in __call__
    await route.handle(scope, receive, send)
  File "c:\users\shash\anaconda3\envs\sentiment_analysis\lib\site-packages\starlette\routing.py", line 227, in handle
    await self.app(scope, receive, send)
  File "c:\users\shash\anaconda3\envs\sentiment_analysis\lib\site-packages\starlette\routing.py", line 41, in app
    response = await func(request)
  File "c:\users\shash\anaconda3\envs\sentiment_analysis\lib\site-packages\fastapi\routing.py", line 213, in app
    is_coroutine=is_coroutine,
  File "c:\users\shash\anaconda3\envs\sentiment_analysis\lib\site-packages\fastapi\routing.py", line 136, in serialize_response
    return jsonable_encoder(response_content)
  File "c:\users\shash\anaconda3\envs\sentiment_analysis\lib\site-packages\fastapi\encoders.py", line 115, in jsonable_encoder
    sqlalchemy_safe=sqlalchemy_safe,
  File "c:\users\shash\anaconda3\envs\sentiment_analysis\lib\site-packages\fastapi\encoders.py", line 160, in jsonable_encoder
    raise ValueError(errors)
ValueError: [TypeError("'numpy.float32' object is not iterable",), TypeError('vars() argument must have __dict__ attribute',)]

Additional comments

This does not occur if the value of polarity is typecast to float before returning the response.

Environment

  • OS: Windows:
  • FastAPI Version : 0.59.0
  • Python version: 3.6.10

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions