Skip to content

fix: credit pool access outside flask context#36143

Merged
hjlarry merged 3 commits into
langgenius:mainfrom
hjlarry:p421
May 14, 2026
Merged

fix: credit pool access outside flask context#36143
hjlarry merged 3 commits into
langgenius:mainfrom
hjlarry:p421

Conversation

@hjlarry
Copy link
Copy Markdown
Contributor

@hjlarry hjlarry commented May 14, 2026

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 #36132

error logs:

Traceback (most recent call last):\n  File \"/app/api/.venv/lib/python3.12/site-packages/graphon/graph/graph.py\", line 161, in _create_node_instances\n    node_instance = node_factory.create_node(node_config)\n                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/app/api/core/workflow/node_factory.py\", line 443, in create_node\n    node_init_kwargs = node_init_kwargs_factories.get(node_type, lambda: {})()\n                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/app/api/core/workflow/node_factory.py\", line 398, in <lambda>\n    BuiltinNodeTypes.LLM: lambda: self._build_llm_compatible_node_init_kwargs(\n                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/app/api/core/workflow/node_factory.py\", line 480, in _build_llm_compatible_node_init_kwargs\n    model_instance = self._build_model_instance_for_llm_node(validated_node_data)\n                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/app/api/core/workflow/node_factory.py\", line 508, in _build_model_instance_for_llm_node\n    model_instance, _ = fetch_model_config(\n                        ^^^^^^^^^^^^^^^^^^^\n  File \"/app/api/core/app/llm/model_access.py\", line 141, in fetch_model_config\n    credentials = credentials_provider.fetch(node_data_model.provider, node_data_model.name)\n                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/app/api/core/app/llm/model_access.py\", line 53, in fetch\n    provider_configurations = self.provider_manager.get_configurations(self.tenant_id)\n                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/app/api/core/provider_manager.py\", line 230, in get_configurations\n    system_configuration = self._to_system_configuration(tenant_id, provider_entity, provider_records)\n                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/app/api/core/provider_manager.py\", line 958, in _to_system_configuration\n    trail_pool = CreditPoolService.get_pool(\n                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/app/api/services/credit_pool_service.py\", line 44, in get_pool\n    with sessionmaker(db.engine, expire_on_commit=False).begin() as session:\n                      ^^^^^^^^^\n  File \"/app/api/.venv/lib/python3.12/site-packages/flask_sqlalchemy/extension.py\", line 709, in engine\n    return self.engines[None]\n           ^^^^^^^^^^^^\n  File \"/app/api/.venv/lib/python3.12/site-packages/flask_sqlalchemy/extension.py\", line 687, in engines\n    app = current_app._get_current_object()  # type: ignore[attr-defined]\n          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/app/api/.venv/lib/python3.12/site-packages/werkzeug/local.py\", line 519, in _get_current_object\n    raise RuntimeError(unbound_message) from None\nRuntimeError: Working outside of application context.\n\nThis typically means that you attempted to use functionality that needed\nthe current application. To solve this, set up an application context\nwith app.app_context(). See the documentation for more information.\n

Screenshots

Before After
... ...

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

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 14, 2026

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-05-14 02:35:53.823781109 +0000
+++ /tmp/pyrefly_pr.txt	2026-05-14 02:35:46.203724358 +0000
@@ -3782,29 +3782,29 @@
 ERROR Argument `dict[str, dict[str, str | dict[str, float | list[str]]]]` is not assignable to parameter `config` with type `AppModelConfigDict` in function `core.app.app_config.easy_ui_based_app.model_config.manager.ModelConfigManager.convert` [bad-argument-type]
   --> tests/unit_tests/core/app/test_easy_ui_model_config_manager.py:49:41
 ERROR Object of class `FromClause` has no attribute `create` [missing-attribute]
-   --> tests/unit_tests/core/app/test_llm_quota.py:136:5
+   --> tests/unit_tests/core/app/test_llm_quota.py:147:5
 ERROR Object of class `FromClause` has no attribute `insert` [missing-attribute]
