Skip to content

fix: filter out empty text block objects from content blocks#380

Open
manikanta5827 wants to merge 7 commits intostrands-agents:mainfrom
manikanta5827:manikanta-fix_content_blocks_validation_exception
Open

fix: filter out empty text block objects from content blocks#380
manikanta5827 wants to merge 7 commits intostrands-agents:mainfrom
manikanta5827:manikanta-fix_content_blocks_validation_exception

Conversation

@manikanta5827
Copy link

Description

This PR fixes an issue where content blocks were not properly filtering out empty text block objects, leading to potential inconsistencies or unexpected behavior in downstream processing.

The change ensures that any text block objects with empty content are removed before the content block is finalized. This improves data cleanliness and prevents unnecessary empty elements from being processed.

Related Issues

#373

Documentation PR

N/A

Type of Change

Bug fix

Testing

How have you tested the change?

  • I ran npm run check

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Before

Screenshot 2026-01-05 at 9 45 58 PM

After

Screenshot 2026-01-05 at 9 48 14 PM

cagataycali
cagataycali previously approved these changes Jan 11, 2026
Copy link

@cagataycali cagataycali left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ LGTM - Approving this bug fix

Summary

This PR correctly fixes the ValidationException: The text field in the ContentBlock object is blank issue (#373) by filtering out empty TextBlock objects before sending them to the model.

Code Review

The implementation looks solid:

  1. Change from const to let - Necessary to allow reassignment during filtering
  2. Filter logic - Correctly targets empty TextBlock instances using instanceof check
  3. Placement - Filtering happens at the right spot (before stoppedMessage creation)

Verification

  • The fix addresses the root cause identified by @manikanta5827 in the issue comments
  • Works around models (GPT/Qwen on Bedrock) that insert empty text blocks alongside reasoning/tool blocks

Suggestions (non-blocking)

Consider adding a unit test for this edge case to prevent regression:

it("should filter out empty TextBlock objects from content blocks", async () => {
  // Test that empty text blocks are removed before message creation
});

Great work on debugging and fixing this! 👍🦆

Copy link
Collaborator

@strands-agent strands-agent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

👍 Looks good - Solid fix for issue #373! Just waiting for integration tests to complete.

Problem Being Solved

Some Bedrock models (GPT/Qwen) insert empty TextBlock objects in responses, causing:

ValidationException: The text field in the ContentBlock object is blank

Solution Assessment

Correct approach: Filter empty TextBlock instances before API calls
Type-safe: Uses proper instanceof check
Minimal change: Only touches affected code path
Addresses root cause: Based on issue discussion analysis

Implementation Review

The fix properly identifies and removes empty text blocks:

// Filter out empty TextBlock objects
let contentBlocks = contentBlocks.filter(
  (block) => !(block instanceof TextBlock && block.text === '')
);

This is the right pattern:

  • ✅ Checks type with instanceof (not just property existence)
  • ✅ Only removes TextBlock with empty string (not other empty content)
  • ✅ Preserves all other block types

Suggestions (Non-blocking)

  1. Edge case test: Consider adding a unit test for this scenario
it('should filter empty TextBlock objects', () => {
  const blocks = [
    new TextBlock({ text: 'Hello' }),
    new TextBlock({ text: '' }), // Should be filtered
    new TextBlock({ text: 'World' })
  ];
  const result = filterEmptyTextBlocks(blocks);
  expect(result).toHaveLength(2);
});
  1. Documentation: If this is a known Bedrock model behavior, might be worth a code comment explaining why this filter exists.

CI Status

⏳ Integration tests are pending - this is normal for external contributor PRs and requires maintainer approval to run.

Backward Compatibility

✅ No breaking changes - only prevents errors that would have occurred anyway.

Great work identifying and fixing this! The implementation looks solid. 🎉

🦆


🤖 This is an experimental AI agent response from the Strands team, powered by Strands Agents. We're exploring how AI agents can help with community support and development. Your feedback helps us improve! If you'd prefer human assistance, please let us know.

@strands-agent
Copy link
Collaborator

👋 Friendly follow-up!

This PR is:

  • APPROVED by @cagataycali
  • MERGEABLE (no conflicts)
  • ⏳ CI checks pending

This is a nice fix for issue #373 (ValidationException with blank text field). The implementation cleanly filters out empty TextBlock instances before creating the stoppedMessage. Would be great to get this merged once CI completes!

Thanks @manikanta5827 for the contribution! 🙏


🤖 This comment was generated by an AI agent using strands-agents. Workflow Run: 20944495454

@manikanta5827
Copy link
Author

Implemented a unit test for the identified edge case to ensure that empty text blocks are correctly filtered from the model response.

pgrayy
pgrayy previously approved these changes Jan 26, 2026
@pgrayy pgrayy self-requested a review January 26, 2026 21:13
@manikanta5827 manikanta5827 force-pushed the manikanta-fix_content_blocks_validation_exception branch from b15d534 to a130d7e Compare February 4, 2026 18:31
@manikanta5827
Copy link
Author

added empty commit to re run the failed jobs

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.

4 participants