Skip to content

feat: log a warning when a prediction ran without its saved cache - #469

Open
safaricd wants to merge 5 commits into
mainfrom
claude/kv-fallback-warning
Open

safaricd wants to merge 5 commits into
mainfrom
claude/kv-fallback-warning

Conversation

@safaricd

@safaricd safaricd commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Changes

  • New constructor parameter cache_fallback_strategy ("error" or "refit", default "error") on TabPFNClassifier and TabPFNRegressor. With "error" a stale cache raises RefitRequiredError, as before, and the request carries nothing new. With "refit" the synchronous predict sends the key, and a server that offers it answers from the general pool at once and rebuilds the cache in the background. The client does not wait for the rebuild.
  • ASYNC mode and submit_predict() never send the key and keep raising RefitRequiredError.
  • The value is checked in _prepare_predict, which now shares _validate_choice with output_type. A class-level default keeps estimators pickled by older versions working. save_model leaves it out, like api_mode.
  • api_models.py gets the alias and the optional field on the three predict request classes, as the server's generated models have them.
  • A prediction logs one warning when the response metadata says cache_outcome == "fallback", once for a prediction split over several requests (estimator.py).
  • The README's KV Cache section and a changelog fragment describe it.

Motivation

After a service upgrade, a fitted model's saved cache can stop working. A server that handles this predicts without the cache from the training data it stores. The answer is correct but slower and billed as a predict without a cache, and a background rebuild keeps the model_id usable. The user should choose that, not get it by default, so the default stays the error the client always raised.

A fallback response says cache_outcome="fallback". The client kept the metadata and never read it, so a user who stored a model_id saw a slow prediction with no reason. The server also reports "fallback" when its cache path did not answer in time, so the text names no cause.

The warning is a log record, not a Python warning. A warning raised as an error (-W error) would lose a prediction the server had already computed and billed. The classifier's predict, predict_proba and _predict keep their shape, so subclasses that override _predict are unaffected.

The server side is PriorLabs/tabpfn-server#1838.

How it was tested

  • pytest -q tests/unit: 898 passed, 1 skipped. The shared venv imports tabpfn_client from the main checkout, so I ran it with PYTHONPATH=<worktree>/src:<worktree> and checked that the module resolves to the worktree.
  • Request bodies are checked through the existing mock-server helpers: the default sends no key, "refit" sends it for the inline and the upload predict, a background job never sends it, and predict_chunks forwards it.
  • An invalid value raises ValueError before any request; clone and get_params keep the value; a pickle from before the parameter loads and predicts with "error"; save_model leaves it out and a model saved with "refit" loads with the default.
  • KV_CACHE_INVALID raises RefitRequiredError when the server does not fall back.
  • A subclass that overrides _predict and returns an array still gets arrays, and a run with warnings.simplefilter("error") still gets its result.
  • ruff check src tests and ruff format --check src tests: clean. basedpyright on the five changed source files with a scratch config: 0 errors.
  • tests/integration/test_server_config.py: 1 passed.
  • Not run: trunk check (it needs a .venv inside the worktree), tests/integration/test_tabpfn_classifier.py (it overwrites the real token cache) and a round trip against a real server.

Open items

  • Rollout order: a server without the new field answers "refit" with a 422 (its request bodies forbid unknown fields), which the client raises as RuntimeError. It does so for any fit mode, because the client sends the key whatever the fit mode is. Deploy tabpfn-server#1838 to prod before releasing this PR. A user on the default sends no key and is not affected.
  • Server flags: the fallback also needs PRIOR_KV_INVALID_CACHE_FALLBACK on, and the rebuild PRIOR_KV_STALE_CACHE_REFIT. Both default to off. With the first off, "refit" still raises RefitRequiredError.
  • Sync only: ASYNC mode and submit_predict() never fall back. That needs a server change first (follow-up).
  • "fallback" also covers a cache path that timed out, so the log says only that the saved cache could not be used.
  • Servers that do not send cache_outcome, or send another value, log nothing.

🤖 Generated with Claude Code

safaricd and others added 3 commits November 25, 2026 12:13
When the server cannot use a fitted model's saved cache, it fits the model again for the prediction and says so in the response metadata (cache_outcome is "fallback"). The client stored the metadata but never read that field, so a user whose saved model got slow saw no reason.

Each estimator's _finish_predict now logs one warning per prediction call when any request of the prediction fell back. It is a log record, not a Python warning: a warning raised as an error would lose a prediction the server already computed and billed. The text names no cause and gives no refit advice, because the server also reports a fallback when its cache path did not answer in time. The classifier's predict, predict_proba and _predict keep their shape, so subclasses that override _predict are unaffected.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The changelog check requires the fragment to carry the PR number, which is only known once the PR exists.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The tests covered the same paths again along helper axes (via, output type, estimator) and kept a local copy of the offline-server helper. They now reuse _offline_server, which takes an optional metadata argument, and _predict from test_submit_predict.py, and keep one case per distinct invariant: it logs once on a fallback, it logs nothing for the other values, a split prediction logs once, a warning filter set to error still returns the result, a subclass that overrides _predict still works, and the metadata models of both tasks carry the field. 55 tests in 259 lines become 27 in 189.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@safaricd
safaricd marked this pull request as ready for review November 25, 2026 12:29
@safaricd
safaricd requested a review from a team as a code owner November 25, 2026 12:29

@eddiebergman-priorlabs eddiebergman-priorlabs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Lovely stuff

safaricd and others added 2 commits November 25, 2026 16:23
A predict on a model whose cache another version wrote either fails or, on a server that offers it, runs without the cache. The client had no way to say which it wants. The new cache_fallback_strategy parameter on both estimators is "error" by default: it raises RefitRequiredError as before and sends nothing new. "refit" sends the key on synchronous predicts, so the server answers at once and rebuilds the cache in the background. ASYNC mode and submit_predict never send it.

The value is checked in _prepare_predict, which now shares _validate_choice with output_type. A class-level default keeps estimators pickled by older versions working.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A record that holds a parameter an older client does not know fails to load there. cache_fallback_strategy says how this process talks to the server, not what the model computes, so it joins the transport params: save_model leaves it out and a loaded model uses the default.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

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.

2 participants