Support context argument in batch and unbatch methods - #711
Merged
bhimrazy merged 6 commits intoAug 7, 2026
Conversation
The batched loops called lit_api.batch and lit_api.unbatch directly, so user implementations declaring a context parameter failed with a TypeError. Route both calls through _inject_context, the same way decode_request, predict and encode_response are already handled, in both the regular and streaming batched loops. Document the optional context argument in the LitAPI.batch/unbatch docstrings and add regression tests covering both loops. Fixes Lightning-AI#617
discobot
requested review from
Abdul-0x4A,
andyland,
aniketmaurya,
dmitsf,
ethanwharris,
justusschock,
k223kim and
tchaton
as code owners
June 13, 2026 11:15
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #711 +/- ##
===================================
Coverage 85% 85%
===================================
Files 39 39
Lines 3285 3285
===================================
Hits 2781 2781
Misses 504 504 🚀 New features to boost your workflow:
|
The test set max_batch_size=2 but sent a single request, so every batch had size 1 and the ordering between contexts and requests was never checked. Send two concurrent requests with different inputs instead and assert each one gets its own context value back. Each context also records the batch size, so the test fails loudly if the two requests ever stop sharing a batch rather than silently degrading.
Say which signature to write and note that the argument has to be named context, since _inject_context matches on the parameter name and `**kwargs` never receives it. Drops the RST markup for the plain prose and indented examples used elsewhere in the file.
bhimrazy
approved these changes
Aug 6, 2026
dmitsf
approved these changes
Aug 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes #617.
LitAPI.batchandLitAPI.unbatchare the only hooks that never receive the per-requestcontext. It is worse than a missing option: declaringbatch(self, inputs, context)withmax_batch_size > 1currently fails every batched request withTypeError: batch() missing 1 required positional argument: 'context', surfaced as HTTP 500.Root cause: the batched loops (regular and streaming) call
lit_api.batch/lit_api.unbatchdirectly instead of through the_inject_contexthelper thatdecode_request/predict/encode_responsealready go through — even though thecontextslist is in scope at all four call sites. This PR wraps those call sites with_inject_context(contexts, ...)and documents the optionalcontextargument in thebatch/unbatchdocstrings._inject_contextonly passescontextwhen the method signature declares it, so existing implementations are unaffected.Added
test_inject_context_in_batch_and_unbatchcovering both loops:batchstores each request's input in its per-request context andunbatchreads it back, so the test fails with theTypeErrorabove without the fix.tests/unit/test_lit_server.py,tests/unit/test_batch.pyandtests/unit/test_loops.pypass locally.Before submitting
PR review
Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in GitHub issues there's a high chance it will not be merged.
Did you have fun?
Make sure you had fun coding 🙃