Skip to content

fix: filter empty text blocks before Anthropic API call#1806

Open
ThanhNguyxn wants to merge 1 commit intobytedance:mainfrom
ThanhNguyxn:fix/anthropic-empty-text-blocks
Open

fix: filter empty text blocks before Anthropic API call#1806
ThanhNguyxn wants to merge 1 commit intobytedance:mainfrom
ThanhNguyxn:fix/anthropic-empty-text-blocks

Conversation

@ThanhNguyxn
Copy link

Summary

Resolves #987 - Fixes the "text content blocks must be non-empty" error when using Anthropic API.

Root Cause

When tool calls return null/undefined/empty results, the message conversion creates empty text blocks { type: 'text', text: '' } which violate Anthropic's API validation requirements.

Solution

Added sanitizeEmptyTextBlocks() function in the Anthropic handler that:

  • Filters out text blocks with empty or whitespace-only content
  • Preserves non-text blocks (images, tool_use, tool_result)
  • Adds [No content] placeholder if all content would be removed

The fix is applied at the Anthropic handler level before sending messages to the API, ensuring the issue is handled at the source without modifying upstream message conversion logic.

Changes

  • multimodal/tarko/llm-client/src/handlers/anthropic.ts: Added sanitization function and applied it to messages before API call

Testing

  • Build passes: pnpm build in llm-client package succeeds
  • The fix handles edge cases:
    • Empty text blocks { type: 'text', text: '' } → filtered
    • Whitespace-only blocks { type: 'text', text: ' ' } → filtered
    • Non-text blocks (images, tool_use) → preserved
    • Message with all empty text blocks → gets [No content] placeholder

Resolves bytedance#987 - Anthropic API returns 'text content blocks must be non-empty' error

Root cause: When tool calls return null/undefined/empty results, the message
conversion creates empty text blocks { type: 'text', text: '' } which violate
Anthropic's API validation requirements.

Solution: Add sanitizeEmptyTextBlocks() function that:
- Filters out text blocks with empty or whitespace-only content
- Preserves non-text blocks (images, tool_use, tool_result)
- Adds '[No content]' placeholder if all content would be removed

This fix is applied at the Anthropic handler level before sending messages
to the API, ensuring the issue is handled at the source without modifying
upstream message conversion logic.
@netlify
Copy link

netlify bot commented Feb 5, 2026

Deploy Preview for tarko ready!

Name Link
🔨 Latest commit 4b10b8a
🔍 Latest deploy log https://app.netlify.com/projects/tarko/deploys/69846791c03c0f00087481d4
😎 Deploy Preview https://deploy-preview-1806--tarko.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify
Copy link

netlify bot commented Feb 5, 2026

Deploy Preview for agent-tars-docs ready!

Name Link
🔨 Latest commit 4b10b8a
🔍 Latest deploy log https://app.netlify.com/projects/agent-tars-docs/deploys/698467912c62060008bc574c
😎 Deploy Preview https://deploy-preview-1806--agent-tars-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@CLAassistant
Copy link

CLAassistant commented Feb 5, 2026

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug Report]: text content blocks must be non-empty

2 participants