-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix: strip XML tags from kimi-k2-thinking model reasoning output #9173
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
base: main
Are you sure you want to change the base?
Conversation
- Added stripXmlTags method to remove XML tags from reasoning content - Applied XML tag stripping specifically for moonshot/kimi-k2-thinking model - Added tests to verify XML tags are stripped for kimi model but preserved for others - Fixes #9172 where XML tags were displayed in thinking prompts
Review completed. Found 1 issue that should be addressed:
Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues. |
| */ | ||
| private stripXmlTags(text: string): string { | ||
| // Remove XML tags but preserve the content between them | ||
| return text.replace(/<[^>]*>/g, "") |
Check failure
Code scanning / CodeQL
Incomplete multi-character sanitization High
<script
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 5 days ago
To effectively fix this issue, we must ensure the removal of all potentially dangerous HTML/XML tags—even if the data may contain multiple consecutive or malformed tags. The most robust approach is to use a vetted HTML sanitizer (like sanitize-html) to prevent corner-case vulnerabilities. However, if adding a dependency is undesirable due to scope, we should at least repeatedly apply the sanitization logic until no changes occur, eliminating all instances of tags in line with the presented best practices (looping until stable).
The change applies to the function stripXmlTags in src/api/providers/openrouter.ts.
Edit lines 105–108 so that the regexp-based tag removal is performed in a loop until the string is stable. If possible, use a built-in JavaScript method—do not attempt to write your own custom sanitizer.
No additional imports are needed for this looped regex (unless the project wants to use sanitize-html, but the fixes will show both). We'll show the preferred loop-based fix, matching the project's approach and keeping dependencies stable.
-
Copy modified lines R107-R112
| @@ -104,7 +104,12 @@ | ||
| */ | ||
| private stripXmlTags(text: string): string { | ||
| // Remove XML tags but preserve the content between them | ||
| return text.replace(/<[^>]*>/g, "") | ||
| let previous; | ||
| do { | ||
| previous = text; | ||
| text = text.replace(/<[^>]*>/g, ""); | ||
| } while (text !== previous); | ||
| return text; | ||
| } | ||
|
|
||
| override async *createMessage( |
| */ | ||
| private stripXmlTags(text: string): string { | ||
| // Remove XML tags but preserve the content between them | ||
| return text.replace(/<[^>]*>/g, "") |
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.
The regex /<[^>]*>/g will strip all angle bracket pairs, which could unintentionally remove legitimate content in reasoning output. For example, mathematical comparisons like a < b, generic type syntax like Array<string>, or arrow operators like -> could be partially or fully removed if they appear in reasoning content. Consider using a more specific pattern that targets only complete XML tags (e.g., /< *\/?[a-zA-Z][^>]*>/g) or document which specific tags need to be removed and target those explicitly.
Fix it with Roo Code or mention @roomote and request a fix.
This PR attempts to address Issue #9172. Feedback and guidance are welcome.
Problem
When using the kimi-k2-thinking model via OpenRouter with "show thinking prompts" enabled, XML tags were being displayed in the reasoning/thinking output, making it difficult to read.
Solution
stripXmlTagsmethod to the OpenRouterHandler class that removes XML tags while preserving the content between themmoonshot/kimi-k2-thinkingmodel in the reasoning output streamTesting
Changes
src/api/providers/openrouter.tsto add XML tag stripping logicsrc/api/providers/__tests__/openrouter.spec.tsFixes #9172
Important
Adds XML tag stripping to
moonshot/kimi-k2-thinkingmodel reasoning output inOpenRouterHandler.stripXmlTagsmethod toOpenRouterHandlerto remove XML tags from reasoning output formoonshot/kimi-k2-thinkingmodel.moonshot/kimi-k2-thinkingmodel; other models remain unchanged.openrouter.spec.tsto verify XML tags are stripped formoonshot/kimi-k2-thinkingand preserved for other models.openrouter.tsto include XML tag stripping logic increateMessage()function.This description was created by
for bfabe5d. You can customize this summary. It will automatically update as commits are pushed.