Knowledge retrieval POST endpoints randomly returning 400 errors
Self Checks
- [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 (我已阅读并同意 Language Policy).
- [X] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
- [X] Please do not modify this template :) and fill in all the required fields.
Dify version
0.13.2
Cloud or Self Hosted
Cloud
Steps to reproduce
- Create a HTTP Request block
- Set it to POST
https://api.dify.ai/v1/datasets/{DATASET_ID}/retrieve - Set headers
Content-Typetoapplication/json - Add the following as a raw body (side note: the docs have conflicting information on what this request body should be):
{
"query": "{{#sys.query#}}",
"retrieval_model": {
"search_method": "hybrid_search",
"reranking_enable": false,
"reranking_mode": null,
"reranking_model": {
"reranking_provider_name": "",
"reranking_model_name": ""
},
"weights": 0.7,
"top_k": 4,
"score_threshold_enabled": false,
"score_threshold": null
}
}
- Open up preview and start chatting. Type "Hello world" followed by shift + return. Hit "return", check workflow process.
✔️ Expected Behavior
200 response
❌ Actual Behavior
Sometimes the request works, sometimes it doesn't. We've tested it using the exact same query string and have seen both 200 and 400 responses.
When it goes wrong, this happens: Inspecting the Workflow process shows that the HTTP request has been parsed as:
{
"request": "POST /v1/datasets/{xxxxxxx}7/retrieve HTTP/1.1\r\nHost: api.dify.ai\r\nContent-Type: application/json\r\nAuthorization: ***************************************\r\nContent-Length: 456\r\n\r\n{\n\t\"query\": \"I bought a pair of shoes 10 days ago and I want to return it.\n\",\n\t\"retrieval_model\": {\n \"search_method\": \"hybrid_search\",\n \"reranking_enable\": false,\n \"reranking_mode\": null,\n \"reranking_model\": {\n \"reranking_provider_name\": \"\",\n \"reranking_model_name\": \"\"\n },\n \"weights\": 0.7,\n \"top_k\": 4,\n \"score_threshold_enabled\": false,\n \"score_threshold\": null\n }\n}"
}
and the response is:
{
"status_code": 400,
"body": "{\"code\": \"bad_request\", \"message\": \"The browser (or proxy) sent a request that this server could not understand.\", \"status\": 400}\n",
"headers": {
"date": "Fri, 13 Dec 2024 12:27:51 GMT",
"content-type": "application/json",
"content-length": "130",
"connection": "keep-alive",
"access-control-allow-origin": "*",
"x-env": "PRODUCTION",
"x-version": "0.13.2",
"cf-cache-status": "DYNAMIC",
"report-to": "{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v4?s=uqH%2Ft4P%2BZyrd1%2Fx2s7x%2Bxkv2ctMJd%2BxLJ856n42Kt82ovRXtH%2BtAxkXMXdGVs0DmzsUNixso1NUksP%2FfkvmDCVnbFYl5HkVR%2Bhb4ED61se%2BTQmzc3nZib0GC9HXZ\"}],\"group\":\"cf-nel\",\"max_age\":604800}",
"nel": "{\"success_fraction\":0,\"report_to\":\"cf-nel\",\"max_age\":604800}",
"strict-transport-security": "max-age=31536000; includeSubDomains",
"x-content-type-options": "nosniff",
"x-frame-options": "DENY",
"server": "cloudflare",
"cf-ray": "8f15f35ba9148bbe-IAD",
"server-timing": "cfL4;desc=\"?proto=TCP&rtt=2081&min_rtt=1357&rtt_var=1026&sent=4&recv=7&lost=0&retrans=0&sent_bytes=2827&recv_bytes=1478&delivery_rate=2134119&cwnd=251&unsent_bytes=0&cid=77161576d4e8b5c1&ts=60&x=0\""
},
"files": []
}
The 400 Bad Request error you're encountering with the Dify knowledge retrieval POST endpoint could be due to several factors. Here are some potential causes and solutions:
-
Request Body Format: Ensure that the JSON request body is correctly formatted and includes all required fields. According to the documentation, the
queryfield and theretrieval_modelobject should be properly structured. Double-check that the JSON is valid and that all necessary fields are included [1]. -
Validation Errors: The Dify API might return a 400 error if there are validation issues with the request. This could include missing required fields or incorrect data types. Make sure that fields like
query,retrieval_model,search_method,weights, andtop_kare correctly specified. -
Rate Limiting: If your requests are being sent in quick succession, you might be hitting a rate limit, which can cause inconsistent responses. The Dify codebase includes rate limiting mechanisms that could affect the consistency of responses if the limit is exceeded [2].
-
Error Codes and Messages: The Dify API provides specific error codes and messages for 400 errors. Check if the response includes any of these codes, as they can give more insight into what might be going wrong. For example, errors like
app_unavailable,not_completion_app, orprovider_not_initializecould indicate specific issues with the app configuration or provider credentials [3]. -
Logging and Debugging: Utilize the logging capabilities in the Dify platform to trace the processing of your requests. This can help identify where the error is occurring in the workflow [4].
By addressing these areas, you may be able to resolve the issue or at least gain more insight into why the 400 errors are occurring.
To continue talking to Dosu, mention @dosu.
Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other
I also meet this issue when i added a http node in my workflow, And the http is for calling the endpoint https://api.dify.ai/v1/datasets/{DATASET_ID}/retrieve.
I checked the Dify's requests in browser's devtool, and find it may be caused by some special characters in the query string, such as the \n.
Details:
First, i did following configuration for the HTTP node:
Then when i input a query which contains a \n, i input this by using Dify's UI and press (Shift + Return)
Hello, who are\nyou?, then the 400 error occurs.
And i found the Dify is sending out the query like this:
Then i try to switch the Body option to JSON
And found it's now successful, and the Dify is sending out the query:
Not sure if this is a bug for raw format of BODY in HTTP Request node on Dify side, Could Dify detect this kind of escape issues and make the error not happen? Thanks a lot.
Hi, @sugoh. I'm Dosu, and I'm helping the Dify team manage their backlog. I'm marking this issue as stale.
Issue Summary:
- Inconsistent 400 errors from knowledge retrieval POST endpoints in Dify version 0.13.2.
- Potential causes included request body format issues and special characters in the query string.
- "Blacktail" suggested switching the request body format to JSON, which resolved the issue.
- You acknowledged the resolution with a thumbs-up reaction.
Next Steps:
- Please confirm if this issue is still relevant to the latest version of Dify. If so, you can keep the discussion open by commenting here.
- If there are no further updates, this issue will be automatically closed in 15 days.
Thank you for your understanding and contribution!