sansan0/TrendRadar · error · InvalidParameterError

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

Error message

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

What it means

Raised by DateParser.resolve_date_range_expression when a natural-language date expression matches none of the supported Chinese or English patterns (e.g. 今天/昨天/本周/最近N天, today/last N days). The parser first normalizes the expression via regex matching; if normalization yields nothing, it rejects the input as unrecognizable. The suggestion lists all supported expressions in both languages.

Source

Thrown at mcp_server/utils/date_parser.py:403

            # 中文: 最近N天
            cn_match = re.match(r'最近(\d+)天', expression_lower)
            if cn_match:
                days = int(cn_match.group(1))
                normalized = f"last_{days}_days"

            # 英文: last N days
            en_match = re.match(r'(?:last|past)\s+(\d+)\s+days?', expression_lower)
            if en_match:
                days = int(en_match.group(1))
                normalized = f"last_{days}_days"

        if not normalized:
            # 提供支持的表达式列表
            supported_cn = ["今天", "昨天", "本周", "上周", "本月", "上月",
                           "最近7天", "最近30天", "最近N天"]
            supported_en = ["today", "yesterday", "this week", "last week",
                           "this month", "last month", "last 7 days", "last N days"]
            raise InvalidParameterError(
                f"无法识别的日期表达式: {expression}",
                suggestion=f"支持的表达式:\n中文: {', '.join(supported_cn)}\n英文: {', '.join(supported_en)}"
            )

        # 3. 根据 normalized 类型计算日期范围
        start_date, end_date, description = DateParser._calculate_date_range(
            normalized, today
        )

        return {
            "success": True,
            "expression": expression,
            "normalized": normalized,
            "date_range": {
                "start": start_date.strftime("%Y-%m-%d"),
                "end": end_date.strftime("%Y-%m-%d")
            },
            "current_date": today_str,

View on GitHub (pinned to 8ee26026ba)

Solutions

  1. Use one of the supported expressions listed in the suggestion: 今天, 昨天, 本周, 上周, 本月, 上月, 最近N天 (CN) or today, yesterday, this week, last week, this month, last month, last N days (EN)
  2. If you need an exact range, bypass the parser and pass explicit dates: {"start": "2025-10-01", "end": "2025-10-11"} or a single "YYYY-MM-DD" string
  3. If you control the codebase, extend the regex patterns in DateParser (e.g. add '(?:in the )?(?:last|past)\s+(\d+)\s+days?' or '前天') and add the phrase to the supported lists in the error suggestion
  4. Pre-normalize free-text on the caller side into a canonical expression before calling the tool

Example fix

// before
DateParser.resolve_date_range_expression("past week")
// after
DateParser.resolve_date_range_expression("last 7 days")
Defensive patterns

Strategy: validation

Validate before calling

const SUPPORTED = /^(今天|昨天|本周|上周|本月|上月|最近\d+天|today|yesterday|this week|last week|this month|last month|(?:last|past)\s+\d+\s+days?)$/i;
if (!SUPPORTED.test(expr.trim())) {
  expr = 'last 7 days'; // or convert to explicit {start, end}
}

Type guard

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

Try / catch

try {
  const r = DateParser.resolve_date_range_expression(expr);
} catch (e) {
  if (e.message.includes('无法识别的日期表达式')) {
    // fall back to explicit ISO range computed client-side
    range = { start: isoDaysAgo(7), end: isoToday() };
  } else throw e;
}

Prevention

When it happens

Trigger: Calling a tool that accepts a date_range expression (e.g. resolve_date_range_expression('past week'), '前天', 'last 3 weeks', '本周一') with any phrase outside the hardcoded pattern set. Typos, unsupported relative forms, or non-CN/EN languages (e.g. Japanese, abbreviations like 'yday') all fall through to this raise.

Common situations: LLM-driven MCP clients freely generating date phrases; users assuming arbitrary natural language is supported; mixed-language input like 'this 本周'; singular/plural or article variants ('in the last 7 days') that the regex does not tolerate.

Related errors


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