Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'NoneType' object has no attribute 'timestamp' #5794

Open
5 tasks done
NiuBlibing opened this issue Jul 1, 2024 · 3 comments
Open
5 tasks done

AttributeError: 'NoneType' object has no attribute 'timestamp' #5794

NiuBlibing opened this issue Jul 1, 2024 · 3 comments
Labels
🐞 bug Something isn't working

Comments

@NiuBlibing
Copy link

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).
  • 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.6.11

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

Create a workflow and send request to api.

✔️ Expected Behavior

Success for response.

❌ Actual Behavior

Error with error log:

2024-07-01 02:24:30.292 ERROR [Dummy-36] [app.py:838] - Exception on /v1/workflows/run [POST]
Traceback (most recent call last):
File "/app/api/controllers/service_api/app/workflow.py", line 45, in post
response = AppGenerateService.generate(
File "/app/api/services/app_generate_service.py", line 67, in generate
return WorkflowAppGenerator().generate(
File "/app/api/core/app/apps/workflow/app_generator.py", line 83, in generate
return self._generate(
File "/app/api/core/app/apps/workflow/app_generator.py", line 129, in _generate
response = self._handle_response(
File "/app/api/core/app/apps/workflow/app_generator.py", line 276, in _handle_response
return generate_task_pipeline.process()
File "/app/api/core/app/apps/workflow/generate_task_pipeline.py", line 111, in process
return self._to_blocking_response(generator)
File "/app/api/core/app/apps/workflow/generate_task_pipeline.py", line 119, in _to_blocking_response
for stream_response in generator:
File "/app/api/core/app/apps/workflow/generate_task_pipeline.py", line 197, in _process_stream_response
yield self._workflow_node_finish_to_stream_response(
File "/app/api/core/app/task_pipeline/workflow_cycle_manage.py", line 389, in _workflow_node_finish_to_stream_response
finished_at=int(workflow_node_execution.finished_at.timestamp()),
AttributeError: 'NoneType' object has no attribute 'timestamp'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 880, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 865, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
File "/usr/local/lib/python3.10/site-packages/flask_restful/__init__.py", line 489, in wrapper
resp = resource(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/flask/views.py", line 110, in view
return current_app.ensure_sync(self.dispatch_request)(**kwargs) # type: ignore[no-any-return]
File "/usr/local/lib/python3.10/site-packages/flask_restful/__init__.py", line 604, in dispatch_request
resp = meth(*args, **kwargs)
File "/app/api/controllers/service_api/wraps.py", line 75, in decorated_view
return view_func(*args, **kwargs)
File "/app/api/controllers/service_api/app/workflow.py", line 66, in post
raise InternalServerError()
werkzeug.exceptions.InternalServerError: 500 Internal Server Error: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
@dosubot dosubot bot added the 🐞 bug Something isn't working label Jul 1, 2024
Copy link

dosubot bot commented Jul 1, 2024

The AttributeError you're encountering is due to the finished_at attribute being None when the code attempts to call the timestamp() method on it. This issue can be resolved by ensuring that the finished_at attribute is set correctly when the workflow node execution is completed.

Here are the steps to ensure the finished_at attribute is set:

  1. Check Workflow Node Execution Methods:
    Ensure that the finished_at attribute is set in the _workflow_node_execution_success and _workflow_node_execution_failed methods. These methods should be called when the workflow node execution completes successfully or fails.

    def _workflow_node_execution_success(self, workflow_node_execution: WorkflowNodeExecution,
                                         start_at: float,
                                         inputs: Optional[dict] = None,
                                         process_data: Optional[dict] = None,
                                         outputs: Optional[dict] = None,
                                         execution_metadata: Optional[dict] = None) -> WorkflowNodeExecution:
        # ... other code ...
        workflow_node_execution.finished_at = datetime.now(timezone.utc).replace(tzinfo=None)
        # ... other code ...
    
    def _workflow_node_execution_failed(self, workflow_node_execution: WorkflowNodeExecution,
                                        start_at: float,
                                        error: str,
                                        inputs: Optional[dict] = None,
                                        process_data: Optional[dict] = None,
                                        outputs: Optional[dict] = None,
                                        execution_metadata: Optional[dict] = None) -> WorkflowNodeExecution:
        # ... other code ...
        workflow_node_execution.finished_at = datetime.now(timezone.utc).replace(tzinfo=None)
        # ... other code ...
  2. Verify Method Calls:
    Ensure that these methods are being called correctly when the workflow node execution completes. If the finished_at attribute is still None, you might need to debug the code to check if these methods are being skipped or if there is an issue with the database commit.

  3. Check Workflow Service:
    In the WorkflowService class, ensure that the finished_at attribute is set in the run_draft_workflow_node method.

    def run_draft_workflow_node(self, app_model: App,
                                node_id: str,
                                user_inputs: dict,
                                account: Account) -> WorkflowNodeExecution:
        # ... other code ...
        if node_run_result.status == WorkflowNodeExecutionStatus.SUCCEEDED:
            workflow_node_execution = WorkflowNodeExecution(
                # ... other attributes ...
                finished_at=datetime.now(timezone.utc).replace(tzinfo=None)
            )
        else:
            workflow_node_execution = WorkflowNodeExecution(
                # ... other attributes ...
                finished_at=datetime.now(timezone.utc).replace(tzinfo=None)
            )
        db.session.add(workflow_node_execution)
        db.session.commit()
        return workflow_node_execution
  4. Additional Logging:
    Add additional logging to verify that these methods are being executed and that the finished_at attribute is being set as expected.

By ensuring that the finished_at attribute is set correctly in all relevant parts of your code, you should be able to resolve the AttributeError [1][2][3].

To continue talking to Dosu, mention @dosu.

@NiuBlibing
Copy link
Author

[2024-07-01 10:26:49 +0000] [276] [ERROR] Error handling request
Traceback (most recent call last):
  File "/app/api/.venv/lib/python3.10/site-packages/gunicorn/workers/base_async.py", line 115, in handle_request
    for item in respiter:
  File "/app/api/.venv/lib/python3.10/site-packages/werkzeug/wsgi.py", line 256, in __next__
    return self._next()
  File "/app/api/.venv/lib/python3.10/site-packages/werkzeug/wrappers/response.py", line 32, in _iter_encoded
    for item in iterable:
  File "/app/api/.venv/lib/python3.10/site-packages/flask/helpers.py", line 113, in generator
    yield from gen
  File "/app/api/libs/helper.py", line 162, in generate
    yield from response
  File "/app/api/core/app/apps/base_app_generate_response_converter.py", line 28, in _generate
    for chunk in cls.convert_stream_full_response(response):
  File "/app/api/core/app/apps/workflow/generate_response_converter.py", line 45, in convert_stream_full_response
    for chunk in stream_response:
  File "/app/api/core/app/apps/workflow/generate_task_pipeline.py", line 158, in _to_stream_response
    for stream_response in generator:
  File "/app/api/core/app/apps/workflow/generate_task_pipeline.py", line 186, in _process_stream_response
    workflow_node_execution = self._handle_node_start(event)
  File "/app/api/core/app/task_pipeline/workflow_cycle_manage.py", line 444, in _handle_node_start
    workflow_node_execution = self._init_node_execution_from_workflow_run(
  File "/app/api/core/app/task_pipeline/workflow_cycle_manage.py", line 199, in _init_node_execution_from_workflow_run
    tenant_id=workflow_run.tenant_id,
AttributeError: 'NoneType' object has no attribute 'tenant_id'
[2024-07-01 08:27:40 +0000] [276] [ERROR] Error handling request
Traceback (most recent call last):
File "/app/api/.venv/lib/python3.10/site-packages/gunicorn/workers/base_async.py", line 115, in handle_request
for item in respiter:
File "/app/api/.venv/lib/python3.10/site-packages/werkzeug/wsgi.py", line 256, in __next__
return self._next()
File "/app/api/.venv/lib/python3.10/site-packages/werkzeug/wrappers/response.py", line 32, in _iter_encoded
for item in iterable:
File "/app/api/.venv/lib/python3.10/site-packages/flask/helpers.py", line 113, in generator
yield from gen
File "/app/api/libs/helper.py", line 162, in generate
yield from response
File "/app/api/core/app/apps/base_app_generate_response_converter.py", line 28, in _generate
for chunk in cls.convert_stream_full_response(response):
File "/app/api/core/app/apps/workflow/generate_response_converter.py", line 45, in convert_stream_full_response
for chunk in stream_response:
File "/app/api/core/app/apps/workflow/generate_task_pipeline.py", line 158, in _to_stream_response
for stream_response in generator:
File "/app/api/core/app/apps/workflow/generate_task_pipeline.py", line 201, in _process_stream_response
workflow_node_execution = self._handle_node_finished(event)
File "/app/api/core/app/task_pipeline/workflow_cycle_manage.py", line 516, in _handle_node_finished
usage_dict = outputs.get('usage', {})
AttributeError: 'NoneType' object has no attribute 'get'

Other fileds may also be none, there is something wrong in the function.
@dosu.

Copy link

dosubot bot commented Jul 1, 2024

The errors you're encountering indicate that workflow_run and outputs are None in the workflow_cycle_manage.py file. Here are the specific conditions under which these can be None:

  1. workflow_run can be None:

    • When the workflow_run is queried from the database and no matching record is found:
      workflow_run = db.session.query(WorkflowRun).filter(
          WorkflowRun.id == self._task_state.workflow_run_id).first()
      if not workflow_run:
          return None
  2. outputs can be None:

    • When there is no latest_node_execution_info available in the task state:
      if self._task_state.latest_node_execution_info:
          workflow_node_execution = db.session.query(WorkflowNodeExecution).filter(
              WorkflowNodeExecution.id == self._task_state.latest_node_execution_info.workflow_node_execution_id).first()
          outputs = workflow_node_execution.outputs
      else:
          outputs = None

To address these issues, ensure that these conditions are handled properly in your code. For example, you can add checks to handle None values before accessing their attributes:

if workflow_run is None:
    # Handle the case where workflow_run is None
    return

if outputs is None:
    # Handle the case where outputs is None
    return

Additionally, you might want to review the initialization and management of the workflow_node_execution object to ensure it is correctly set up and not leading to NoneType errors [1].

To continue talking to Dosu, mention @dosu.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant