UnboundLocalError Occurs When Using Knowledge Upload API #14073

Closed
opened 2026-02-21 19:15:26 -05:00 by yindo · 1 comment
Owner

Originally created by @GonzaHM on GitHub (May 21, 2025).

Self Checks

  • 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 (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.3.1

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

  1. Create an empty knowledge base
  2. Upload knowledge using the create_by_file API

✔️ Expected Behavior

The knowledge base should be uploaded successfully without any errors.

Actual Behavior

Actual Behavior:
The knowledge base fails to upload successfully, and the following logs are recorded in the container:

$ sudo docker logs docker-api-1
2025-05-15 08:06:25.204 ERROR [Dummy-338] [app.py:875] - Exception on /v1/datasets/607bab53-b2ce-4a03-b5ea-bb3b8b8986a2/document/create_by_file [POST]
Traceback (most recent call last):
File "/app/api/.venv/lib/python3.12/site-packages/flask/app.py", line 917, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File "/app/api/.venv/lib/python3.12/site-packages/flask/app.py", line 902, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/api/.venv/lib/python3.12/site-packages/flask_restful/__init__.py", line 489, in wrapper
resp = resource(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/api/.venv/lib/python3.12/site-packages/flask/views.py", line 110, in view
return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/api/.venv/lib/python3.12/site-packages/flask_restful/__init__.py", line 604, in dispatch_request
resp = meth(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/app/api/controllers/service_api/wraps.py", line 228, in decorated
return view(api_token.tenant_id, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/api/controllers/service_api/wraps.py", line 137, in decorated
return view(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/app/api/controllers/service_api/wraps.py", line 137, in decorated
return view(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/app/api/controllers/service_api/dataset/document.py", line 210, in post
documents, batch = DocumentService.save_document_with_dataset_id(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/api/services/dataset_service.py", line 954, in save_document_with_dataset_id
if not dataset.retrieval_model:
^^^^^^^^^^^^^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'default_retrieval_model' where it is not associated with a value
Originally created by @GonzaHM on GitHub (May 21, 2025). ### Self Checks - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [x] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [x] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version 1.3.1 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce 1. Create an empty knowledge base 2. Upload knowledge using the create_by_file API ### ✔️ Expected Behavior The knowledge base should be uploaded successfully without any errors. ### ❌ Actual Behavior Actual Behavior: The knowledge base fails to upload successfully, and the following logs are recorded in the container: ``` $ sudo docker logs docker-api-1 2025-05-15 08:06:25.204 ERROR [Dummy-338] [app.py:875] - Exception on /v1/datasets/607bab53-b2ce-4a03-b5ea-bb3b8b8986a2/document/create_by_file [POST] Traceback (most recent call last): File "/app/api/.venv/lib/python3.12/site-packages/flask/app.py", line 917, in full_dispatch_request rv = self.dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/flask/app.py", line 902, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/flask_restful/__init__.py", line 489, in wrapper resp = resource(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/flask/views.py", line 110, in view return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/flask_restful/__init__.py", line 604, in dispatch_request resp = meth(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/app/api/controllers/service_api/wraps.py", line 228, in decorated return view(api_token.tenant_id, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/controllers/service_api/wraps.py", line 137, in decorated return view(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/app/api/controllers/service_api/wraps.py", line 137, in decorated return view(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/app/api/controllers/service_api/dataset/document.py", line 210, in post documents, batch = DocumentService.save_document_with_dataset_id( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/services/dataset_service.py", line 954, in save_document_with_dataset_id if not dataset.retrieval_model: ^^^^^^^^^^^^^^^^^^^^^^^ UnboundLocalError: cannot access local variable 'default_retrieval_model' where it is not associated with a value ```
yindo added the 🐞 bug label 2026-02-21 19:15:26 -05:00
yindo closed this issue 2026-02-21 19:15:26 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (May 21, 2025):

Hello @GonzaHM! I'm Dosu, your friendly bot here to help you solve bugs, answer questions, and guide you in becoming a contributor. I'm ready to assist you while you wait for a human maintainer. Let's get started!

The UnboundLocalError related to the default_retrieval_model variable in the save_document_with_dataset_id function indicates that the variable is being referenced before it is properly initialized. This error can occur due to incorrect indentation or conditional logic that prevents the variable from being set.

To resolve this issue, ensure that the default_retrieval_model is defined before it is used. The initialization should be outside any conditional blocks that might skip its assignment. Here's a correct initialization example:

default_retrieval_model = {
    "search_method": RetrievalMethod.SEMANTIC_SEARCH.value,
    "reranking_enable": False,
    "reranking_model": {"reranking_provider_name": "", "reranking_model_name": ""},
    "top_k": 2,
    "score_threshold_enabled": False,
}

if not dataset.retrieval_model:
    dataset.retrieval_model = (
        knowledge_config.retrieval_model.model_dump()
        if knowledge_config.retrieval_model
        else default_retrieval_model
    )  # type: ignore

Ensure that the default_retrieval_model is initialized at the top level of the function or module, and not inside any conditional statements that might prevent its assignment [1].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

@dosubot[bot] commented on GitHub (May 21, 2025): <!-- Greeting --> Hello @GonzaHM! I'm [Dosu](https://dosu.dev), your friendly bot here to help you solve bugs, answer questions, and guide you in becoming a contributor. I'm ready to assist you while you wait for a human maintainer. Let's get started! <!-- Answer --> The `UnboundLocalError` related to the `default_retrieval_model` variable in the `save_document_with_dataset_id` function indicates that the variable is being referenced before it is properly initialized. This error can occur due to incorrect indentation or conditional logic that prevents the variable from being set. To resolve this issue, ensure that the `default_retrieval_model` is defined before it is used. The initialization should be outside any conditional blocks that might skip its assignment. Here's a correct initialization example: ```python default_retrieval_model = { "search_method": RetrievalMethod.SEMANTIC_SEARCH.value, "reranking_enable": False, "reranking_model": {"reranking_provider_name": "", "reranking_model_name": ""}, "top_k": 2, "score_threshold_enabled": False, } if not dataset.retrieval_model: dataset.retrieval_model = ( knowledge_config.retrieval_model.model_dump() if knowledge_config.retrieval_model else default_retrieval_model ) # type: ignore ``` Ensure that the `default_retrieval_model` is initialized at the top level of the function or module, and not inside any conditional statements that might prevent its assignment <sup>[[1]](https://github.com/langgenius/dify/blob/main/api/services/dataset_service.py)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.* --- <sup>Help Dosu learn! Give it feedback: [Great Response](https://app.dosu.dev/response-feedback/dad245f2-023a-41d9-98bf-49e3e54578ec?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/dad245f2-023a-41d9-98bf-49e3e54578ec?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/dad245f2-023a-41d9-98bf-49e3e54578ec?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/dad245f2-023a-41d9-98bf-49e3e54578ec?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/dad245f2-023a-41d9-98bf-49e3e54578ec?feedback_type=hallucination) | [Bug Report](https://app.dosu.dev/response-feedback/dad245f2-023a-41d9-98bf-49e3e54578ec?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/dad245f2-023a-41d9-98bf-49e3e54578ec?feedback_type=other)</sup>
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#14073