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

automatically dump dictionary to json in encode_response while Streaming #84

Closed
aniketmaurya opened this issue May 11, 2024 · 0 comments · Fixed by #85
Closed

automatically dump dictionary to json in encode_response while Streaming #84

aniketmaurya opened this issue May 11, 2024 · 0 comments · Fixed by #85
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@aniketmaurya
Copy link
Collaborator

aniketmaurya commented May 11, 2024

For a typical Streaming example, if the LitAPI.encode_response yields a dictionary or any non primitive dtype then server might run into error since FastAPI StreamingResponse doesn't accept dictionary.

Proposal: LitServe automatically detects and dumps a dictionary or Pydantic object into a JSON string value of the same so that FastAPI doesn't run into the following error.

During handling of the above exception, another exception occurred:

  + Exception Group Traceback (most recent call last):
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 407, in run_asgi
  |     result = await app(  # type: ignore[func-returns-value]
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 69, in __call__
  |     return await self.app(scope, receive, send)
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/fastapi/applications.py", line 1054, in __call__
  |     await super().__call__(scope, receive, send)
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/starlette/applications.py", line 123, in __call__
  |     await self.middleware_stack(scope, receive, send)
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/starlette/middleware/errors.py", line 186, in __call__
  |     raise exc
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/starlette/middleware/errors.py", line 164, in __call__
  |     await self.app(scope, receive, _send)
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 65, in __call__
  |     await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
  |     raise exc
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
  |     await app(scope, receive, sender)
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/starlette/routing.py", line 756, in __call__
  |     await self.middleware_stack(scope, receive, send)
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/starlette/routing.py", line 776, in app
  |     await route.handle(scope, receive, send)
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/starlette/routing.py", line 297, in handle
  |     await self.app(scope, receive, send)
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/starlette/routing.py", line 77, in app
  |     await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
  |     raise exc
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
  |     await app(scope, receive, sender)
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/starlette/routing.py", line 75, in app
  |     await response(scope, receive, send)
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/starlette/responses.py", line 258, in __call__
  |     async with anyio.create_task_group() as task_group:
  |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 678, in __aexit__
  |     raise BaseExceptionGroup(
  | exceptiongroup.ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/starlette/responses.py", line 261, in wrap
    |     await func()
    |   File "/home/zeus/miniconda3/envs/cloudspace/lib/python3.10/site-packages/starlette/responses.py", line 252, in stream_response
    |     chunk = chunk.encode(self.charset)
    | AttributeError: 'dict' object has no attribute 'encode'

cc: @lantiga

@aniketmaurya aniketmaurya added enhancement New feature or request help wanted Extra attention is needed labels May 11, 2024
@aniketmaurya aniketmaurya changed the title automatically dump dictionary to json automatically dump dictionary to json in encode_response while Streaming May 11, 2024
@aniketmaurya aniketmaurya self-assigned this May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant