Skip to content
This repository was archived by the owner on May 15, 2026. It is now read-only.

Commit 33687d9

Browse files
committed
fix: handle undefined/null/non-array content in filterNonAnthropicBlocks
- 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
1 parent 953c777 commit 33687d9

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

src/api/transform/__tests__/anthropic-filter.spec.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,61 @@ describe("anthropic-filter", () => {
140140
expect(result).toHaveLength(1)
141141
expect(result[0].content).toEqual([{ type: "text", text: "Valid text" }])
142142
})
143+
144+
it("should handle messages with undefined content gracefully", () => {
145+
const messages: Anthropic.Messages.MessageParam[] = [
146+
{ role: "user", content: "Hello" },
147+
{ role: "assistant", content: undefined as any },
148+
{ role: "user", content: "Continue" },
149+
]
150+
151+
const result = filterNonAnthropicBlocks(messages)
152+
153+
expect(result).toHaveLength(2)
154+
expect(result[0].content).toBe("Hello")
155+
expect(result[1].content).toBe("Continue")
156+
})
157+
158+
it("should handle messages with null content gracefully", () => {
159+
const messages: Anthropic.Messages.MessageParam[] = [
160+
{ role: "user", content: "Hello" },
161+
{ role: "assistant", content: null as any },
162+
{ role: "user", content: "Continue" },
163+
]
164+
165+
const result = filterNonAnthropicBlocks(messages)
166+
167+
expect(result).toHaveLength(2)
168+
expect(result[0].content).toBe("Hello")
169+
expect(result[1].content).toBe("Continue")
170+
})
171+
172+
it("should handle messages with non-array content gracefully", () => {
173+
const messages: Anthropic.Messages.MessageParam[] = [
174+
{ role: "user", content: "Hello" },
175+
{ role: "assistant", content: { invalid: "object" } as any },
176+
{ role: "user", content: "Continue" },
177+
]
178+
179+
const result = filterNonAnthropicBlocks(messages)
180+
181+
expect(result).toHaveLength(2)
182+
expect(result[0].content).toBe("Hello")
183+
expect(result[1].content).toBe("Continue")
184+
})
185+
186+
it("should handle empty array content", () => {
187+
const messages: Anthropic.Messages.MessageParam[] = [
188+
{ role: "user", content: "Hello" },
189+
{ role: "assistant", content: [] },
190+
{ role: "user", content: "Continue" },
191+
]
192+
193+
const result = filterNonAnthropicBlocks(messages)
194+
195+
expect(result).toHaveLength(2)
196+
expect(result[0].content).toBe("Hello")
197+
expect(result[1].content).toBe("Continue")
198+
})
143199
})
144200
})

src/api/transform/anthropic-filter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ export function filterNonAnthropicBlocks(
3232
return message
3333
}
3434

35+
// Guard against undefined, null, or non-array content
36+
if (!message.content || !Array.isArray(message.content)) {
37+
return undefined
38+
}
39+
3540
const filteredContent = message.content.filter((block) => {
3641
const blockType = (block as { type: string }).type
3742
// Only keep block types that Anthropic recognizes

0 commit comments

Comments
 (0)