feat(coglet): restore Scope.context for per-prediction context#2853
Merged
markphelps merged 4 commits intomainfrom Mar 20, 2026
Merged
feat(coglet): restore Scope.context for per-prediction context#2853markphelps merged 4 commits intomainfrom
markphelps merged 4 commits intomainfrom
Conversation
Re-implement the context property on coglet's Scope that was lost when the Python server was replaced by the Rust coglet in #2714. The original feature (#2330) allowed predictors to access per-prediction context via current_scope().context. Thread context (HashMap<String, String>) from the HTTP request body through the full stack: PredictionRequest -> service.predict -> SlotRequest::Predict -> worker -> ScopeGuard -> Scope.context getter. Closes #2852
michaeldwan
added a commit
that referenced
this pull request
Apr 7, 2026
Regression from #2853: the context property returns Py<PyDict> which pyo3-stub-gen infers as bare `dict`. Add an override_return_type annotation so the generated stub preserves the intended dict[str, str] contract.
github-merge-queue bot
pushed a commit
that referenced
this pull request
Apr 8, 2026
* ci: add stub freshness check to CI The generate:stubs mise task and stub:check already exist but weren't enforced in CI, so .pyi stubs could drift when Rust source changed. Adds a check-stubs job that runs `mise run --force stub:check` on Rust changes, using --force to bypass sources/outputs mtime freshness (which would skip regeneration in CI's fresh checkout). * chore: regenerate coglet Python stubs Stubs drifted after the BuildInfo.dirty field was added in #2829 and docstring updates in recent PRs. Regenerated with `mise run generate:stubs`. * fix(ci): set LD_LIBRARY_PATH for stub_gen in check-stubs stub_gen is a PyO3 binary that dynamically links libpython. setup-uv installs Python to a non-standard path, so the shared library isn't on LD_LIBRARY_PATH by default. Query sysconfig for the correct LIBDIR. * fix: restore dict[str, str] return type for Scope.context stub Regression from #2853: the context property returns Py<PyDict> which pyo3-stub-gen infers as bare `dict`. Add an override_return_type annotation so the generated stub preserves the intended dict[str, str] contract. * fix(ci): use setup-python for shared libpython in check-stubs stub_gen needs libpython3.x.so at runtime (PyO3 auto-initialize). setup-uv's python-build-standalone is statically linked and doesn't ship the .so, causing 'cannot open shared object file' at runtime.
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.
Summary
current_scope().contexton coglet's RustScopethat was dropped during the coglet rewrite (feature: Coglet (Rust) HTTP Server Only #2714)context: HashMap<String, String>from the HTTP request body through the full stack to the Python-visibleScope.contextgetterhello-contextinreplicate/cog-exampleswhich relies on this APIBackground
The original
Scope.contextwas introduced in #2330 (v0.14.11) to allow per-prediction configuration to be passed alongside user input:When the Python server was replaced by coglet (Rust) in #2714,
python/cog/server/scope.pywas deleted and the new RustScopeonly hadmetricsandrecord_metric()— thecontextfield was not carried over.Changes
metric_scope.rscontext: Py<PyDict>toScope, expose as#[getter], accept inScope::new()andScopeGuard::enter()worker_bridge.rscontextfrompredict()toScopeGuard::enter()protocol.rscontext: HashMap<String, String>toSlotRequest::Predict, updaterehydrate_input()routes.rscontexttoPredictionRequest, thread throughcreate_prediction_with_id()service.rscontextthroughpredict()andbuild_slot_request()worker.rscontexttoPredictHandler::predicttrait andrun_prediction()__init__.pyicontextproperty toScopetype stubTesting
context: {}field in serializedSlotRequestCloses #2852