Skip to content

fix: validate target columns before inference - #145

Open
sun-9545sunoj wants to merge 1 commit into
sktime:mainfrom
sun-9545sunoj:fix/validate-target-columns
Open

sun-9545sunoj wants to merge 1 commit into
sktime:mainfrom
sun-9545sunoj:fix/validate-target-columns

Conversation

@sun-9545sunoj

@sun-9545sunoj sun-9545sunoj commented Nov 10, 2026 •

Copy link
Copy Markdown

Fixes #144

target was validated for presence only. _require_columns passes as long as every name is a column of past, so a target that repeats a column, or that names the time column, was accepted by PredictRequest, accepted by coerce_request, and then failed inside pandas at past.loc[:, target] in from_request. The caller got HTTP 400 carrying a raw pandas message that never mentioned target:

request target before after
["sales", "sales"] 'DataFrame' object has no attribute 'dtype' target repeats column(s) ['sales'].
["timestamp", "sales"] "['timestamp'] not in index" target includes the time column 'timestamp'.
["timestamp"] "None of [Index(['timestamp'], dtype='object')] are in the [columns]" target includes the time column 'timestamp'.

A duplicated target was the worse of the two: past.loc[:, ["sales", "sales"]] selects a two-column frame, so the dtype AttributeError came from sktime inspecting what it expected to be a Series.

Change

  • _check_target_columns in types/_checks.py rejects a target that contains time, and a target that repeats a column. Each message states the problem, then what to send instead, in the style the rest of the request contract uses:

    target repeats column(s) ['sales'].
    
    Each target column is forecast once, so name it once, e.g. target=['sales'].
    
    target includes the time column 'timestamp'.
    
    time is the index of the series, not a value to forecast. Drop it from
    target, e.g. target=['sales'], or omit target to have it inferred.
    

    The suggested value is derived from what was sent, so target=["sales", "promo", "sales"] suggests target=['sales', 'promo'].

  • The check runs on CoercedPredictRequest._check_frame_columns, so it covers POST /predict and POST /predict/bytes together — the latter decodes straight into the coerced model without a PredictRequest, the same reason the quantile check had to live there in fix: validate quantiles in PredictRequest and CoercedPredictRequest #137.

  • Both shapes are now rejected before any fit/predict, so no inference pass is paid for a request that cannot work.

Column inference is deliberately untouched: it takes the past columns other than time and those of future, which are unique and exclude time by construction, so it can produce neither shape.

Tests

src/tserve/types/tests/test_models.py:

  • four new cases on test_coerced_request_rejects (duplicate, several duplicates, time plus a target, time alone);
  • test_coerced_request_target_errors_name_what_to_send_instead, asserting each message carries the corrected target;
  • test_coerced_request_keeps_distinct_target_columns for the valid multi-target path.

src/tserve/server/tests/test_routes.py:

  • test_predict_rejects_malformed_target_before_predict, which asserts HTTP 400 with the new message and scheduler.run.assert_not_called().
173 passed, 1 skipped

ruff check, ruff format, ty and codespell all pass via pre-commit. Docs updated in client/data.md (column inference) and reference/errors.md (the 400 row).

@geetu040 could you review this when you have a moment? Happy to adjust the wording of either message, or to move the check if you would rather have it somewhere else in the contract.

Copilot AI balanced review requested due to automatic review settings November 10, 2026 14:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@read-the-docs-community

read-the-docs-community Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 tserve | 🛠️ Build #35786028 | 📁 Comparing a8617d2 against latest (8bd2d73)

  🔍 Preview build  

2 files changed
± client/data/index.html
± reference/errors/index.html

`target` was validated for presence only: `_require_columns` passes as
long as every name is a column of `past`. A `target` that repeats a
column, or that names the time column, was carried past the boundary and
failed inside pandas at `past.loc[:, target]` with a message that never
mentioned `target`:

| target | before |
| --- | --- |
| `["sales", "sales"]` | `'DataFrame' object has no attribute 'dtype'` |
| `["timestamp", "sales"]` | `"['timestamp'] not in index"` |
| `["timestamp"]` | `"None of [Index(['timestamp'] ...)] are in the [columns]"` |

`_check_target_columns` now rejects both shapes on
`CoercedPredictRequest`, naming what was sent and what to send instead,
so `POST /predict` and `POST /predict/bytes` behave the same and neither
reaches an estimator. Column inference is unaffected: it takes the past
columns other than `time` and those of `future`, which are unique and
exclude `time` by construction.

Fixes sktime#144
@sun-9545sunoj
sun-9545sunoj force-pushed the fix/validate-target-columns branch from 54bf407 to a8617d2 Compare November 10, 2026 14:08

This branch has not been deployed

No deployments
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.

[BUG] target accepts duplicates and the time column, failing later with raw pandas errors

2 participants