taskiq
taskiq copied to clipboard
Task arguments with Pydantic models and aliases
First, I'd like to thank the author and all contributors for creating and maintaining this great library.
I encountered an issue while deserializing Pydantic models with aliased fields from task arguments.
As a temporary workaround, I created a custom wrapper around AsyncKicker class to call Pydantic model_dump with by_alias=True.
However, I was wondering if there might be a better solution to allow developers to more easily customize Pydantic serialization/deserialization.
Example
import asyncio
from pydantic import BaseModel, Field
from taskiq_redis import ListQueueBroker, RedisAsyncResultBackend
REDIS_URL = "redis://localhost:6379"
broker = ListQueueBroker(REDIS_URL).with_result_backend(RedisAsyncResultBackend(REDIS_URL))
class ExampleModel(BaseModel):
value_: str = Field(alias="value")
@broker.task
async def my_task(data: ExampleModel):
print("Hello task")
async def main():
data = ExampleModel(value="example")
await my_task.kiq(data)
if __name__ == "__main__":
asyncio.run(main())
The receiver raises an error when attempting to cast the task arguments.
[][taskiq.receiver.params_parser][WARNING][worker-0] Can't parse argument 0 for task my_task. Reason: 1 validation error for ExampleModel
value
Field required [type=missing, input_value={'value_': 'example'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.10/v/missing
I second this. This is a regular use case especially when using FastAPI, so it should support aliases out of the box instead of throwing strange errors.