Skip to content

Fix JSON serialization in API request to avoid Unicode escaping for non-ASCII characters#297

Open
gyl6666 wants to merge 3 commits into
langgenius:mainfrom
gyl6666:patch-1
Open

Fix JSON serialization in API request to avoid Unicode escaping for non-ASCII characters#297
gyl6666 wants to merge 3 commits into
langgenius:mainfrom
gyl6666:patch-1

Conversation

@gyl6666
Copy link
Copy Markdown

@gyl6666 gyl6666 commented Mar 23, 2026

Problem

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.

Solution

  • Modified the JSON serialization logic to set ensure_ascii=False.
  • This ensures non-ASCII characters (like Chinese) are preserved in their original form in the request body.
  • File changed: [dify_plugin/interfaces/model/openai_compatible/llm.py]

Testing

  • Tested with Chinese input payloads.
  • Verified that the outgoing HTTP request body contains raw characters (e.g., "你好") instead of escape sequences (e.g., "\u4f60\u597d").
  • Confirmed token usage is reduced compared to the previous implementation.

Pull Request Checklist

Compatibility Check

  • I have checked whether this change affects the backward compatibility of the plugin declared in README.md
  • I have checked whether this change affects the forward compatibility of the plugin declared in README.md
  • If this change introduces a breaking change, I have discussed it with the project maintainer and specified the release version in the README.md
  • I have described the compatibility impact and the corresponding version number in the PR description
  • I have checked whether the plugin version is updated in the README.md (如果没更新版本号请保持 unchecked)

Available Checks

  • Code has passed local tests
  • Relevant documentation has been updated (if necessary)

…on-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.
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue where non-ASCII characters in API requests to large language models were being unnecessarily escaped, leading to increased token usage and potential comprehension issues. By adjusting the JSON serialization settings, the system now preserves original character encoding, optimizing request payloads and enhancing model performance for multilingual content.

Highlights

  • JSON Serialization Fix: Modified the openai_api_compatible plugin's JSON serialization to set ensure_ascii=False, preventing Unicode escaping for non-ASCII characters like Chinese text.
  • Token Usage Optimization: This change significantly reduces token usage and improves model understanding by sending non-ASCII characters in their original form, as verified with Chinese input payloads.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request aims to fix an issue with JSON serialization where non-ASCII characters were escaped, increasing token usage. The approach of using ensure_ascii=False is correct, but its implementation is flawed. Using the json parameter of requests.post with an already serialized string will lead to double-encoding and a malformed request body. I've provided a critical-severity comment with a suggested fix to use the data parameter instead, which will correctly send the pre-serialized JSON.

endpoint_url,
headers=headers,
json=data,
json=json.dumps(data, ensure_ascii=False),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Using the json parameter of requests.post with an already-serialized JSON string is incorrect. The requests library will serialize the provided string again, resulting in a double-encoded request body. For example, {'key': 'value'} would become '"{\"key\": \"value\"}"' in the request body, which is a JSON string containing another string, not the intended JSON object {"key": "value"}. This will likely cause the server to fail parsing the request.

To send a pre-serialized JSON string, you should use the data parameter and provide it with a bytes-encoded string. The Content-Type header is already correctly set to application/json earlier in the code, so this change will ensure the request is sent correctly.

Suggested change
json=json.dumps(data, ensure_ascii=False),
data=json.dumps(data, ensure_ascii=False).encode('utf-8'),

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>
Copy link
Copy Markdown
Author

@gyl6666 gyl6666 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To send a pre-serialized JSON string, we should use the data parameter and provide it with a bytes-encoded string

send data with byte coded string.
Copy link
Copy Markdown
Author

@gyl6666 gyl6666 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

send data with bytecoded string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant