opencode icon indicating copy to clipboard operation
opencode copied to clipboard

fix(session): safeguard message conversion against invalid prompts (#…

Open shoaibansari5398 opened this issue 14 hours ago • 1 comments

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.

shoaibansari5398 avatar Jan 16 '26 13:01 shoaibansari5398