The output results generated by the time tool contain escape characters \n, which leads to failure when writing to the knowledge base.
Self Checks
- [X] This is only for bug report, if you would like to ask a quesion, 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] Pleas do not modify this template :) and fill in all the required fields.
Dify version
0.6.7
Cloud or Self Hosted
Self Hosted (Docker)
Steps to reproduce
Although custom time formats have been added in this version, there is still an error when importing the knowledge base using the API. I think this should be caused by \n.
✔️ Expected Behavior
code 200
❌ Actual Behavior
code 400
The issue you're encountering with the "400 Bad Request" error when writing to the knowledge base via the API is likely due to the handling of newline characters (\n) in your JSON payload. When these characters are not properly escaped, they can disrupt the JSON formatting, leading to errors.
To address this, ensure that any newline characters in the strings of your JSON payload are properly escaped. This is crucial when constructing the prompt field or any other string fields in your JSON data. The json.dumps() method in Python automatically escapes these characters, so verify that the data being serialized does not contain unescaped newline characters or other special characters that could disrupt the JSON structure.
If automatic escaping does not resolve the issue, you may need to manually process the content of each message to ensure that all special characters are properly escaped before they are joined with \n. This step is important to prevent formatting issues that could lead to a "400 Bad Request" error.
Additionally, confirm that the API endpoint and the server are correctly configured to handle JSON payloads with escaped characters. If there are strict validation rules or incorrect handling of JSON escape sequences, it could result in errors.
Make sure also to follow the expected JSON payload structure as required by the API endpoint for writing to the knowledge base. Incorrect payload structure can also lead to errors.
If you continue to face issues, reviewing the server-side configurations and the specific validations that are applied to the API requests might be necessary. Ensure that your request adheres to the expected format and content as outlined in the documentation.
This approach should help you resolve the issue with the "400 Bad Request" error when attempting to write to the knowledge base using the API.
Details
Sources
- api/core/model_runtime/model_providers/openllm/llm/openllm_generate.py
- zh_CN/guides/knowledge-base/maintain-dataset-via-api.md
- api/core/model_runtime/model_providers/openllm/llm/openllm_generate_errors.py
- api/core/prompt/prompt_templates/common_completion.json
- api/core/prompt/prompt_templates/common_chat.json
- en/features/prompt-engineering/prompt-template.md
- api/controllers/service_api/app/error.py
- api/controllers/console/app/error.py
- api/controllers/web/error.py
- api/core/tools/provider/builtin/time/time.yaml
- api/core/tools/provider/builtin/time/tools/current_time.yaml
- api/config.py
- en/features/datasets/maintain-dataset-via-api.md
About Dosu This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
To continue the conversation, mention @dosu.