[Question]: local variable 'response' referenced before assignment
Self Checks
- [x] I have searched for existing issues search for existing issues, including closed ones.
- [x] I confirm that I am using English to submit this report (Language Policy).
- [x] Non-english title submitions will be closed directly ( 非英文标题的提交将会被直接关闭 ) (Language Policy).
- [x] Please do not modify this template :) and fill in all the required fields.
Describe your problem
"max_token": 256, "threshold": 0.1, "max_cluster": 64, "random_seed": 0}, "graphrag": {"use_graphrag": true, "entity_types": ["organization", "person", "geo", "event", "category"], "method": "general", "resolution": true, "community": true}}, "img2txt_id": "qwen-vl-plus@Tongyi-Qianwen", "asr_id": "", "llm_id": "Triangle104/pocketthinker-qwq-3b-instruct-q8_0-GGUF/pocketthinker-qwq-3b-instruct-q8_0.gguf@LM-Studio", "update_time": 1743672658211, "task_type": "graphrag"}
2025-04-03 18:26:55.388 | + Exception Group Traceback (most recent call last):
2025-04-03 18:26:55.388 | | File "/ragflow/rag/svr/task_executor.py", line 602, in handle_task
2025-04-03 18:26:55.388 | | await do_handle_task(task)
2025-04-03 18:26:55.388 | | File "/ragflow/rag/svr/task_executor.py", line 529, in do_handle_task
2025-04-03 18:26:55.388 | | await run_graphrag(task, task_language, with_resolution, with_community, chat_model, embedding_model, progress_callback)
2025-04-03 18:26:55.388 | | File "/ragflow/graphrag/general/index.py", line 58, in run_graphrag
2025-04-03 18:26:55.388 | | subgraph = await generate_subgraph(
2025-04-03 18:26:55.388 | | File "/ragflow/graphrag/general/index.py", line 147, in generate_subgraph
2025-04-03 18:26:55.388 | | ents, rels = await ext(doc_id, chunks, callback)
2025-04-03 18:26:55.388 | | File "/ragflow/graphrag/general/extractor.py", line 97, in call
2025-04-03 18:26:55.388 | | async with trio.open_nursery() as nursery:
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/trio/_core/_run.py", line 1058, in aexit
2025-04-03 18:26:55.388 | | raise combined_error_from_nursery
2025-04-03 18:26:55.388 | | exceptiongroup.ExceptionGroup: Exceptions from Trio nursery (8 sub-exceptions)
2025-04-03 18:26:55.388 | +-+---------------- 1 ----------------
2025-04-03 18:26:55.388 | | Traceback (most recent call last):
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 69, in map_httpcore_exceptions
2025-04-03 18:26:55.388 | | yield
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 233, in handle_request
2025-04-03 18:26:55.388 | | resp = self._pool.handle_request(req)
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/httpcore/_sync/connection_pool.py", line 256, in handle_request
2025-04-03 18:26:55.388 | | raise exc from None
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/httpcore/_sync/connection_pool.py", line 236, in handle_request
2025-04-03 18:26:55.388 | | response = connection.handle_request(
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/httpcore/_sync/connection.py", line 103, in handle_request
2025-04-03 18:26:55.388 | | return self._connection.handle_request(request)
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/httpcore/_sync/http11.py", line 136, in handle_request
2025-04-03 18:26:55.388 | | raise exc
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/httpcore/_sync/http11.py", line 106, in handle_request
2025-04-03 18:26:55.388 | | ) = self._receive_response_headers(**kwargs)
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/httpcore/_sync/http11.py", line 177, in _receive_response_headers
2025-04-03 18:26:55.388 | | event = self._receive_event(timeout=timeout)
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/httpcore/_sync/http11.py", line 217, in _receive_event
2025-04-03 18:26:55.388 | | data = self._network_stream.read(
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/httpcore/_backends/sync.py", line 126, in read
2025-04-03 18:26:55.388 | | with map_exceptions(exc_map):
2025-04-03 18:26:55.388 | | File "/usr/lib/python3.10/contextlib.py", line 153, in exit
2025-04-03 18:26:55.388 | | self.gen.throw(typ, value, traceback)
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions
2025-04-03 18:26:55.388 | | raise to_exc(exc) from exc
2025-04-03 18:26:55.388 | | httpcore.ReadTimeout: timed out
2025-04-03 18:26:55.388 | |
2025-04-03 18:26:55.388 | | The above exception was the direct cause of the following exception:
2025-04-03 18:26:55.388 | |
2025-04-03 18:26:55.388 | | Traceback (most recent call last):
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/openai/_base_client.py", line 973, in _request
2025-04-03 18:26:55.388 | | response = self._client.send(
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/httpx/_client.py", line 914, in send
2025-04-03 18:26:55.388 | | response = self._send_handling_auth(
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/httpx/_client.py", line 942, in _send_handling_auth
2025-04-03 18:26:55.388 | | response = self._send_handling_redirects(
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/httpx/_client.py", line 979, in _send_handling_redirects
2025-04-03 18:26:55.388 | | response = self._send_single_request(request)
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/httpx/_client.py", line 1015, in _send_single_request
2025-04-03 18:26:55.388 | | response = transport.handle_request(request)
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 232, in handle_request
2025-04-03 18:26:55.388 | | with map_httpcore_exceptions():
2025-04-03 18:26:55.388 | | File "/usr/lib/python3.10/contextlib.py", line 153, in exit
2025-04-03 18:26:55.388 | | self.gen.throw(typ, value, traceback)
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 86, in map_httpcore_exceptions
2025-04-03 18:26:55.388 | | raise mapped_exc(message) from exc
2025-04-03 18:26:55.388 | | httpx.ReadTimeout: timed out
2025-04-03 18:26:55.388 | |
2025-04-03 18:26:55.388 | | The above exception was the direct cause of the following exception:
2025-04-03 18:26:55.388 | |
2025-04-03 18:26:55.388 | | Traceback (most recent call last):
2025-04-03 18:26:55.388 | | File "/ragflow/rag/llm/chat_model.py", line 101, in chat
2025-04-03 18:26:55.388 | | response = self.client.chat.completions.create(model=self.model_name, messages=history, **gen_conf)
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/openai/_utils/_utils.py", line 274, in wrapper
2025-04-03 18:26:55.388 | | return func(*args, **kwargs)
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/openai/resources/chat/completions.py", line 704, in create
2025-04-03 18:26:55.388 | | return self._post(
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/openai/_base_client.py", line 1260, in post
2025-04-03 18:26:55.388 | | return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/openai/_base_client.py", line 937, in request
2025-04-03 18:26:55.388 | | return self._request(
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/openai/_base_client.py", line 982, in _request
2025-04-03 18:26:55.388 | | return self._retry_request(
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/openai/_base_client.py", line 1075, in _retry_request
2025-04-03 18:26:55.388 | | return self._request(
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/openai/_base_client.py", line 982, in _request
2025-04-03 18:26:55.388 | | return self._retry_request(
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/openai/_base_client.py", line 1075, in _retry_request
2025-04-03 18:26:55.388 | | return self._request(
2025-04-03 18:26:55.388 | | File "/ragflow/.venv/lib/python3.10/site-packages/openai/_base_client.py", line 992, in _request
2025-04-03 18:26:55.388 | | raise APITimeoutError(request=request) from err
2025-04-03 18:26:55.388 | | openai.APITimeoutError: Request timed out.
2025-04-03 18:26:55.388 | |
2025-04-03 18:26:55.388 | | During handling of the above exception, another exception occurred:
2025-04-03 18:26:55.388 | |
2025-04-03 18:26:55.388 | | Traceback (most recent call last):
2025-04-03 18:26:55.388 | | File "/ragflow/graphrag/general/graph_extractor.py", line 111, in _process_single_content
2025-04-03 18:26:55.388 | | response = await trio.to_thread.run_sync(lambda: self._chat(hint_prompt, [{"role": "user", "content": "Output:"}], gen_conf))
2025-04-03 18:26:55.389 | | File "/ragflow/.venv/lib/python3.10/site-packages/trio/_threads.py", line 447, in to_thread_run_sync
2025-04-03 18:26:55.389 | | return msg_from_thread.unwrap()
2025-04-03 18:26:55.389 | | File "/ragflow/.venv/lib/python3.10/site-packages/outcome/_impl.py", line 213, in unwrap
2025-04-03 18:26:55.389 | | raise captured_error
2025-04-03 18:26:55.389 | | File "/ragflow/.venv/lib/python3.10/site-packages/trio/_threads.py", line 373, in do_release_then_return_result
2025-04-03 18:26:55.389 | | return result.unwrap()
2025-04-03 18:26:55.389 | | File "/ragflow/.venv/lib/python3.10/site-packages/outcome/_impl.py", line 213, in unwrap
2025-04-03 18:26:55.389 | | raise captured_error
2025-04-03 18:26:55.389 | | File "/ragflow/.venv/lib/python3.10/site-packages/trio/_threads.py", line 392, in worker_fn
2025-04-03 18:26:55.389 | | ret = context.run(sync_fn, *args)
2025-04-03 18:26:55.389 | | File "/ragflow/graphrag/general/graph_extractor.py", line 111, in
The error "local variable 'response' referenced before assignment" in the chat method of chat_model.py occurs because the response variable is being used in the return statement of the exception handling block without being assigned a value in the case of an exception.
To fix this, ensure that the response variable is initialized before the try block or modify the exception handling to not reference response if it hasn't been successfully assigned. For example, you could initialize response to None at the start of the method or adjust the return statement to handle the case where response is not set.
Here's a possible adjustment:
response = None # Initialize response
try:
response = self.client.chat.completions.create(model=self.model_name, messages=history, **gen_conf)
except Exception as e:
return f"{ERROR_PREFIX}: {error_code} - {str(e)}. response: {response if response else 'No response'}", 0
This ensures that response is always defined, preventing the UnboundLocalError.
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
这个错误是在Chat中出现的,还是在Agent中出现的?
Hi, @superwiboy
Could you kindly tell me how to reproduce it? So that I can address this issue and then try to solve it properly. Thank you!
Same problem in Extract knowledge graph
Using Local LM-Stuidio bge-m3 as text-embbeding model.
Both docx and pdf file returned same error.
Default Extract knowledge graph setting in configuration.
knowledge graph same issue