Skip to content

fix(router): accept per-call hooks in Router.predict_batch - #917

Closed
sathariels wants to merge 2 commits into
NandhaKishorM:mainfrom
sathariels:fix/predict-batch-hooks-909
Closed

sathariels wants to merge 2 commits into
NandhaKishorM:mainfrom
sathariels:fix/predict-batch-hooks-909

Conversation

@sathariels

Copy link
Copy Markdown
Contributor

What

Router.predict_batch accepts hooks= and composes them the same way Router.predict does: installed hooks first, then the per-call list. None and [] add nothing. The list runs once per request, including on_route.

The TypeScript Router.predictBatch / predictMany path had the same gap and now takes the same per-call list. RemoteRouter.predict_batch takes hooks= to keep the shared signature, and refuses a non-None value because a callable can't be sent to laya-serve.

Why

predict and the hooks docs already accept per-call hooks, but predict_batch(..., hooks=[...]) raised TypeError: unexpected keyword argument 'hooks' and only dispatched self.hooks. A hook could not be attached to one batch.

How it was verified

python tests/test_router.py
python scripts/test_suites.py
ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120
python -m compileall -q laya/ tests/
cd laya-ts && npm test -- --run && npx tsc --noEmit

tests/test_router.py: 703 passed. scripts/test_suites.py: all 79 suites passed. laya-ts: 629 tests passed, tsc --noEmit clean.

Checklist

  • Focused on one change (split unrelated work into another PR)
  • Rebased on the latest main
  • Tests pass locally
  • Docs or examples updated when the public API changed

Fixes #909

cursoragent and others added 2 commits October 3, 2026 23:49
Router.predict_batch rejected hooks= even though predict and the docs
accept it. Compose per-call hooks after the installed ones, with the
same None and empty-list handling, and mirror that on the TypeScript
batch path.

Co-authored-by: Nithilan Kumaran <sathariels@users.noreply.github.com>
The strict docs build warns when an Args entry has no type. Give
hooks the HookArg annotation predict's composition already uses, on
route_batch, predict_batch, and RemoteRouter.predict_batch.

Co-authored-by: Nithilan Kumaran <sathariels@users.noreply.github.com>
@NandhaKishorM

Copy link
Copy Markdown
Owner

Closing as a duplicate, and this was the closest call of the batch.

I merged @Swatantra-66's #912 and the deciding factor was API surface, not quality. Router.predict takes hooks, on_predict_start, on_predict_end and hooks_raise; #912 forwards all four to the batch path, yours forwards hooks. The pattern this repository has followed all week is to widen a surface to exactly what the single-request path already forwards (#763, #766, #770, #797, #798, #804 are all that shape), so forwarding three and leaving hooks_raise behind would have been the next pull request.

Your comment about composing before routing is the thing I checked hardest, because if it were a real behavioural difference it would have decided this the other way. I probed both heads with a malformed per-call hook on predict_batch:

#912:  TypeError: hooks entries must implement at least one of on_predict_start, on_pred...
#917:  TypeError: hooks entries must implement at least one of on_predict_start, on_pred...

Identical, and both raise before any checkpoint loads, so the property you were protecting holds on both.

What I would still like from your PR, and it is the best writing in either: the comment explaining compose_hooks against list(self.hooks), and that reading the instance list alone silently dropped every set_default_hooks default from the batched path while keeping them on predict. That is the actual bug behind the symptom and #912's comment does not say it. A docs or comment-only follow-up carrying that paragraph into laya/router.py would be welcome and I would merge it.

#909 is closed by #912 in 0.3.27. Thank you.

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]: Router.predict_batch rejects documented per-call hooks

3 participants