Skip to content

fix(channels): skip empty text blocks in DingTalk rich text parsing#1554

Merged
xieyxclack merged 2 commits intoagentscope-ai:mainfrom
mvanhorn:osc/1303-dingtalk-rich-text-empty-blocks
Mar 16, 2026
Merged

fix(channels): skip empty text blocks in DingTalk rich text parsing#1554
xieyxclack merged 2 commits intoagentscope-ai:mainfrom
mvanhorn:osc/1303-dingtalk-rich-text-empty-blocks

Conversation

@mvanhorn
Copy link
Contributor

Summary

DingTalk richText messages can contain empty or whitespace-only text items. The current _parse_rich_content method creates TextContent(text="") blocks from these, which causes LLM API errors:

{"type":"error","error":{"type":"invalid_request_error","message":"messages: text content blocks must be non-empty"}}

The error persists because corrupted messages are stored in session history, requiring manual session file deletion to recover.

Changes

  • dingtalk/handler.py: Strip text before appending and skip if empty, matching the guard pattern already used in the Feishu channel (as suggested in #1303 comment)
  • base.py: Change the empty content_parts fallback from text="" to text=" " as a defensive measure against the same error class
  • test_dingtalk_rich_text.py: Add regression tests verifying empty text items are filtered while valid text is preserved

Scope

This fix is intentionally scoped to the DingTalk handler and base fallback. The prior attempt (#1331) moved filtering into the shared build_agent_request_from_user_content, which was broader than needed. This PR keeps the change minimal and channel-specific.

Fixes #1303

This contribution was developed with AI assistance (Claude Code).

When DingTalk sends richText messages, empty or whitespace-only text
items created empty TextContent blocks that caused LLM API errors
("text content blocks must be non-empty"). This filters empty text
in _parse_rich_content, matching the approach used in the Feishu
channel. Also changes the base channel empty-content fallback from
an empty string to a single space to prevent the same error class.

Fixes agentscope-ai#1303

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions github-actions bot added the first-time-contributor PR created by a first time contributor label Mar 16, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 addresses an issue where DingTalk rich text messages containing empty or whitespace-only text items led to LLM API errors and corrupted session history. The changes prevent these invalid text blocks from being processed by stripping text content and skipping empty results in the DingTalk handler, and by providing a more robust fallback for empty content parts in the base channel utility. This ensures message integrity and prevents API failures.

Highlights

  • DingTalk Rich Text Parsing Fix: Implemented a fix in the DingTalk channel handler to prevent empty or whitespace-only text blocks from being processed, resolving LLM API errors caused by invalid content.
  • Defensive Fallback for Empty Content: Updated the base channel utility to use a single space as a fallback for empty text content, providing a more robust default and preventing similar API errors in other contexts.
  • New Unit Tests: Added comprehensive unit tests specifically for DingTalk rich text parsing to ensure that empty and whitespace-only text items are correctly filtered out while valid text is preserved.

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

Changelog
  • src/copaw/app/channels/base.py
    • Changed the default TextContent for empty content_parts from an empty string to a single space to prevent LLM API errors.
  • src/copaw/app/channels/dingtalk/handler.py
    • Modified the _parse_rich_content method to strip whitespace from text items and only append TextContent if the stripped text is not empty.
  • tests/unit/channels/init.py
    • Added an empty init.py file to the tests/unit/channels directory.
  • tests/unit/channels/test_dingtalk_rich_text.py
    • Added new test cases to verify that _parse_rich_content correctly skips empty and whitespace-only text items.
    • Included tests to confirm that valid text items are preserved and correctly stripped.
Activity
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 Generative AI Prohibited Use Policy, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
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 effectively addresses a crash caused by empty text blocks from DingTalk rich text messages. The fix in dingtalk/handler.py to filter out empty or whitespace-only text items is correct and well-implemented. The addition of regression tests in test_dingtalk_rich_text.py ensures this issue won't reappear. The defensive change in base.py to use a single space as a fallback for empty content is a pragmatic solution that avoids API errors while integrating well with the existing debouncing logic. I have one minor suggestion for code simplification.

@xieyxclack
Copy link
Member

xieyxclack commented Mar 16, 2026

Thank you for your contribution! Please remove the test files. And run pre-commit run --all-files to format the code

@xieyxclack xieyxclack temporarily deployed to maintainer-approved March 16, 2026 09:03 — with GitHub Actions Inactive
@xieyxclack xieyxclack merged commit f6aefc6 into agentscope-ai:main Mar 16, 2026
24 checks passed
@github-actions
Copy link

Welcome to CoPaw! 🎉

Thank you @mvanhorn for your first contribution! Your PR has been merged. 🚀

We'd love to give you a shout-out in our release notes! If you're comfortable sharing, please reply to this comment with your social media handles using the format below:

discord: your_discord_handle
x: your_x_handle
xiaohongshu: your_xiaohongshu_id

Note: Only share what you're comfortable with — all fields are optional.

Thanks again for helping make CoPaw better!

@mvanhorn
Copy link
Contributor Author

github: @mvanhorn
twitter/x: @mvanhorn
discord: mvanhorn

hh0592821 pushed a commit to hh0592821/CoPaw that referenced this pull request Mar 19, 2026
…gentscope-ai#1554)

Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Co-authored-by: xieyxclack <yuexiang.xyx@alibaba-inc.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

first-time-contributor PR created by a first time contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: DingTalk Channel cannot handle rich text content

3 participants