feat: expose current session to model plugins#313
Merged
WH-2099 merged 1 commit intoJul 6, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request implements request-scoped session context propagation for model plugins using Python's contextvars. It introduces a get_current_session utility and updates the PluginExecutor to manage session state across various model invocation types. A critical issue was identified in the invoke_llm implementation where managing the ContextVar outside of the generator's execution flow could lead to context leaks or ValueError exceptions. The reviewer suggests moving the context management logic entirely inside the generator wrapper to ensure proper cleanup.
2bd0f27 to
541f6af
Compare
11 tasks
5 tasks
ba8243c to
64407d2
Compare
64407d2 to
d7cd56d
Compare
WH-2099
approved these changes
Jul 6, 2026
Merged
3 tasks
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.
Fixes #311
Follow-up to langgenius/dify#35772.
Summary
Expose the current SDK
Sessionto model plugins throughget_current_session().This lets provider plugins read
session.app_idfor provider-side request metadata and cost attribution.Current host-side design
The earlier plan changed substantially.
The host side now passes app identity explicitly before the request reaches this SDK:
request_metadatathrough model runtime protocols.request_metadata["app_id"]into plugin model dispatch payloads.Session.That leaves one SDK problem: model plugin implementations do not receive
Session.Tool plugins receive it in their constructor, but model plugin
_invoke()signatures are implemented by existing third-party plugins.Adding a new
_invoke()parameter would break those plugins.AIModel.__init__()is final, so constructor injection is not the right extension point either.Changes
dify_plugin.core.session_context.get_current_session().get_current_sessionfrom the top-leveldify_pluginpackage.Sessionaround model invocation paths inPluginExecutor.Usage
session.app_idisNonefor model calls outside app execution, such as routing, title generation, suggested questions, or other system use.Provider plugins should skip tagging or bucket those calls as internal/system traffic.
Validation
just checkjust test