-   --> tests/unit_tests/core/app/test_llm_quota.py:139:13
+   --> tests/unit_tests/core/app/test_llm_quota.py:150:13
 ERROR Object of class `FromClause` has no attribute `create` [missing-attribute]
-   --> tests/unit_tests/core/app/test_llm_quota.py:327:5
+   --> tests/unit_tests/core/app/test_llm_quota.py:338:5
 ERROR Object of class `FromClause` has no attribute `insert` [missing-attribute]
-   --> tests/unit_tests/core/app/test_llm_quota.py:330:13
+   --> tests/unit_tests/core/app/test_llm_quota.py:341:13
 ERROR No matching overload found for function `dict.__init__` called with arguments: (Sequence[Row[tuple[str, int | None]]]) [no-matching-overload]
-   --> tests/unit_tests/core/app/test_llm_quota.py:387:32
+   --> tests/unit_tests/core/app/test_llm_quota.py:398:32
 ERROR Object of class `FromClause` has no attribute `update` [missing-attribute]
-   --> tests/unit_tests/core/app/test_llm_quota.py:398:13
+   --> tests/unit_tests/core/app/test_llm_quota.py:409:13
 ERROR Object of class `FromClause` has no attribute `create` [missing-attribute]
-   --> tests/unit_tests/core/app/test_llm_quota.py:438:5
+   --> tests/unit_tests/core/app/test_llm_quota.py:449:5
 ERROR Object of class `FromClause` has no attribute `insert` [missing-attribute]
-   --> tests/unit_tests/core/app/test_llm_quota.py:441:13
+   --> tests/unit_tests/core/app/test_llm_quota.py:452:13
 ERROR Argument `SimpleNamespace` is not assignable to parameter `model_instance` with type `ModelInstance` in function `core.app.llm.quota.ensure_llm_quota_available` [bad-argument-type]
-   --> tests/unit_tests/core/app/test_llm_quota.py:548:51
+   --> tests/unit_tests/core/app/test_llm_quota.py:559:51
 ERROR Argument `SimpleNamespace` is not assignable to parameter `model_instance` with type `ModelInstance` in function `core.app.llm.quota.ensure_llm_quota_available` [bad-argument-type]
-   --> tests/unit_tests/core/app/test_llm_quota.py:569:51
+   --> tests/unit_tests/core/app/test_llm_quota.py:580:51
 ERROR Argument `SimpleNamespace` is not assignable to parameter `model_instance` with type `ModelInstance` in function `core.app.llm.quota.deduct_llm_quota` [bad-argument-type]
-   --> tests/unit_tests/core/app/test_llm_quota.py:588:28
+   --> tests/unit_tests/core/app/test_llm_quota.py:599:28
 ERROR Argument `SimpleNamespace` is not assignable to parameter `model_instance` with type `ModelInstance` in function `core.app.llm.quota.deduct_llm_quota` [bad-argument-type]
-   --> tests/unit_tests/core/app/test_llm_quota.py:615:28
+   --> tests/unit_tests/core/app/test_llm_quota.py:626:28
 ERROR Argument `SimpleNamespace` is not assignable to parameter `file` with type `File` in function `core.app.workflow.file_runtime.DifyWorkflowFileRuntime.resolve_file_url` [bad-argument-type]
   --> tests/unit_tests/core/app/workflow/test_file_runtime.py:60:39
 ERROR Argument `SimpleNamespace` is not assignable to parameter `graph_init_params` with type `GraphInitParams` in function `core.workflow.node_factory.DifyNodeFactory.__init__` [bad-argument-type]
@@ -5620,13 +5620,13 @@
 ERROR Argument `dict[str, str]` is not assignable to parameter `labels` with type `dict[str, AttributeValue]` in function `enterprise.telemetry.exporter.EnterpriseExporter.record_histogram` [bad-argument-type]
    --> tests/unit_tests/enterprise/telemetry/test_exporter.py:615:85
 ERROR Object of class `FromClause` has no attribute `create` [missing-attribute]
