Skip to content

[model-gateway]: Tool parser for glm47#15520

Merged
slin1237 merged 9 commits intosgl-project:mainfrom
UbeCc:feat/gateway_parser_for_glm47
Dec 21, 2025
Merged

[model-gateway]: Tool parser for glm47#15520
slin1237 merged 9 commits intosgl-project:mainfrom
UbeCc:feat/gateway_parser_for_glm47

Conversation

@UbeCc
Copy link
Copy Markdown
Contributor

@UbeCc UbeCc commented Dec 20, 2025

Motivation

This pr implements tool parser for GLM-4.7.

GLM-4.7 removes '\n' in template from GLM-4.6.

before (GLM-4.6)

<tool_call>{name}\n<arg_key>{key}</arg_key>\n<arg_value>{value}</arg_value>\n</tool_call>

after (GLM-4.7)

<tool_call>{name}<arg_key>{key}</arg_key><arg_value>{value}</arg_value></tool_call>

Reasoning parser is the same as glm45

Modifications

Add Glm47MoeParser, inherited from Glm4MoeParser

Accuracy Tests

failure for non-existing mcp server (do not affect correctness)

image

Benchmarking and Profiling

Checklist

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @UbeCc, 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 integrates support for the GLM-4.7 model into the model gateway by introducing a new tool parser. The primary goal is to accurately interpret tool calls from GLM-4.7, which has a slightly modified format compared to its predecessor, ensuring seamless communication and functionality within the system.

Highlights

  • New Tool Parser for GLM-4.7: A dedicated tool parser, Glm47MoeParser, has been implemented to support the GLM-4.7 model's specific tool call format.
  • GLM-4.7 Format Adaptation: The new parser addresses a change in the GLM-4.7 tool call template, which removes newline characters previously present in GLM-4.6, ensuring correct parsing of the updated format.
  • Comprehensive Parsing Logic: The Glm47MoeParser includes robust logic for both complete and incremental parsing of tool calls, handling XML-style tags, key-value arguments, and type conversions for various data types.
  • Unit Test Coverage: Extensive unit tests have been added to validate the functionality of the Glm47MoeParser, covering scenarios like multiple tool calls, streaming, format detection, and Python literal conversions.

🧠 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.

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

  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.

@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Dec 20, 2025
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

