Skip to content

fix: ensure newlines around think tags for proper markdown rendering#20594

Merged
crazywoola merged 1 commit intolanggenius:mainfrom
xuzijie1995:fix/think-tag-newline-handling
Jun 3, 2025
Merged

fix: ensure newlines around think tags for proper markdown rendering#20594
crazywoola merged 1 commit intolanggenius:mainfrom
xuzijie1995:fix/think-tag-newline-handling

Conversation

@xuzijie1995
Copy link
Copy Markdown
Contributor

@xuzijie1995 xuzijie1995 commented Jun 3, 2025

Fix markdown rendering issues when think content directly connects with markdown content

Fixes #20408

Summary

This PR addresses an issue where markdown content following think tags may not render correctly due to inconsistent newline handling. The problem specifically occurs when LLM responses have different formatting patterns for "thinking" content and regular responses.

Background

Different LLM providers handle the formatting between "thinking" content and regular responses differently:

  • Some models (like DeepSeek-R1) naturally include newlines between think tags and content
  • Others (like Doubao-1.5-thinking-pro) may connect think tags directly with content without newlines

This inconsistency leads to markdown rendering issues, particularly affecting:

  • First-level headings
  • Table formatting
  • List rendering

Solution

The fix improves the preprocessThinkTag function to:

  1. Handle think tags with or without surrounding newlines
  2. Ensure proper spacing between </details> tags and subsequent content
  3. Maintain consistent markdown rendering regardless of LLM response format

Long-term Consideration

While this frontend fix addresses the immediate rendering issues, a more comprehensive solution would involve:

  • Backend standardization of LLM response formatting
  • Consistent handling of think/content separation across different LLM providers
  • API response format specifications for third-party LLM integrations

Test Cases

The changes have been tested with the following prompts:

Test Prompt 1:

Give me a mock with tabular information data in markdown format, requirements:
1. direct output from the mock data, no other irrelevant content
2. the mock data with a first level title
3. no need to ```wrap the data

Test Prompt 2:

Give me a mock with tabular information data in markdown format, requirements:
1. direct output from the mock data, no other irrelevant content
2. the mock data without any level title
3. no need to ```wrap the data
iShot_2025-06-03_18.46.46.mp4

Screenshots

Before After

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • [] I've updated the documentation accordingly.
  • [] I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

- Fix markdown rendering issues when think content directly connects with markdown content
@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. ☕️ typescript labels Jun 3, 2025
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jun 3, 2025
@crazywoola crazywoola merged commit 077d627 into langgenius:main Jun 3, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

markdown first line

2 participants