Skip to content

Support context argument in batch and unbatch methods - #711

Merged
bhimrazy merged 6 commits into
Lightning-AI:mainfrom
discobot:fix/617-batch-unbatch-context
Aug 7, 2026
Merged

Support context argument in batch and unbatch methods#711
bhimrazy merged 6 commits into
Lightning-AI:mainfrom
discobot:fix/617-batch-unbatch-context

Conversation

@discobot

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #617.

LitAPI.batch and LitAPI.unbatch are the only hooks that never receive the per-request context. It is worse than a missing option: declaring batch(self, inputs, context) with max_batch_size > 1 currently fails every batched request with TypeError: 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.unbatch directly instead of through the _inject_context helper that decode_request/predict/encode_response already go through — even though the contexts list is in scope at all four call sites. This PR wraps those call sites with _inject_context(contexts, ...) and documents the optional context argument in the batch/unbatch docstrings. _inject_context only passes context when the method signature declares it, so existing implementations are unaffected.

Added test_inject_context_in_batch_and_unbatch covering both loops: batch stores each request's input in its per-request context and unbatch reads it back, so the test fails with the TypeError above without the fix. tests/unit/test_lit_server.py, tests/unit/test_batch.py and tests/unit/test_loops.py pass locally.

Before submitting
  • Was this discussed/agreed via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure to update the docs?
  • Did you write any new necessary tests?

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 🙃

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
@codecov-commenter

codecov-commenter commented Aug 6, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85%. Comparing base (b44a9a1) to head (6ef474f).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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
bhimrazy merged commit 9dd1ea0 into Lightning-AI:main Aug 7, 2026
35 of 47 checks passed
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.

Option to use the context variable from the batch and unbatch methods.

4 participants