This pull request adds a tool parser for GLM-4.7 by creating a new Glm47MoeParser. The implementation is largely a copy of the existing Glm4MoeParser with a modified regex to support the new tool call format. While the approach is functional and the accompanying tests are comprehensive, it introduces significant code duplication, which could pose maintenance challenges in the future. I've provided a suggestion to refactor this into a more generic parser. Additionally, I've identified a couple of critical issues in the new parser's implementation that would likely lead to compilation errors and should be addressed.

) -> ParserResult<StreamingParseResult> {
// Python logic: Wait for complete tool call, then parse it all at once
self.buffer.push_str(chunk);
let current_text = &self.buffer.clone();
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

The expression &self.buffer.clone() creates a reference to a temporary value. While this may compile in some cases due to temporary lifetime extension, it is an anti-pattern and can be confusing or lead to bugs. It's much clearer and safer to bind the cloned String to a variable first and then take references to it.

Additionally, cloning the entire buffer on each call to parse_incremental can be inefficient, especially for large inputs. If possible, consider a refactor that would allow you to modify the buffer in-place (e.g., using drain()) to improve performance.

Suggested change
let current_text = &self.buffer.clone();
let current_text = self.buffer.clone();

Comment on lines +258 to +263
helpers::reset_current_tool_state(
&mut self.buffer,
&mut false, // glm47_moe doesn't track name_sent per tool
&mut self.streamed_args_for_tool,
&self.prev_tool_call_arr,
);
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

Passing &mut false to helpers::reset_current_tool_state will cause a compilation error, as it's not possible to create a mutable reference to a literal false.

To fix this, you should declare a local mutable variable and pass a reference to it instead. This issue also suggests a potential design flaw, as the helper function seems to have different requirements than what this parser provides (i.e., this parser doesn't use current_tool_name_sent).

Suggested change
helpers::reset_current_tool_state(
&mut self.buffer,
&mut false, // glm47_moe doesn't track name_sent per tool
&mut self.streamed_args_for_tool,
&self.prev_tool_call_arr,
);
let mut name_sent_dummy = false;
helpers::reset_current_tool_state(
&mut self.buffer,
&mut name_sent_dummy, // glm47_moe doesn't track name_sent per tool
&mut self.streamed_args_for_tool,
&self.prev_tool_call_arr,
);

/// - XML-style tags for tool calls
/// - Key-value pairs for arguments
/// - Support for multiple sequential tool calls
pub struct Glm47MoeParser {
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.

high

This Glm47MoeParser is almost an exact copy of Glm4MoeParser, with the only significant difference being the func_detail_extractor regex. This approach introduces a large amount of duplicated code, which will make maintenance more difficult and error-prone.

I strongly recommend refactoring to avoid this duplication. A better approach would be to create a single, generic GlmParser struct that can be initialized with the specific regex pattern required for a given GLM version. This would centralize the parsing logic and make it easier to support future versions.

For example, you could have a generic parser and instantiate it in factory.rs like so:

// In factory.rs
registry.register_parser("glm4_moe", || Box::new(GlmParser::new(r"(?s)<tool_call>([^
]*)
(.*)</tool_call>")));
registry.register_parser("glm47_moe", || Box::new(GlmParser::new(r"(?s)<tool_call>\s*([^<\s]+)\s*(.*?)</tool_call>")));

This would eliminate nearly 300 lines of duplicated code.

@UbeCc
Copy link
Copy Markdown
Contributor Author

UbeCc commented Dec 20, 2025

Initially, I want to keep the original code format so I just copy the glm4 parser. Now I merged the two parsers and re-run tests for glm4 and glm47 parser

image image

@slin1237
Copy link
Copy Markdown
Collaborator

/gemini review

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

This pull request successfully implements a tool parser for GLM-4.7 by refactoring the existing Glm4MoeParser to support both the old and new tool call formats. The changes are well-structured, with corresponding updates to tests, benchmarks, and documentation. The use of factory methods to configure the parser for different GLM versions is a clean approach.

My review comments focus on improving documentation consistency and API clarity. I've identified a few places in the reasoning_parser's README that are inconsistent with the implementation, and I've suggested a couple of minor API improvements in the tool_parser to enhance safety and reduce potential confusion. Overall, this is a great contribution, and addressing these points will further improve the code's maintainability.

PP --> QWT[Qwen3-Thinking]
PP --> KM[Kimi]
PP --> GL[GLM45]
PP --> GL47[GLM47]
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.

medium

The Mermaid diagrams on lines 59 and 69 are inconsistent with the implementation, as they depict GLM47 as a separate parser. Since glm47 uses the glm45 parser, it would be clearer to either remove the GLM47 entries from the diagrams or show it as an alias for GLM45 to avoid confusion.

Comment on lines +330 to +331
- `glm45`: GLM-4.5 / GLM-4.6 parser
- `glm47`: GLM-4.7 parser
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.

medium

This documentation is inconsistent with the implementation in reasoning_parser/factory.rs, which maps the glm47 model pattern to the existing glm45 parser. A new glm47 parser is not actually introduced. To avoid confusion, I recommend merging the glm47 information into the glm45 description.

Suggested change
- `glm45`: GLM-4.5 / GLM-4.6 parser
- `glm47`: GLM-4.7 parser
- `glm45`: GLM-4.5 / GLM-4.6 / GLM-4.7 parser

"qwen3" → "qwen3"
"qwen" → "qwen3"
"glm45" → "glm45"
"glm47" → "glm47"
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.

medium

This mapping should point to glm45, as the factory maps the glm47 pattern to the glm45 parser.

Suggested change
"glm47" → "glm47"
"glm47" → "glm45"

/// - `func_detail_pattern`: Regex pattern for extracting function name and arguments
/// - For GLM-4: `r"(?s)<tool_call>([^\n]*)\n(.*)</tool_call>"`
/// - For GLM-4.7: `r"(?s)<tool_call>\s*([^<\s]+)\s*(.*?)</tool_call>"`
pub fn new(func_detail_pattern: &str) -> Self {
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.

medium

This new function is now a generic constructor that takes a regex pattern. To create a safer and cleaner public API, consider making this function private to the crate (pub(crate) fn new(...)) and only exposing the specific glm4() and glm47() factory methods. This would prevent users from providing invalid regex patterns that could cause a panic at runtime due to the .expect() call.

Suggested change
pub fn new(func_detail_pattern: &str) -> Self {
pub(crate) fn new(func_detail_pattern: &str) -> Self {

pub use pythonic::PythonicParser;
pub use qwen::QwenParser;
pub use step3::Step3Parser;
pub type Glm47MoeParser = Glm4MoeParser;
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.

medium

This type alias can be confusing. It suggests that Glm47MoeParser is a distinct type, but it's just an alias for Glm4MoeParser. This allows for confusing calls like Glm47MoeParser::glm4().

To improve clarity, I'd recommend removing this alias and using Glm4MoeParser directly in all places, including the new test file tests/tool_parser_glm47_moe.rs. The parser's behavior is determined by the factory method used (glm4() vs. glm47()), and relying on that is clearer than using a type alias.

@slin1237 slin1237 merged commit 26704c2 into sgl-project:main Dec 21, 2025
61 checks passed
jiaming1130 pushed a commit to zhuyijie88/sglang that referenced this pull request Dec 25, 2025
YChange01 pushed a commit to YChange01/sglang that referenced this pull request Jan 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation model-gateway run-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants