-
Notifications
You must be signed in to change notification settings - Fork 13.4k
chat: Fix streaming parser for granite models #15682
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
Merged
gabe-l-hart
merged 2 commits into
ggml-org:master
from
shun095:fix_granite_streaming_parser
Sep 19, 2025
+114
−13
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2025,15 +2025,28 @@ static common_chat_params common_chat_params_init_granite(const common_chat_temp | |
|
|
||
| static void common_chat_parse_granite(common_chat_msg_parser & builder) { | ||
| // Parse thinking tags | ||
| static const common_regex start_think_regex(regex_escape("<think>")); | ||
| static const common_regex end_think_regex(regex_escape("</think>")); | ||
| // Granite models output partial tokens such as "<" and "<think". | ||
| // By leveraging try_consume_regex()/try_find_regex() throwing | ||
| // common_chat_msg_partial_exception for these partial tokens, | ||
| // processing is interrupted and the tokens are not passed to add_content(). | ||
| if (auto res = builder.try_consume_regex(start_think_regex)) { | ||
| // Restore position for try_parse_reasoning() | ||
| builder.move_to(res->groups[0].begin); | ||
| builder.try_find_regex(end_think_regex, std::string::npos, false); | ||
| // Restore position for try_parse_reasoning() | ||
| builder.move_to(res->groups[0].begin); | ||
| } | ||
| builder.try_parse_reasoning("<think>", "</think>"); | ||
|
|
||
| // Parse response tags using regex | ||
| static const common_regex response_regex("<response>([\\s\\S]*?)</response>"); | ||
| if (auto res = builder.try_find_regex(response_regex)) { | ||
| // Extract the content between the tags (capture group 1) | ||
| auto content = builder.str(res->groups[1]); | ||
| builder.add_content(content); | ||
| builder.move_to(res->groups[0].end); | ||
| // Parse response tags | ||
| static const common_regex start_response_regex(regex_escape("<response>")); | ||
| static const common_regex end_response_regex(regex_escape("</response>")); | ||
| // Granite models output partial tokens such as "<" and "<response". | ||
| // Same hack as reasoning parsing. | ||
| if (builder.try_consume_regex(start_response_regex)) { | ||
| builder.try_find_regex(end_response_regex); | ||
| } | ||
|
|
||
| if (!builder.syntax().parse_tool_calls) { | ||
|
|
@@ -2047,13 +2060,10 @@ static void common_chat_parse_granite(common_chat_msg_parser & builder) { | |
| builder.move_to(res->groups[0].end); | ||
|
|
||
| // Expect JSON array of tool calls | ||
| auto tool_calls_data = builder.consume_json(); | ||
| if (tool_calls_data.json.is_array()) { | ||
| if (!builder.add_tool_calls(tool_calls_data.json)) { | ||
| builder.add_content("<|tool_call|>" + tool_calls_data.json.dump()); | ||
| if (auto tool_call = builder.try_consume_json_with_dumped_args({{{"arguments"}}})) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some of the earlier 3-series granite models have different tool calling behavior. I'm trying to verify whether any of them would return a single tool-call object (versus an array), but we may need to retain the clause that checks |
||
| if (!builder.add_tool_calls(tool_call->value) || tool_call->is_partial) { | ||
| throw common_chat_msg_partial_exception("incomplete tool call"); | ||
| } | ||
| } else { | ||
| builder.add_content("<|tool_call|>" + tool_calls_data.json.dump()); | ||
| } | ||
| } else { | ||
| builder.add_content(builder.consume_rest()); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Please let me know if there are some better way to implement this.
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.
This isn't a corner of the code I'm intimately familiar with, but I see similar usage elsewhere to parse tool calls that may be partial, so I think this looks like the right approach.