Skip to content

fix(compaction): filter media parts to prevent API error#1398

Merged
RealKai42 merged 4 commits intoMoonshotAI:mainfrom
howardpen9:fix/filter-media-parts-in-compaction
Mar 11, 2026
Merged

fix(compaction): filter media parts to prevent API error#1398
RealKai42 merged 4 commits intoMoonshotAI:mainfrom
howardpen9:fix/filter-media-parts-in-compaction

Conversation

@howardpen9
Copy link
Copy Markdown
Contributor

@howardpen9 howardpen9 commented Mar 11, 2026

Summary

When context usage is high (~85%+) and auto-compaction triggers, media content parts (image_url, audio_url, video_url) are included in the compaction request sent to the Kimi API. The API rejects these with:

Error code: 400 - {'error': {'message': "the message at position 1 with role 'user' contains an invalid part type: video_url", 'type': 'invalid_request_error'}}

This PR filters out ImageURLPart, AudioURLPart, and VideoURLPart from the compaction input in SimpleCompaction.prepare(), while preserving media parts in recent (non-compacted) messages.

Root cause: compaction.py:177 only filtered ThinkPart but let all media parts through:

# Before
part for part in msg.content if not isinstance(part, ThinkPart)

# After
part for part in msg.content if not isinstance(part, (ThinkPart, ImageURLPart, AudioURLPart, VideoURLPart))

Changes

  • src/kimi_cli/soul/compaction.py — Filter media parts from compaction input (2-line change)
  • tests/core/test_simple_compaction.py — Add 2 test cases:
    • test_prepare_filters_out_media_parts — Verifies all 3 media types are stripped
    • test_prepare_preserves_media_parts_in_recent_messages — Verifies preserved messages keep media intact

Reproduction

  1. Upload a video file in a conversation
  2. Continue chatting until context usage reaches ~85%
  3. Auto-compaction triggers → 400 error

Related


Open with Devin

…xt compaction

When context usage is high and auto-compaction triggers, media content parts
(image_url, audio_url, video_url) were included in the compaction request.
The Kimi API does not accept media parts in compaction messages, causing a
400 error: "the message contains an invalid part type: video_url".

Filter out ImageURLPart, AudioURLPart, and VideoURLPart from compaction input,
keeping only text and tool-related parts. Preserved (recent) messages retain
their original media parts.

Fixes MoonshotAI#1395
Fixes MoonshotAI#1390
Related: MoonshotAI#802
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

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.

the message at position 1 with role 'user' contains an invalid part type: video_url error response with video attachment

2 participants