Skip to content

Commit

Permalink
Fixed pydantic serializartion issue.
Browse files Browse the repository at this point in the history
Whisper is running now.
  • Loading branch information
movchan74 committed Nov 8, 2023
1 parent fcb3c77 commit dde9313
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion aana/api/responses.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
from typing import Any, Optional
from fastapi.responses import JSONResponse
import orjson
from pydantic import BaseModel


def orjson_default(obj: Any) -> Any:
"""
Default function for orjson.dumps to handle pydantic models.
If orjson does not know how to serialize an object, it calls the default function.
If we see that the object is a pydantic model,
we call the dict method to get the dictionary representation of the model that orjson can serialize.
If the object is not a pydantic model, we raise a TypeError.
Args:
obj (Any): The object to serialize.
Returns:
Any: The serialized object.
Raises:
TypeError: If the object is not a pydantic model.
"""

if isinstance(obj, BaseModel):
return obj.dict()
raise TypeError


class AanaJSONResponse(JSONResponse):
Expand All @@ -23,4 +50,4 @@ def render(self, content: Any) -> bytes:
"""
Override the render method to use orjson.dumps instead of json.dumps.
"""
return orjson.dumps(content, option=self.option)
return orjson.dumps(content, option=self.option, default=orjson_default)

0 comments on commit dde9313

Please sign in to comment.