ZhuLinsen/daily_stock_analysis · warning · ValueError

Date-filtered summary matches too many rows; narrow the anal

Error message

Date-filtered summary matches too many rows; narrow the analysis date range or stock code.

What it means

Same guard as the phase-filtered variant, but for a plain date/code-filtered summary request: repo.count_results over the given filters exceeds MAX_DYNAMIC_SUMMARY_ROWS, so the service refuses to build the dynamic summary rather than aggregate an unbounded set. Raised as ValueError → HTTP 400 invalid_params.

Source

Thrown at src/services/backtest_service.py:600

                    engine_version=engine_version,
                    analysis_date_from=analysis_date_from,
                    analysis_date_to=analysis_date_to,
                )
            ew = int(eval_window_days) if eval_window_days is not None else None
            count = self.repo.count_results(
                code=code,
                eval_window_days=ew,
                engine_version=engine_version,
                analysis_date_from=analysis_date_from,
                analysis_date_to=analysis_date_to,
            )
            if count > self.MAX_DYNAMIC_SUMMARY_ROWS:
                if phase_bucket is not None:
                    raise ValueError(
                        "Phase-filtered summary candidate set matches too many rows; "
                        "narrow the analysis date range, stock code, or evaluation window."
                    )
                raise ValueError("Date-filtered summary matches too many rows; narrow the analysis date range or stock code.")
            if phase_bucket is not None:
                rows_with_context = self.repo.list_results_with_context(
                    code=code,
                    eval_window_days=ew,
                    engine_version=engine_version,
                    analysis_date_from=analysis_date_from,
                    analysis_date_to=analysis_date_to,
                    limit=self.MAX_DYNAMIC_SUMMARY_ROWS + 1,
                )
                if len(rows_with_context) > self.MAX_DYNAMIC_SUMMARY_ROWS:
                    raise ValueError(
                        "Phase-filtered summary matches too many rows; narrow the analysis date range or stock code."
                    )
                filtered_pairs = [
                    (row, snapshot)
                    for row, snapshot in rows_with_context
                    if self._phase_bucket_from_snapshot(snapshot) == phase_bucket
                ]

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Set analysis_date_from/analysis_date_to to a window whose row count is under the cap.
  2. Scope to one stock with code=.
  3. Reduce stored volume (prune or archive old backtest_results) if unbounded summaries are a product requirement.

Example fix

# before
summary = service.get_backtest_summary()

# after
summary = service.get_backtest_summary(code="600519", analysis_date_from="2026-08-01")
Defensive patterns

Strategy: validation

Validate before calling

count = service.repo.count_results(code=code, eval_window_days=eval_window_days, engine_version=engine_version, analysis_date_from=analysis_date_from, analysis_date_to=analysis_date_to)
if count > service.MAX_DYNAMIC_SUMMARY_ROWS:
    analysis_date_from, analysis_date_to = shrink_range(analysis_date_from, analysis_date_to)
summary = service.get_backtest_summary(code=code, analysis_date_from=analysis_date_from, analysis_date_to=analysis_date_to)

Try / catch

try:
    summary = service.get_backtest_summary(code=code, analysis_date_from=dfrom, analysis_date_to=dto)
except ValueError as exc:
    if "too many rows" in str(exc):
        return auto_narrow_and_retry(code, dfrom, dto)  # retry once with halved range
    raise

Prevention

When it happens

Trigger: GET /api/v1/backtest/summary without analysis_phase, with loose or absent date/code filters, once matched rows exceed MAX_DYNAMIC_SUMMARY_ROWS.

Common situations: Long-running deployments accumulating backtest_results; web UI loading the summary tab with default (empty) filters; switching engine versions so the previously-narrowing filter no longer matches.

Related errors


AI-assisted analysis of ZhuLinsen/daily_stock_analysis@5159bd72e8 (2026-08-15). Data as JSON: /api/errors/c7d4f28151171c97. Report an issue: GitHub.