dify - ✅(Solved) Fix Working outside of application context [1 pull requests, 1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langgenius/dify#35818Fetched 2026-05-06 06:36:21
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
1
Author
Timeline (top)
closed ×1commented ×1cross-referenced ×1labeled ×1

Error Message

Traceback (most recent call last): File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/graphon/graph/graph.py", line 155, in _create_node_instances node_instance = node_factory.create_node(node_config) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fatelei/dify/dify/api/core/workflow/node_factory.py", line 448, in create_node node_init_kwargs = node_init_kwargs_factories.get(node_type, lambda: {})() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fatelei/dify/dify/api/core/workflow/node_factory.py", line 403, in <lambda> BuiltinNodeTypes.LLM: lambda: self._build_llm_compatible_node_init_kwargs( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fatelei/dify/dify/api/core/workflow/node_factory.py", line 484, in _build_llm_compatible_node_init_kwargs model_instance = self._build_model_instance_for_llm_node(validated_node_data) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fatelei/dify/dify/api/core/workflow/node_factory.py", line 512, in _build_model_instance_for_llm_node model_instance, _ = fetch_model_config( ^^^^^^^^^^^^^^^^^^^ File "/Users/fatelei/dify/dify/api/core/app/llm/model_access.py", line 141, in fetch_model_config credentials = credentials_provider.fetch(node_data_model.provider, node_data_model.name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fatelei/dify/dify/api/core/app/llm/model_access.py", line 53, in fetch provider_configurations = self.provider_manager.get_configurations(self.tenant_id) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fatelei/dify/dify/api/core/provider_manager.py", line 225, in get_configurations custom_configuration = self._to_custom_configuration( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fatelei/dify/dify/api/core/provider_manager.py", line 717, in _to_custom_configuration custom_provider_configuration = self._get_custom_provider_configuration( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fatelei/dify/dify/api/core/provider_manager.py", line 762, in _get_custom_provider_configuration encrypted_config=custom_provider_record.encrypted_config, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fatelei/dify/dify/api/models/provider.py", line 95, in encrypted_config credential = self.credential ^^^^^^^^^^^^^^^ File "/Users/fatelei/.local/share/uv/python/cpython-3.12.12-macos-aarch64-none/lib/python3.12/functools.py", line 998, in get val = self.func(instance) ^^^^^^^^^^^^^^^^^^^ File "/Users/fatelei/dify/dify/api/models/provider.py", line 86, in credential return db.session.scalar(select(ProviderCredential).where(ProviderCredential.id == self.credential_id)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/scoping.py", line 1826, in scalar return self._proxied.scalar( ^^^^^^^^^^^^^ File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/scoping.py", line 195, in _proxied return self.registry() ^^^^^^^^^^^^^^^ File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/util/_collections.py", line 629, in call key = self.scopefunc() ^^^^^^^^^^^^^^^^ File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/flask_sqlalchemy/session.py", line 111, in _app_ctx_id return id(app_ctx._get_current_object()) # type: ignore[attr-defined] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/werkzeug/local.py", line 519, in _get_current_object raise RuntimeError(unbound_message) from None RuntimeError: Working outside of application context.

Fix Action

Fixed

PR fix notes

PR #35819: fix: fix Working outside of application context

Description (problem / solution / changelog)

[!IMPORTANT]

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

fix #35818

Screenshots

BeforeAfter
......

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and cd web && pnpm exec vp staged (frontend) to appease the lint gods

Changed files

  • api/core/provider_manager.py (modified, +9/-9)
  • api/models/provider.py (modified, +7/-5)
  • api/tests/unit_tests/core/test_provider_manager.py (modified, +6/-10)

Code Example

Traceback (most recent call last):
  File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/graphon/graph/graph.py", line 155, in _create_node_instances
    node_instance = node_factory.create_node(node_config)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/workflow/node_factory.py", line 448, in create_node
    node_init_kwargs = node_init_kwargs_factories.get(node_type, lambda: {})()
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/workflow/node_factory.py", line 403, in <lambda>
    BuiltinNodeTypes.LLM: lambda: self._build_llm_compatible_node_init_kwargs(
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/workflow/node_factory.py", line 484, in _build_llm_compatible_node_init_kwargs
    model_instance = self._build_model_instance_for_llm_node(validated_node_data)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/workflow/node_factory.py", line 512, in _build_model_instance_for_llm_node
    model_instance, _ = fetch_model_config(
                        ^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/app/llm/model_access.py", line 141, in fetch_model_config
    credentials = credentials_provider.fetch(node_data_model.provider, node_data_model.name)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/app/llm/model_access.py", line 53, in fetch
    provider_configurations = self.provider_manager.get_configurations(self.tenant_id)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/provider_manager.py", line 225, in get_configurations
    custom_configuration = self._to_custom_configuration(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/provider_manager.py", line 717, in _to_custom_configuration
    custom_provider_configuration = self._get_custom_provider_configuration(
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/provider_manager.py", line 762, in _get_custom_provider_configuration
    encrypted_config=custom_provider_record.encrypted_config,
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/models/provider.py", line 95, in encrypted_config
    credential = self.credential
                 ^^^^^^^^^^^^^^^
  File "/Users/fatelei/.local/share/uv/python/cpython-3.12.12-macos-aarch64-none/lib/python3.12/functools.py", line 998, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/models/provider.py", line 86, in credential
    return db.session.scalar(select(ProviderCredential).where(ProviderCredential.id == self.credential_id))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/scoping.py", line 1826, in scalar
    return self._proxied.scalar(
           ^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/scoping.py", line 195, in _proxied
    return self.registry()
           ^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/util/_collections.py", line 629, in __call__
    key = self.scopefunc()
          ^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/flask_sqlalchemy/session.py", line 111, in _app_ctx_id
    return id(app_ctx._get_current_object())  # type: ignore[attr-defined]
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/werkzeug/local.py", line 519, in _get_current_object
    raise RuntimeError(unbound_message) from None
RuntimeError: Working outside of application context.
RAW_BUFFERClick to expand / collapse

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.14.0

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

  1. create workflow

test2.yml

  1. run it

✔️ Expected Behavior

no error

❌ Actual Behavior

Traceback (most recent call last):
  File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/graphon/graph/graph.py", line 155, in _create_node_instances
    node_instance = node_factory.create_node(node_config)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/workflow/node_factory.py", line 448, in create_node
    node_init_kwargs = node_init_kwargs_factories.get(node_type, lambda: {})()
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/workflow/node_factory.py", line 403, in <lambda>
    BuiltinNodeTypes.LLM: lambda: self._build_llm_compatible_node_init_kwargs(
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/workflow/node_factory.py", line 484, in _build_llm_compatible_node_init_kwargs
    model_instance = self._build_model_instance_for_llm_node(validated_node_data)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/workflow/node_factory.py", line 512, in _build_model_instance_for_llm_node
    model_instance, _ = fetch_model_config(
                        ^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/app/llm/model_access.py", line 141, in fetch_model_config
    credentials = credentials_provider.fetch(node_data_model.provider, node_data_model.name)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/app/llm/model_access.py", line 53, in fetch
    provider_configurations = self.provider_manager.get_configurations(self.tenant_id)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/provider_manager.py", line 225, in get_configurations
    custom_configuration = self._to_custom_configuration(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/provider_manager.py", line 717, in _to_custom_configuration
    custom_provider_configuration = self._get_custom_provider_configuration(
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/core/provider_manager.py", line 762, in _get_custom_provider_configuration
    encrypted_config=custom_provider_record.encrypted_config,
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/models/provider.py", line 95, in encrypted_config
    credential = self.credential
                 ^^^^^^^^^^^^^^^
  File "/Users/fatelei/.local/share/uv/python/cpython-3.12.12-macos-aarch64-none/lib/python3.12/functools.py", line 998, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/models/provider.py", line 86, in credential
    return db.session.scalar(select(ProviderCredential).where(ProviderCredential.id == self.credential_id))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/scoping.py", line 1826, in scalar
    return self._proxied.scalar(
           ^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/scoping.py", line 195, in _proxied
    return self.registry()
           ^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/sqlalchemy/util/_collections.py", line 629, in __call__
    key = self.scopefunc()
          ^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/flask_sqlalchemy/session.py", line 111, in _app_ctx_id
    return id(app_ctx._get_current_object())  # type: ignore[attr-defined]
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fatelei/dify/dify/api/.venv/lib/python3.12/site-packages/werkzeug/local.py", line 519, in _get_current_object
    raise RuntimeError(unbound_message) from None
RuntimeError: Working outside of application context.

extent analysis

TL;DR

The error "Working outside of application context" suggests that the issue is related to the application context not being properly set when accessing the database.

Guidance

  • The error occurs in the provider.py file when trying to access the credential attribute, which is a database query. This query is being executed outside of the Flask application context.
  • To fix this, ensure that the database query is executed within the application context. This can be done by using the with app.app_context(): block when accessing the database.
  • Review the code to identify where the application context is being set and ensure it is properly propagated to the point where the database query is being executed.
  • Consider using a try-except block to catch the RuntimeError exception and provide a more informative error message.

Example

from flask import current_app

with current_app.app_context():
    # Execute database query here
    credential = db.session.scalar(select(ProviderCredential).where(ProviderCredential.id == self.credential_id))

Notes

The exact fix will depend on the specific code structure and how the application context is being managed. This guidance provides a general direction, but additional investigation may be required to identify the root cause.

Recommendation

Apply workaround: Ensure that all database queries are executed within the application context using the with app.app_context(): block. This will help to prevent the "Working outside of application context" error.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

dify - ✅(Solved) Fix Working outside of application context [1 pull requests, 1 comments, 2 participants]