taskiq
taskiq copied to clipboard
Pydantic model arguments with bytes cannot be sent to a worker
I have a Pydantic model which has a "bytes" field:
class RawData:
info: str
bytes: bytes
And some task definition, for example:
@worker.task
async def some_task(data: RawData):
...
But when it tries to send it to a worker, it raises UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid utf-8. After digging into the source code, it turned out the library uses model_dump(mode='json') when preparing the message:
def model_dump(instance: Model) -> Dict[str, Any]:
return instance.model_dump(mode="json")
And it just can't decode some byte to UTF8. When using model_dump without mode="json", it works though.
I can solve this by sending the fields as parameters of a task, but it's not the way I want it to work. Any chance it can be fixed?