From c20b348055ec78575275ad88d8e9a6ecdc166110 Mon Sep 17 00:00:00 2001 From: gyl <270279long@163.com> Date: Mon, 23 Mar 2026 18:00:03 +0800 Subject: [PATCH 1/3] Fix JSON serialization in API request to avoid Unicode escaping for non-ASCII characters When requesting a large language model, the openai_api_compatible plugin serializes and sends the request payload. When the content contains Chinese text, the request body is fully encoded as Unicode escape sequences (e.g., \uXXXX), which significantly increases token usage and may negatively impact the model's understanding. --- python/dify_plugin/interfaces/model/openai_compatible/llm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/dify_plugin/interfaces/model/openai_compatible/llm.py b/python/dify_plugin/interfaces/model/openai_compatible/llm.py index 34135350..83ab1b94 100644 --- a/python/dify_plugin/interfaces/model/openai_compatible/llm.py +++ b/python/dify_plugin/interfaces/model/openai_compatible/llm.py @@ -485,7 +485,7 @@ def _generate( response = requests.post( endpoint_url, headers=headers, - json=data, + json=json.dumps(data, ensure_ascii=False), timeout=(10, _plugin_config.MAX_REQUEST_TIMEOUT), stream=stream, ) From 91bf4b3b09442535075dec9da46c46895f1e94a1 Mon Sep 17 00:00:00 2001 From: gyl <270279long@163.com> Date: Mon, 23 Mar 2026 18:22:33 +0800 Subject: [PATCH 2/3] Apply suggestion from @gemini-code-assist[bot] To send a pre-serialized JSON string, we should use the data parameter and provide it with a bytes-encoded string Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- python/dify_plugin/interfaces/model/openai_compatible/llm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/dify_plugin/interfaces/model/openai_compatible/llm.py b/python/dify_plugin/interfaces/model/openai_compatible/llm.py index 83ab1b94..9561eeca 100644 --- a/python/dify_plugin/interfaces/model/openai_compatible/llm.py +++ b/python/dify_plugin/interfaces/model/openai_compatible/llm.py @@ -485,7 +485,7 @@ def _generate( response = requests.post( endpoint_url, headers=headers, - json=json.dumps(data, ensure_ascii=False), + data=json.dumps(data, ensure_ascii=False).encode('utf-8'), timeout=(10, _plugin_config.MAX_REQUEST_TIMEOUT), stream=stream, ) From 114a396377cca466098d3b534e246273d833a010 Mon Sep 17 00:00:00 2001 From: gyl <270279long@163.com> Date: Mon, 23 Mar 2026 18:35:22 +0800 Subject: [PATCH 3/3] send data with byte coded string. send data with byte coded string. --- python/dify_plugin/interfaces/model/openai_compatible/llm.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/dify_plugin/interfaces/model/openai_compatible/llm.py b/python/dify_plugin/interfaces/model/openai_compatible/llm.py index 9561eeca..4bac745f 100644 --- a/python/dify_plugin/interfaces/model/openai_compatible/llm.py +++ b/python/dify_plugin/interfaces/model/openai_compatible/llm.py @@ -482,6 +482,7 @@ def _generate( if user: data["user"] = user + headers['Content-Type'] = 'application/json; charset=utf-8' response = requests.post( endpoint_url, headers=headers,