Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Jan 25, 2026

Fixes the "Cannot read properties of undefined (reading 'filter')" error reported in Reddit.

Problem

Users were experiencing API request failures when using Anthropic Claude models with the error:

Cannot read properties of undefined (reading 'filter')

The error occurred in filterNonAnthropicBlocks() when attempting to filter message content that was undefined, null, or not an array.

Solution

Added a guard clause in filterNonAnthropicBlocks() to check if message.content is:

  • undefined
  • null
  • Not an array

If any of these conditions are true, the message is filtered out gracefully rather than causing a crash.

Changes

  • Added defensive check in filterNonAnthropicBlocks()
  • Added comprehensive test coverage for edge cases:
    • Undefined content
    • Null content
    • Non-array content (e.g., objects)
    • Empty array content

Testing

All tests pass (13/13) including 4 new tests for edge cases.


View task on Roo Code Cloud


Important

Adds a guard clause in filterNonAnthropicBlocks() to handle undefined, null, or non-array message.content, preventing crashes and adding tests for these cases.

  • Behavior:
    • Adds a guard clause in filterNonAnthropicBlocks() in anthropic-filter.ts to handle undefined, null, or non-array message.content.
    • Filters out messages with invalid content gracefully, preventing crashes.
  • Testing:
    • Adds tests in anthropic-filter.spec.ts for edge cases: undefined, null, non-array, and empty array content.
    • All tests pass, including 4 new tests for edge cases.

This description was created by Ellipsis for 33687d9. You can customize this summary. It will automatically update as commits are pushed.

- Add guard clause to check if message.content is undefined, null, or not an array
- Prevents "Cannot read properties of undefined (reading filter)" error
- Add comprehensive tests for edge cases (undefined, null, non-array, empty array)
- Fixes issue reported in Reddit where Anthropic API calls would fail unexpectedly
@roomote
Copy link
Contributor Author

roomote bot commented Jan 25, 2026

Rooviewer Clock   See task on Roo Cloud

Review complete. No issues found.

The implementation correctly adds a guard clause to handle undefined, null, or non-array message.content in filterNonAnthropicBlocks(). The fix prevents the "Cannot read properties of undefined (reading 'filter')" crash and includes comprehensive test coverage for all edge cases.

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

@roomote
Copy link
Contributor Author

roomote bot commented Jan 25, 2026

Closing this PR to investigate the root cause. The defensive fix treats the symptom (undefined content) rather than addressing why content is undefined in the first place, which violates the type contract. Will create a new PR after finding where malformed messages are being created.

@roomote roomote bot closed this Jan 25, 2026
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Jan 25, 2026
@github-project-automation github-project-automation bot moved this from Triage to Done in Roo Code Roadmap Jan 25, 2026
roomote bot pushed a commit that referenced this pull request Jan 25, 2026
…errors

Root cause: JSON.parse() in readApiMessages() deserializes data with zero validation,
allowing corrupted or legacy data to violate type contracts (e.g., undefined content).

This causes downstream crashes in functions like filterNonAnthropicBlocks() that assume
message.content is always string | ContentBlock[] per the type system, but receive
undefined/null values from corrupted historical data.

Solution:
- Add validateApiMessage() to sanitize deserialized messages at the boundary
- Fix undefined/null/non-array content by converting to empty array
- Filter out malformed messages with invalid roles or non-object structure
- Filter out invalid content blocks missing the required type field
- Add comprehensive test coverage (12 tests covering all edge cases)

Fixes: https://www.reddit.com/r/RooCode/s/yUXVFMYu3P
Related to closed PR #10954 (which treated symptom, not cause)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants