[PR #8888] fix(session): safeguard message conversion against invalid prompts (#… #12902

Open
opened 2026-02-16 18:17:47 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/8888

State: open
Merged: No


What does this PR do?

Fixes #8862 - Resolves "Invalid prompt at compaction" error (AI_InvalidPromptError) during session compaction.

Problem: The error "The messages must be a ModelMessage[]" occurred when convertToModelMessages failed validation. This happened because
toModelMessage
was occasionally passing:

Empty messages (with 0 parts) to the converter.
undefined values for attachments or providerMetadata in tool-result parts, which some AI providers (like Anthropic) check strictly.

How did you verify your code works?

Solution:

Filter empty messages: Added a filter to remove messages with parts.length === 0 before conversion.
Safeguard Tool Results:
Default attachments to [] if undefined.
Default callProviderMetadata to {} if undefined.
typescript
const filtered = result
.filter((msg) => msg.parts.length > 0) // Filter out empty messages
.filter((msg) => msg.parts.some((part) => part.type !== "step-start"))
return convertToModelMessages(filtered, {
tools: options?.tools,
})

How did you verify your code works?
Verified by code inspection that filtered array will strictly contain messages with content, satisfying
ModelMessage
requirements.
Verified that part.state.attachments and part.metadata access is now null-safe, preventing undefined values from reaching the AI SDK validators.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/8888 **State:** open **Merged:** No --- ### What does this PR do? Fixes #8862 - Resolves "Invalid prompt at compaction" error (AI_InvalidPromptError) during session compaction. Problem: The error "The messages must be a ModelMessage[]" occurred when convertToModelMessages failed validation. This happened because toModelMessage was occasionally passing: Empty messages (with 0 parts) to the converter. undefined values for attachments or providerMetadata in tool-result parts, which some AI providers (like Anthropic) check strictly. ### How did you verify your code works? Solution: Filter empty messages: Added a filter to remove messages with parts.length === 0 before conversion. Safeguard Tool Results: Default attachments to [] if undefined. Default callProviderMetadata to {} if undefined. typescript const filtered = result .filter((msg) => msg.parts.length > 0) // Filter out empty messages .filter((msg) => msg.parts.some((part) => part.type !== "step-start")) return convertToModelMessages(filtered, { tools: options?.tools, }) How did you verify your code works? Verified by code inspection that filtered array will strictly contain messages with content, satisfying ModelMessage requirements. Verified that part.state.attachments and part.metadata access is now null-safe, preventing undefined values from reaching the AI SDK validators.
yindo added the pull-request label 2026-02-16 18:17:47 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12902