-  --> tests/unit_tests/events/test_update_provider_when_message_created.py:16:5
+  --> tests/unit_tests/events/test_update_provider_when_message_created.py:27:5
 ERROR Object of class `FromClause` has no attribute `insert` [missing-attribute]
-  --> tests/unit_tests/events/test_update_provider_when_message_created.py:21:13
+  --> tests/unit_tests/events/test_update_provider_when_message_created.py:32:13
 ERROR Argument `SimpleNamespace` is not assignable to parameter `sender` with type `Message` in function `events.event_handlers.update_provider_when_message_created.handle` [bad-argument-type]
-  --> tests/unit_tests/events/test_update_provider_when_message_created.py:61:20
+  --> tests/unit_tests/events/test_update_provider_when_message_created.py:72:20
 ERROR Argument `SimpleNamespace` is not assignable to parameter `sender` with type `Message` in function `events.event_handlers.update_provider_when_message_created.handle` [bad-argument-type]
-   --> tests/unit_tests/events/test_update_provider_when_message_created.py:103:20
+   --> tests/unit_tests/events/test_update_provider_when_message_created.py:114:20
 ERROR Cannot index into `object` [bad-index]
    --> tests/unit_tests/extensions/otel/test_celery_sqlcommenter.py:136:20
 ERROR Cannot index into `object` [bad-index]
@@ -5957,13 +5957,13 @@
 ERROR Class member `FixedDateTime.now` overrides parent class `datetime` in an inconsistent manner [bad-override]
    --> tests/unit_tests/services/test_clear_free_plan_tenant_expired_logs.py:460:13
 ERROR Object of class `FromClause` has no attribute `create` [missing-attribute]
-  --> tests/unit_tests/services/test_credit_pool_service.py:17:5
+  --> tests/unit_tests/services/test_credit_pool_service.py:19:5
 ERROR Object of class `FromClause` has no attribute `insert` [missing-attribute]
-  --> tests/unit_tests/services/test_credit_pool_service.py:22:13
+  --> tests/unit_tests/services/test_credit_pool_service.py:24:13
 ERROR Object of class `FromClause` has no attribute `create` [missing-attribute]
-  --> tests/unit_tests/services/test_credit_pool_service.py:55:5
+  --> tests/unit_tests/services/test_credit_pool_service.py:75:5
 ERROR Object of class `FromClause` has no attribute `create` [missing-attribute]
-   --> tests/unit_tests/services/test_credit_pool_service.py:107:5
+   --> tests/unit_tests/services/test_credit_pool_service.py:127:5
 ERROR Argument `SimpleNamespace` is not assignable to parameter `account` with type `Account` in function `services.dataset_service.DatasetService.create_empty_dataset` [bad-argument-type]
    --> tests/unit_tests/services/test_dataset_service_dataset.py:180:93
 ERROR Argument `SimpleNamespace` is not assignable to parameter `account` with type `Account` in function `services.dataset_service.DatasetService.create_empty_dataset` [bad-argument-type]

@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 0.00% 43.70% +43.70%
Strict coverage 0.00% 43.22% +43.22%
Typed symbols 0 22,025 +22,025
Untyped symbols 0 28,690 +28,690
Modules 0 2551 +2,551

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label May 14, 2026
@hjlarry hjlarry marked this pull request as ready for review May 14, 2026 02:45
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label May 14, 2026
@hjlarry hjlarry added this pull request to the merge queue May 14, 2026
Merged via the queue into langgenius:main with commit d1d1903 May 14, 2026
27 checks passed
@hjlarry hjlarry deleted the p421 branch May 14, 2026 02:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Iteration block fails with 'Working outside of application context error' when parallel mode is enabled in Dify 1.14.1

2 participants