-
Notifications
You must be signed in to change notification settings - Fork 52.4k
fix: escape triple-backtick inside reasoning before wrapping in outer code block #48477
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
Closed
Skywind5487
wants to merge
1
commit into
NousResearch:main
from
Skywind5487:fix/escape-reasoning-fences
Closed
Changes from all commits
Commits
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
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 |
|---|---|---|
|
|
@@ -47,6 +47,24 @@ | |
| _COMMENTARY = object() | ||
|
|
||
|
|
||
| def escape_code_fences_for_display(text: str) -> str: | ||
| """Escape triple-backtick markers so text can be safely wrapped | ||
| inside an outer ``` code block without breaking the fence. | ||
|
|
||
| When reasoning content contains ``` (e.g. the model quotes code | ||
| in its thinking), wrapping it in an outer ``` for display causes | ||
| the inner fence to break the outer block. Solution: replace each | ||
| `` ``` `` with `` \\`\\`\\` `` before wrapping. | ||
|
|
||
| Returns: | ||
| The input text with each `` ``` `` replaced by `` \\`\\`\\` ``, | ||
| or the input unchanged if no triple-backticks are present. | ||
| """ | ||
| if not isinstance(text, str) or "```" not in text: | ||
| return text | ||
| return text.replace("```", "\\`\\`\\`") | ||
|
Comment on lines
+50
to
+65
Collaborator
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. The function is annotated |
||
|
|
||
|
|
||
| @dataclass | ||
| class StreamConsumerConfig: | ||
| """Runtime config for a single stream consumer instance.""" | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| """ | ||
| Tests for escape_code_fences_for_display. | ||
|
|
||
| B1: Escape triple-backtick markers inside reasoning text before wrapping | ||
| in an outer ``` fence, so inner ``` doesn't break the outer block. | ||
| """ | ||
|
|
||
| import pytest | ||
| from gateway.stream_consumer import escape_code_fences_for_display | ||
|
|
||
|
|
||
| class TestEscapeCodeFencesForDisplay: | ||
| """escape_code_fences_for_display prevents inner ``` from breaking | ||
| the outer code block used to render reasoning.""" | ||
|
|
||
| def test_no_fence_passthrough(self): | ||
| text = "plain reasoning text" | ||
| assert escape_code_fences_for_display(text) == text | ||
|
|
||
| def test_single_fence_escaped(self): | ||
| text = "model used ```python\nx = 1\n``` in its thinking" | ||
| result = escape_code_fences_for_display(text) | ||
| assert "```" not in result | ||
| assert "\\`\\`\\`" in result | ||
|
|
||
| def test_multiple_fences_all_escaped(self): | ||
| text = "```\nblock1\n``` and ```python\nblock2\n```" | ||
| result = escape_code_fences_for_display(text) | ||
| assert result.count("```") == 0 | ||
| assert result.count("\\`\\`\\`") == 4 | ||
|
|
||
| def test_empty_string(self): | ||
| assert escape_code_fences_for_display("") == "" | ||
|
|
||
| def test_none_returns_none(self): | ||
| assert escape_code_fences_for_display(None) is None | ||
|
|
||
| def test_integration_with_outer_fence(self): | ||
| """Simulates the gateway's reasoning wrapping logic.""" | ||
| raw = "thinking about:\n```python\nprint('hi')\n```\nok" | ||
| escaped = escape_code_fences_for_display(raw) | ||
| wrapped = f"💭 **Reasoning:**\n```\n{escaped}\n```\n\nHere's the answer." | ||
| # The outer ``` should not be broken by inner ``` | ||
| assert wrapped.count("```") == 2 # only outer open + close | ||
| assert "\\`\\`\\`" in wrapped |
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.
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.
Current main now chooses
subtext,blockquote, orcodeafter buildingdisplay_reasoning(gateway/run.py:11807-11818). Apply this transformation only in the fencedcodebranch; otherwise Discord's default subtext and blockquote output gain visible backslashes despite having no enclosing fence.