mirror of
https://github.com/langgenius/dify-plugin-sdks.git
synced 2026-07-22 02:15:22 -04:00
Is there a way to access session.storage inside a Model Provider? #64
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @AkiraVoid on GitHub (Dec 4, 2025).
I want to persist the API key provided in a provider credential and reuse it in model invocation. But it seems that there's no self.session property inside ModelProvider, is there a way to access the storage or any other solution to persist the API key in provider credential?
@AkiraVoid commented on GitHub (Dec 7, 2025):
Also posted as a discussion in dify's main repo: https://github.com/langgenius/dify/discussions/29249
@Mairuis commented on GitHub (Dec 8, 2025):
Hi @AkiraVoid,
You're correct that
ModelProviderandAIModelclasses do not have access tosession.storage. This is by design - Model providers are intended to be stateless.Why Model Providers don't have session access
Looking at the SDK architecture:
Toolclass hasself.session→ can accesssession.storageEndpointclass hasself.session→ can accesssession.storageModelProviderclass does NOT have session → no storage accessAIModel(LargeLanguageModel, etc.) does NOT have session → no storage accessThis is intentional because model invocations are designed to be stateless and credentials are already passed through the
credentialsparameter in each method call.Your use case
If you want to persist an API key from provider credentials and reuse it in model invocations, you actually already have access to it:
validate_provider_credentials(credentials)- you receive the credentials dictLargeLanguageModel._invoke(model, credentials, ...)- you also receive the credentials dictThe credentials are passed through every invocation, so you don't need to persist them separately. The credentials dict should contain your API key that was configured when the user added the model provider.
If you truly need persistent storage
If your use case requires persistent storage (e.g., caching tokens, storing session state), you would need to:
Could you share more details about why you need to persist the API key separately? There might be a better solution depending on your specific use case.
@AkiraVoid commented on GitHub (Dec 9, 2025):
Thanks, @Mairuis
But actually, the
credentialsdict passed toAIModel._invokedoes not have the API key parameter defined inprovider_credential_schemaofprovider.yaml, this is confirmed by kurokobo in discussion https://github.com/langgenius/dify/discussions/29249.What I need is an ability to access the API key configured in provider level, so the users can configure it once and add multiple models which use that key to send requests (which means the API key input cannot be defined in a
model_credential_schema). Also, as mentioned in that discussion, the API key input defined in themodel_credential_schemais not safe.@AkiraVoid commented on GitHub (Dec 10, 2025):
Hi, @Mairuis
I am wondering if there's any plan for us to achieve this? Or do I need to open another issue in another repository for this problem?