[BUG] Python SDK: unable to disable task retries if any default is set
Describe the issue
When default retries are set using TaskDefaults(retries=3), you can't disable them for a specific task, as the @task decorator has retries=0 as default value (instead of None or some other sentinel value)
Environment
- SDK: Python 1.37.2
- Engine: irrelevant
Expected behavior You can disable retries on individual tasks
Code to Reproduce, Logs, or Screenshots
In this example, no_retry_task will have retries=3
some_wf = hatchet.workflow(
name="SomeWF",
task_defaults=TaskDefaults(
retries=3
),
)
@some_wf.task(retries=0)
def no_retry_task(...):
...
Additional context The code handling defaults is here https://github.com/hatchet-dev/hatchet/blob/main/sdks/python/hatchet_sdk/runnables/workflow.py#L139-L143
🤖 AI Disclosure
This was written entirely by hand
Thanks, this is definitely a bug. Feel free to open a PR with a fix!
I'd like to take this one. Requesting assignment per CONTRIBUTING step 2.
I have the fix working locally. Summary of what I found, in case it changes how you'd want it done:
The sentinel is the whole problem. fall_back_to_default decides "was this supplied?" by comparing against the param default:
if value != param_default:
return value
That works for schedule_timeout and friends, where the default is a value nobody passes deliberately. It cannot work for retries, where 0 is both the param default and a value users pass on purpose. So retries=0 is indistinguishable from "unset" and the TaskDefaults value wins.
My approach is to give retries a None sentinel, matching what TaskDefaults.retries already uses:
- add
fall_back_to_default_optional, which treats onlyNoneas unset, and leavefall_back_to_defaultuntouched for the other params retries: int = 0becomesretries: int | None = NoneonWorkflow.taskandWorkflow.durable_taskComputedTaskParameters.retriesbecomesint | None, with aresolved_retriesproperty so call sites still get a plainint
Two things worth flagging:
on_failure_task and on_success_task are unaffected. They build Task(...) directly and never go through ComputedTaskParameters, so they never consulted TaskDefaults in the first place and retries=0 already works there. I left their signatures alone rather than widen the API for no behavioural gain. Say the word if you'd rather they were consistent.
Batch tasks pass a literal retries=0 into ComputedTaskParameters and expose no retries param, so I left that alone too.
Verification so far: reproduced the bug first (retries=0 + TaskDefaults(retries=3) resolves to 3), added a parametrised regression test to tests/test_task_default_fallbacks.py next to the existing ones, and confirmed the whole file passes (18 tests). I also reverted each part of the fix in turn to check the tests actually catch it — reverting the sentinel fails the new test, and reverting the decorator defaults fails 4 existing tests. black, ruff and mypy are clean on both files, with mypy showing the same 2 pre-existing missing-stub errors before and after.
One caveat: I could not run the repo's full suite locally because the root conftest.py starts a live worker via poetry run python examples/worker.py and CI mints a real token. I ran the default-fallback tests under the tests/unit/ conftest, which already overrides that fixture.
Happy to open the PR once assigned, or to hand this over if someone else is already on it.
🤖 AI Disclosure
-
[x] I acknowledge that an LLM was used in the creation of this comment, in accordance with Hatchet's AI_POLICY.md.
-
Details: An LLM assisted with source investigation, drafting the fix and the regression test, and writing this comment. All results reported above were executed and verified locally.
Hi team, I would like to work on this issue. I have reproduced the bug with tests and prepared a fix. Could you please assign this to me?
yeah, this is the same bug aayush hit. retries=0 needs to mean no retries, not fallback to task_defaults.
using the param default to detect "not set" breaks here, so this should switch to a sentinel for retries too. i think that matches the expected behavior and won't change other task defaults. should i go ahead with the fix?
@mrkaye97 Hi, I'd like to take this one if it's still available. The issue looks scoped to distinguishing an explicit etries=0 override from the unset/default case in the Python SDK, and I can add regression coverage for task defaults vs per-task overrides. Could you assign it to me?
Could I pick this one up? Per CONTRIBUTING I'd need the issue assigned to me before opening a PR.
I have the fix working locally. The cause is what you identified: task and
durable_task declare retries: int = 0, and ComputedTaskParameters resolves
it with param_default=0, so fall_back_to_default reads an explicit
retries=0 as "not provided" and the TaskDefaults value wins.
The shape I've gone with is to resolve retries against None — the same
sentinel backoff_factor and backoff_max_seconds already use in that
validator — and settle an unspecified value to 0 afterwards. I left
on_failure_task and on_success_task at int = 0, since they pass retries
straight to Task and never consult TaskDefaults, so 0 already means zero
there.
Verified against your reproduction, for both regular and durable tasks: explicit
retries=0 with TaskDefaults(retries=3) now gives 0, an omitted retries
still inherits 3, and with no defaults set it stays 0. With the change reverted,
exactly the two retries=0 cases fail and the inheritance cases still pass.
Tests added to tests/test_task_default_fallbacks.py.
Happy to adjust the approach if you'd rather it went another way.
Opened #5075 for this.
@sundeep8967 @eskayML @Tyagiquamar @udsy19 — apologies, several of us converged on this one and none of us was ever assigned. I had the fix sitting locally since August and only just pushed it, so if any of you already has a branch, please say so and I am happy to close mine in favour of it or review yours instead. @eskayML and @udsy19 both described the same sentinel approach I took, so the diagnosis was clearly not hard to reach.
For what it is worth, the shape I went with: retries gets a None sentinel (matching TaskDefaults.retries, which is already int | None) through a separate fall_back_to_default_optional, leaving every other parameter on the existing comparison. on_failure_task and on_success_task are untouched on purpose — they build Task directly rather than going through validate_params, so they never consult TaskDefaults.
@mrkaye97 the PR (#5075) was auto-closed by close-unaccepted-prs, which is working as intended — it requires the linked issue to carry the accepted label and list the PR author in assignees, and this issue has neither. That is my mistake: you said "feel free to open a PR" back in August, so I opened one without checking .github/workflows for a gate first.
The branch is pushed and the PR body is intact, so if you add accepted here and assign it to whoever you want to take it, the PR reopens cleanly — the workflow re-runs on reopened.
Given five of us have asked for this one, please don't assign it to me by default. If @sundeep8967, @eskayML, @Tyagiquamar or @udsy19 already has a branch, theirs should go first and I will review it rather than compete. I only pushed because the work had been sitting on my machine since August and the issue was still open with no PR against it.
The diff is on aayushbaluni:fix/4727-explicit-zero-retries if it is useful to anyone regardless of who ends up owning it.
I had worked on this locally as well and reached essentially the same sentinel-based fix, but I hadn't pushed the branch before cleaning up my local checkout.
Given that several of us independently converged on the same approach, I'm happy not to duplicate the work. If #5075 moves forward, I'd be happy to help review or test it. Alternatively, feel free to assign me another issue that needs help and I'll pick that up instead.
Thanks @Tyagiquamar — that is generous, and for what it is worth your reading was right; four of us landing on the same sentinel says the diagnosis was the easy part and the waiting was the hard part.
If #5075 gets unblocked I would genuinely value a review, particularly on the scope call: on_failure_task and on_success_task keep retries: int = 0 because they construct Task directly rather than going through validate_params, so they never read TaskDefaults at all. That is the one judgement in the diff I would most like a second pair of eyes on.
It still needs a maintainer to add accepted and assign the issue before the PR can reopen — the gate re-runs on reopen, so neither of us can move it from here.
Thanks @aayushbaluni and @Tyagiquamar! I already have the fix ready in #4795. @mrkaye97 whenever you have a moment, could you please add the accepted label and assign this issue to me so we can reopen #4795?
should be fixed in the upcoming v2.0.0 release (there's an alpha on pypi now): #3545