assafelovic / gpt-researcher

GPT based autonomous agent that does online comprehensive research on any given topic
https://gptr.dev
MIT License
12.98k stars 1.61k forks source link

AttributeError: 'list' object has no attribute 'dict' -- NOW WITH FIX #618

Closed Speedway1 closed 1 week ago

Speedway1 commented 1 week ago

On occasion we get this error:

đź“‹Subtopics: [] ERROR: Exception in ASGI application Traceback (most recent call last): File "/data/misc/gpt-researcher_env/lib/python3.11/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 244, in run_asgi result = await self.app(self.scope, self.asgi_receive, self.asgi_send) # type: ignore[func-returns-value] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/data/misc/gpt-researcher_env/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 70, in call return await self.app(scope, receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/data/misc/gpt-researcher_env/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in call await super().call(scope, receive, send) File "/data/misc/gpt-researcher_env/lib/python3.11/site-packages/starlette/applications.py", line 123, in call await self.middleware_stack(scope, receive, send) File "/data/misc/gpt-researcher_env/lib/python3.11/site-packages/starlette/middleware/errors.py", line 151, in call await self.app(scope, receive, send) File "/data/misc/gpt-researcher_env/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 65, in call await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send) File "/data/misc/gpt-researcher_env/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app raise exc File "/data/misc/gpt-researcher_env/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "/data/misc/gpt-researcher_env/lib/python3.11/site-packages/starlette/routing.py", line 756, in call await self.middleware_stack(scope, receive, send) File "/data/misc/gpt-researcher_env/lib/python3.11/site-packages/starlette/routing.py", line 776, in app await route.handle(scope, receive, send) File "/data/misc/gpt-researcher_env/lib/python3.11/site-packages/starlette/routing.py", line 373, in handle await self.app(scope, receive, send) File "/data/misc/gpt-researcher_env/lib/python3.11/site-packages/starlette/routing.py", line 96, in app await wrap_app_handling_exceptions(app, session)(scope, receive, send) File "/data/misc/gpt-researcher_env/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app raise exc File "/data/misc/gpt-researcher_env/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "/data/misc/gpt-researcher_env/lib/python3.11/site-packages/starlette/routing.py", line 94, in app await func(session) File "/data/misc/gpt-researcher_env/lib/python3.11/site-packages/fastapi/routing.py", line 348, in app await dependant.call(**values) File "/data/misc/gpt-researcher/backend/server.py", line 53, in websocket_endpoint report = await manager.start_streaming(task, report_type, report_source, websocket) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/data/misc/gpt-researcher/backend/websocket_manager.py", line 57, in start_streaming report = await run_agent(task, report_type, report_source, websocket) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/data/misc/gpt-researcher/backend/websocket_manager.py", line 75, in run_agent report = await researcher.run() ^^^^^^^^^^^^^^^^^^^^^^ File "/data/misc/gpt-researcher/backend/report_type/detailed_report/detailed_report.py", line 36, in run subtopics = await self._get_all_subtopics() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/data/misc/gpt-researcher/backend/report_type/detailed_report/detailed_report.py", line 62, in _get_all_subtopics return subtopics.dict()["subtopics"] ^^^^^^^^^^^^^^ AttributeError: 'list' object has no attribute 'dict'

Speedway1 commented 1 week ago

The solution is to edit this file: backend/report_type/detailed_report/detailed_report.py

And change this:

    async def _get_all_subtopics(self) -> list:
        subtopics = await self.main_task_assistant.get_subtopics()
        return subtopics.dict()["subtopics"]

To this:

async def _get_all_subtopics(self) -> list:
    subtopics_dict = await self.main_task_assistant.get_subtopics()

    # Check if subtopics_dict is empty or if 'subtopics' key is missing
    if not subtopics_dict or "subtopics" not in subtopics_dict:
        return []  # Return an empty list if no subtopics

    # Extract the nested list of subtopics
    subtopics = subtopics_dict["subtopics"]  # This assumes 'subtopics' is always a list within the dict
    return subtopics

Explanation

The original __get_allsubtopics function assumed the _main_task_assistant.getsubtopics() would always return a dictionary containing a "subtopics" key with a list of subtopics. This led to errors in cases where:

  1. Empty Dictionary: The returned dictionary was empty (no subtopics).
  2. Missing Key: The dictionary didn't contain the expected "subtopics" key.

The fix addresses these scenarios by:

  1. Checking for Empty Dictionary: If the returned dictionary is empty, an empty list is returned to signify no subtopics.
  2. Checking for Missing Key: It verifies the presence of the "subtopics" key before attempting to access it. If missing, an empty list is returned.
  3. Safe Subtopic Extraction: Only when both conditions are met (non-empty dictionary and key presence), the code extracts the nested list of subtopics.
assafelovic commented 1 week ago

Hey @Speedway1 thanks for this love your contributions! Just fixed with a more elegant approach that achieves the same: https://github.com/assafelovic/gpt-researcher/commit/66a39246db6bd007d7350b0efd23caa26a36a860