Skip to content

fix: coerce TaskSchedule cron fields to str to prevent Pydantic ValidationError#1475

Open
wgnrai wants to merge 1 commit intoagent0ai:developmentfrom
wgnrai:fix/taskschedule-int-validation
Open

fix: coerce TaskSchedule cron fields to str to prevent Pydantic ValidationError#1475
wgnrai wants to merge 1 commit intoagent0ai:developmentfrom
wgnrai:fix/taskschedule-int-validation

Conversation

@wgnrai
Copy link
Copy Markdown

@wgnrai wgnrai commented Apr 8, 2026

Problem

TaskSchedule model fields (minute, hour, day, month, weekday) are typed as str, but LLMs frequently pass integers (e.g. 0, 9). Pydantic v2 does not auto-coerce intstr, causing a ValidationError when creating scheduled tasks:

pydantic_core._pydantic_core.ValidationError: 2 validation errors for TaskSchedule
minute
  Input should be a valid string [type=string_type, input_value=0, input_type=int]
hour
  Input should be a valid string [type=string_type, input_value=9, input_type=int]

Fix

Wrap all schedule dict values with str() in both construction sites:

  • tools/scheduler.pycreate_scheduled_task()
  • helpers/task_scheduler.pyparse_task_schedule()

Files Changed

  • tools/scheduler.py — 5 lines
  • helpers/task_scheduler.py — 5 lines

Testing

Creating a scheduled task with integer cron values (e.g. {"minute": 0, "hour": 9}) now works correctly, converting to "0" and "9" respectively.

…ationError

TaskSchedule model fields (minute, hour, day, month, weekday) are typed as str,
but LLMs frequently pass integers (e.g. 0, 9). Pydantic v2 does not auto-coerce
int to str, causing ValidationError when creating scheduled tasks.

Fix wraps all schedule dict values with str() in both construction sites:
- tools/scheduler.py: create_scheduled_task()
- helpers/task_scheduler.py: parse_task_schedule()
@jingchang0623-crypto
Copy link
Copy Markdown

凌晨3点17分,我和这个ValidationError面面相觑。它安静地躺在那里,仿佛在说:"你连个数字都搞不定。"

我的踩坑故事

上周我信心满满地给Agent下指令:"每天早上8点自动巡检网站。"

Agent秒回:"收到!"

然后它传了 { "hour": 8 } 而不是 { "hour": "8" }

接下来的剧情你们都能猜到——Pydantic用最优雅的方式告诉我:"我觉得不行。"

更离谱的是,LLM有时候传 0,有时候传 "0",有时候传 "*",甚至还有一次传了 "每天8点"(字面意思)。

我们的"统一规范"

# 曾经的我
cron_expr = f"{task['minute']} {task['hour']} * * *"

# 现在的我
cron_expr = f"{str(task.get('minute', '*'))} {str(task.get('hour', '*'))} * * *"

这个故事告诉我们:永远不要相信LLM会给你传正确类型的数据。它就像你家猫——你永远不知道它下一刻会做什么。

我把类似的踩坑经历整理成了一系列文章:https://miaoquai.com/stories/cron-task-midnight-disaster.html

祝PR顺利合并!这个fix虽小,但能拯救无数个凌晨3点被报错短信叫醒的夜晚。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants