-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[fix] Handle escaped characters in GLM tool call parser to prevent double serialization #12456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Summary of ChangesHello @soaringk, 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 resolves a critical bug in the tool call parser that previously caused failures when processing model outputs containing literal escaped characters. The fix prevents double JSON serialization of arguments by improving the robustness of both regular expression-based parsing for newlines and the subsequent JSON deserialization process, ensuring accurate and reliable tool call execution. Highlights
Using Gemini Code AssistThe 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
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 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. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request aims to fix a bug in the tool call parser for GLM-4 models, where escaped characters in the model's output caused parsing failures and double serialization. The changes include updating regexes to handle both literal and escaped newlines, and making the JSON deserialization more robust. The approach is sound, and new tests are added to cover the fix. However, I've found a critical issue in the implementation of the JSON parsing logic that needs to be addressed.
Motivation
This PR fixes a bug where the tool call parser fails when the model's output contains literal escaped characters, such as
\n,\".The Problem:
When GLM-4 outputs tool calls, the XML may contain '\n' literal characters and the JSON content within
<arg_value>tags may contain escaped characters like\", causing regex matching and json parsing both failed. For example:The current regex fails to match the literal \n. Besides, even if the regex extracts the content within <arg_value>, it is not valid JSON. Attempting json.loads() on this string fails with JSONDecodeError.
When parsing failed, the argument value was treated as a plain string. This led to incorrect behavior in
BaseFormatDetectorto incorrectly perform another layer of JSON serialization on the arguments. The result is a double-escaped string (e.g.,\\"instead of\"), breaking the tool call functionality.Example of problematic tool output:
[ { "function": { "name": "todo_write", "arguments": "{\"todos\": \"[{\\\"id\\\": \\\"1\\\", \\\"task\\\": \\\"检查后端代码的硬编码问题\\\", \\\"status\\\": \\\"in_progress\\\"}, {\\\"id\\\": \\\"2\\\", \\\"task\\\": \\\"检查前端代码的硬编码问题\\\", \\\"status\\\": \\\"pending\\\"}, {\\\"id\\\": \\\"3\\\", \\\"task\\\": \\\"检查违反单一职责原则的代码\\\", \\\"status\\\": \\\"pending\\\"}, {\\\"id\\\": \\\"4\\\", \\\"4\\\", \\\"task\\\": \\\"制定整改方案\\\", \\\"status\\\": \\\"pending\\\"}, {\\\"id\\\": \\\"5\\\", \\\"5\\\", \\\"task\\\": \\\"实施整改\\\", \\\"status\\\": \\\"pending\\\"}]\"}" }, "index": 0, "id": "call_eb99d2a2c7fd44328776fb9c", "type": "function" } ]more test cases can be found at
test_array_argument_with_escaped_json()Modifications
Robust Regex for Newlines: Updated regular expression used to extract the function name and arguments (
func_name,func_args). It now correctly recognizes both actual newlines (\n) and their literal form (\\n). This ensures the initial parsing succeeds even with escaped newlines.Robust JSON Deserialization: Updated
parse_arguments()inglm4_moe_detector.pyto handle escaped JSON strings using a two-step JSON parsing approach:\n, \t, etc.)
Final fallback: Use ast.literal_eval() for Python literals
Why twp-step deserialization:
decode('unicode_escape'), only unescapes JSON-standard escape sequences, not Python-specific ones (like \x, \0).These changes ensure that tool calls with escaped characters are parsed correctly on the first attempt, avoiding the double-serialization bug.
Accuracy Tests
Benchmarking and Profiling
Checklist