dify icon indicating copy to clipboard operation
dify copied to clipboard

Service API audit: six inconsistencies in error handling and event emission

Open RiskeyL opened this issue 4 months ago • 3 comments

Self Checks

  • [x] I have read the Contributing Guide and Language Policy.
  • [x] This is only for bug report, if you would like to ask a question, please head to Discussions.
  • [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, otherwise it will be closed.
  • [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • [x] Please do not modify this template :) and fill in all the required fields.

Dify version

main (b03385ffb0); the same behavior was verified on 1.15.0 during the docs team's Service API audit

Cloud or Self Hosted

Cloud, Self Hosted (Docker)

Steps to reproduce

While rewriting the Service API reference, the docs team code-verified every endpoint and found six backend inconsistencies. Each item below lists the trigger, the code location, and expected vs. actual. They are independent; happy to split into separate issues if that helps triage.

1. Renaming a missing metadata field returns 400 instead of 404

PATCH /v1/datasets/{dataset_id}/metadata/{metadata_id} with a nonexistent metadata_id.

MetadataService.update_metadata_name raises a bare ValueError("Metadata not found.") (api/services/metadata_service.py), which the Service API renders as 400 invalid_param. The same request with a nonexistent dataset_id correctly returns 404 not_found.

Expected: 404 not_found. Actual: 400 invalid_param with message Metadata not found.

2. Built-in metadata toggle can report success without doing anything

POST /v1/datasets/{dataset_id}/metadata/built-in/{action} (DatasetMetadataBuiltInFieldActionServiceApi.post, api/controllers/service_api/dataset/metadata.py). Two facets:

  • The action parameter is typed Literal["enable", "disable"], but the route accepts any string and the match has no default case, so e.g. POST .../built-in/Enable returns {"result": "success"} having done nothing.
  • enable_built_in_field / disable_built_in_field (api/services/metadata_service.py) wrap their whole body in except Exception: logger.exception(...), so a failed toggle (for example lock contention) also returns success.

Expected: unknown actions rejected with 400; failures surfaced as errors. Actual: 200 {"result": "success"} in both cases.

3. Rate limit surfaces as 500 on completion-messages but 429 on chat-messages

Exhaust the workspace rate limit, then call POST /v1/completion-messages.

In api/controllers/service_api/app/completion.py, only ChatApi.post catches InvokeRateLimitError and maps it to 429; CompletionApi.post has no such handler, so the same condition falls through to 500 internal_server_error.

Expected: 429 rate_limit_error, consistent with chat. Actual: 500 internal_server_error.

4. Oversized upload returns 413 with an empty message

POST /v1/files/upload with a file over the size limit.

api/services/file_service.py:82 raises FileTooLargeError with no arguments, and BaseServiceError.__init__ defaults description="", so the 413 response body carries "message": "" and clients get no size context.

Expected: a message naming the limit (like the audio endpoints produce). Actual: empty message.

5. Citations & Attributions toggle has no effect for Agent apps

Enable Citations & Attributions in an Agent app's chat features, run a query that retrieves from knowledge.

The toggle persists to the app config, but nothing under api/core/app/apps/agent_app/ emits QueueRetrieverResourcesEvent (only the advanced-chat pipeline and the workflow runner do), so retriever_resources never appears in message_end metadata for Agent apps.

Expected: citations in message_end metadata, as for Chatbot and Chatflow. Actual: the toggle is a no-op.

6. Stream-events path variable is named task_id but holds the workflow run ID

GET /v1/workflow/{task_id}/events (api/controllers/service_api/app/workflow_events.py:60).

The handler treats the value as the workflow run ID (get_workflow_run_by_id_and_tenant_id(run_id=task_id)). The name collides with the real task_id concept used by the stop endpoints, which is a different identifier. Purely a naming issue, but it misleads integrators reading the route; the API docs already describe this parameter as workflow_run_id.

❌ Actual Behavior

See the per-item expected vs. actual above.

RiskeyL avatar Jul 13 '26 08:07 RiskeyL

I'd like to take item 5 as a focused standalone PR: "Citations & Attributions toggle has no effect for Agent apps."

My plan is to trace the agent-app retrieval event path, emit the same retriever-resource information expected by the message-end metadata pipeline, and add regression coverage showing that enabling citations for an Agent app exposes retriever_resources without changing the disabled behavior.

I will keep the other five independent API inconsistencies out of this PR. Please let me know if you prefer item 5 to be split into its own issue first; otherwise I will reference this issue without closing all six items.

githubbzxs avatar Jul 13 '26 13:07 githubbzxs

I'd like to work on item 1 (404 for metadata not found). Could you assign me?

luoshutian3128-cpu avatar Jul 14 '26 08:07 luoshutian3128-cpu

Hi, @RiskeyL. I'm Dosu, and I'm helping the dify team manage their backlog and am marking this issue as stale.

Issue Summary

  • You reported six backend inconsistencies from a Service API audit, with the main themes being incorrect error codes and inconsistent event emission.
  • Two follow-up efforts were volunteered in the thread: item 5 was split into a focused PR for the Citations & Attributions toggle behavior in Agent apps, and item 1 was picked up for the metadata-not-found case that should return 404.
  • The issue is still unresolved overall, but the discussion suggests some parts may already be moving forward in separate workstreams.

Next Steps

  • Please let us know if this issue is still relevant on the latest version of the dify repository.
  • If it is still relevant, you can keep the discussion open by commenting here; otherwise, this issue will be automatically closed in 15 days.

Thanks for your understanding and contribution.

dosubot[bot] avatar Aug 14 '26 16:08 dosubot[bot]