Skip to content

Celery results are NOT obeying results serializer config in databases #3025

Closed

Description

@fxfitz

So, we're working on getting all of our tasks results stored in a database backend (for now just postgres, but we're not tied to any particular backend).

Anyway, we've noticed that no matter what we try to do, the results are always stored as a pickle object, even though we have the other configurations set properly:

app.conf.update(CELERY_ACCEPT_CONTENT=["json"],
                CELERY_TASK_SERIALIZER="json",
                CELERY_RESULT_SERIALIZER="json")

While not being terribly familiar with SQL Alchemy, I do believe we have found the offending line.

Am I understanding this right? To me, it looks like this has been a bug for years... is it because we might be the only ones using a database backend? :-P

Thanks!

Activity

  1. czchen commented on Jan 26, 2016

    @czchen

    @fxfitz, you are not along. We plan to give MongoDB a try to see if it is is more suitable for our need.

  2. fxfitz commented on Jan 26, 2016

    @fxfitz
    Author

    @czchen That's great to hear! MongoDB appears to work, so good luck! I spoke with someone on IRC about this last night, and it looks like they want to see a patch for v4.0 first, and then they'll think about backporting to 3.1.

    They're obviously concerned about making any breaking changes, but in the meantime I think documentation should be amended to indicate the bug/inconsistency and this issue should be left open and unresolved.

    On a side note, we may write a custom backend that will obey CELERY_RESULT_SERIALIZER. If I do, I'll contribute upstream.

  3. added this to the milestone on Jun 29, 2016
  4. auvipy commented on Jan 19, 2018

    @auvipy
    Member

    what's the update on the issue? any pr? workaround?

  5. jfach commented on Mar 23, 2018

    @jfach

    @fxfitz have you been able to solve this issue? Currently dealing with this myself using Sqlite.

    @auvipy any internal information/updates you can give us?

  6. fxfitz commented on Mar 23, 2018

    @fxfitz
    Author

    Sadly, I moved off of that project and am working on something else now. Sorry!

  7. jfach commented on Mar 23, 2018

    @jfach

    @fxfitz Ok, thanks for the quick response

  8. nick4u commented on Mar 8, 2019

    @nick4u

    Guys .. I lost a lot of time trying to figure it out what is wrong with results stored in database as I expected them to be JSON and there were actually storead as Pickle ... This bug/issue is hanging a long time and the least decent thing to do is to state in documentation that even when you configure as recommended:

        task_serializer='json',
        accept_content=['json'], 
        result_serializer='json',
    

    in-database results will be Pickle objects.
    Best regards - great project.

  9. self-assigned this
    on Mar 12, 2019
  10. thedrow commented on Apr 4, 2019

    @thedrow
    Contributor

    I don't see the offending line anymore.

    @nick4u Did you try this on a recent version of Celery?
    Also, if you feel like adding a note about this, feel free to issue a PR.

  11. adimux commented on Apr 19, 2019

    @adimux

    The offending line is still there: https://github.com/celery/celery/blob/master/celery/backends/database/models.py#L29
    The result column is of type Pickle. It works because the pickle type simply stores anything you pass to it as a BLOB in the DB and restores it when reading from the field.
    The question is: should the result be serialized using the result serializer and be stored in a text field?

  12. 24 remaining items

  13. added this to the 5.3 milestone on Sep 11, 2021
  14. removed this from the 5.3 milestone on Feb 19, 2023
  15. rodionterekhin commented on Nov 1, 2024

    @rodionterekhin

    Still no progress in 2024???

  16. thedrow commented on Nov 5, 2024

    @thedrow
    Contributor

    Contributions are welcome.

  17. tamersalama commented on Mar 11, 2025

    @tamersalama

    Who's here in 2025?

  18. added this to the 5.7.0 milestone on May 19, 2025
  19. WZhijun154 commented on Jun 2, 2025

    @WZhijun154

    hope updates on this issue/bug

  20. auvipy commented on Jun 2, 2025

    @auvipy
    Member

    I will personally handle this. Very soon.

  21. ghirailghiro commented on Aug 30, 2026

    @ghirailghiro
    Contributor

    Hi @auvipy opened #10536, building on the approach from #2881 and #5907.

    The bug is that Task.result/TaskSet.result use sa.PickleType, which
    pickles/unpickles at the ORM level regardless of result_serializer.

    The fix i tried to implement is that columns become sa.LargeBinary (same DDL as PickleType under the
    hood, no migration needed) and the backend now calls encode()/decode()
    itself, same idea as #2881/#5907. Old rows are always pickle no matter the
    configured serializer, so decoding falls back to pickle.loads() when the
    configured serializer can't parse a row.

    In #5907 @auvipy's comment about a native JSON column — tried it, doesn't work:
    inserting pickle bytes into a sa.JSON column raises "Object of type
    bytes is not JSON serializable". Since result_serializer can also be
    pickle/msgpack/yaml, LargeBinary is the only type that works for all of
    them.

    I added a couple of tests on the PR for testing backward-compat

  22. modified the milestones: 5.7.0, 5.8.0 on Aug 31, 2026
  23. modified the milestones: 5.8.0, 5.7.0 on Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions