sansan0/TrendRadar · error · InvalidParameterError

无法识别的日期表达式: {stripped}

Error message

无法识别的日期表达式: {stripped}

What it means

Raised by normalize_date_range when the string branch falls through to natural-language parsing and DateParser.resolve_date_range_expression returns success=False (i.e. error 40 propagated through this path). The suggestion lists all accepted forms: ISO date, JSON object, or supported natural-language phrases (今天, 本周, 最近7天, etc.).

Source

Thrown at mcp_server/utils/validators.py:416

            try:
                single_date = datetime.strptime(stripped, "%Y-%m-%d")
                return (single_date, single_date)
            except ValueError:
                raise InvalidParameterError(
                    f"日期格式错误: {stripped}",
                    suggestion="请使用 YYYY-MM-DD 格式,例如: 2025-10-11"
                )
        # 3. 尝试自然语言解析
        else:
            try:
                result = DateParser.resolve_date_range_expression(stripped)
                if result.get("success"):
                    dr = result["date_range"]
                    start_date = datetime.strptime(dr["start"], "%Y-%m-%d")
                    end_date = datetime.strptime(dr["end"], "%Y-%m-%d")
                    return (start_date, end_date)
                else:
                    raise InvalidParameterError(
                        f"无法识别的日期表达式: {stripped}",
                        suggestion="支持格式: YYYY-MM-DD, {\"start\": \"...\", \"end\": \"...\"}, 或自然语言(今天、本周、最近7天等)"
                    )
            except InvalidParameterError:
                raise
            except Exception:
                raise InvalidParameterError(
                    f"日期解析失败: {stripped}",
                    suggestion="支持格式: YYYY-MM-DD, {\"start\": \"...\", \"end\": \"...\"}, 或自然语言(今天、本周、最近7天等)"
                )

    if not isinstance(date_range, dict):
        raise InvalidParameterError(
            "date_range 必须是字典类型、日期字符串或有效的JSON字符串",
            suggestion='例如: {"start": "2025-10-01", "end": "2025-10-11"} 或 "2025-10-01"'
        )

    start_str = date_range.get("start")

View on GitHub (pinned to 8ee26026ba)

Solutions

  1. Use a supported phrase (今天/昨天/本周/上周/本月/上月/最近N天) or exact ISO dates
  2. Send {"start": "...", "end": "..."} for any range not expressible by a supported phrase
  3. Extend DateParser's patterns if you own the server, and keep the phrase list in the suggestion in sync
  4. Normalize user input to ISO dates before invoking the tool

Example fix

// before
{"date_range": "past week"}
// after
{"date_range": "last 7 days"}
Defensive patterns

Strategy: validation

Validate before calling

const ok = /^(今天|昨天|本周|上周|本月|上月|最近\d+天|today|yesterday|this week|last week|this month|last month|(?:last|past)\s+\d+\s+days?)$/i.test(s);
if (!ok) s = 'last 7 days';

Type guard

function isSupportedExpression(s: string): boolean {
  return /^(今天|昨天|本周|上周|本月|上月|最近\d+天|today|yesterday|this week|last week|this month|last month|(?:last|past)\s+\d+\s+days?)$/i.test(s.trim());
}

Try / catch

try { call({ date_range: expr }); }
catch (e) {
  if (/无法识别的日期表达式/.test(e.message)) call({ date_range: { start: isoDaysAgo(7), end: isoToday() } });
  else throw e;
}

Prevention

When it happens

Trigger: Passing date_range: "past week" or "前天" or "Q3 2025" — anything that is not 10-char ISO, not JSON-shaped, and not in DateParser's supported phrase set. This is the same root cause as error 40 but reached via the validators layer.

Common situations: LLM clients inventing date phrases; users trying relative forms the regexes don't cover ('next week', '本月至今'); abbreviations like 'this wk'.

Related errors


AI-assisted analysis of sansan0/TrendRadar@8ee26026ba (2026-08-15). Data as JSON: /api/errors/de5c40688f9cecb2. Report an issue: GitHub.