Skip to content

Fix: Convert ContentPart list content to string in message extraction, fixes 500 Error with Qwen Code CLI#433

Merged
jundot merged 1 commit intojundot:mainfrom
mbauer:fix/content-list-type-error
Mar 29, 2026
Merged

Fix: Convert ContentPart list content to string in message extraction, fixes 500 Error with Qwen Code CLI#433
jundot merged 1 commit intojundot:mainfrom
mbauer:fix/content-list-type-error

Conversation

@mbauer
Copy link
Copy Markdown

@mbauer mbauer commented Mar 27, 2026

A note ahead: this is AI coded with minor guidance from me. The patch is small (31 lines added) and it fixes a 500 error.

Summary

When using Qwen Code CLI, I got 500 Errors with oMLX but not with plain mlx-server.

The error ValueError: text input must be of type 'str'... was caused by messages with content as a list of ContentPart objects being passed to the MLX tokenizer's apply_chat_template method instead of being converted to strings first.

Root Cause

When the Qwen Code client sent messages with content arrays like:

 1 {"role": "user", "content": [{"type": "text", "text": "hi"}]}

Pydantic parsed these as List[ContentPart] objects. The extract_text_content() function was supposed to convert these to strings, but there were edge cases where:

 1. Tool messages (role="tool") with ContentPart lists weren't being converted
 2. Harmony messages with ContentPart lists weren't being converted properly
 3. The final safety check wasn't ensuring all content was strings

Fix Applied

 1. `_extract_text_from_content_list()`: Enhanced to handle edge cases and return empty string for empty lists
 2. `extract_text_content()`: Added ContentPart list handling for tool messages and final safety check
 3. `extract_multimodal_content()`: Added ContentPart list handling for tool messages
 4. `extract_harmony_messages()`: Added ContentPart list handling for tool and assistant messages

Tests Added

 - test_tool_response_message_with_content_part_list: Tests tool messages with ContentPart lists
 - Enhanced test_content_array_with_pydantic: Verifies content is string type

All existing tests continue to pass.

  • _extract_text_from_content_list(): Enhanced to handle edge cases
  • extract_text_content(): Add ContentPart list handling for tool messages and final safety check to ensure all content is string type
  • extract_multimodal_content(): Add ContentPart list handling for tool messages
  • extract_harmony_messages(): Add ContentPart list handling for tool and assistant messages

Fixes ValueError when messages with content arrays are sent to MLX models.

- _extract_text_from_content_list(): Enhanced to handle edge cases
- extract_text_content(): Add ContentPart list handling for tool messages
  and final safety check to ensure all content is string type
- extract_multimodal_content(): Add ContentPart list handling for tool messages
- extract_harmony_messages(): Add ContentPart list handling for tool and
  assistant messages

Fixes ValueError when messages with content arrays are sent to MLX models.

Co-authored-by: Qwen-Coder <[email protected]>
Copy link
Copy Markdown
Owner

@jundot jundot left a comment

Choose a reason for hiding this comment

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

Thanks for the fix! Tool messages with list content was a real gap.

I'll clean up a couple of things in a follow-up commit (remove the safety check catch-all, refactor some inline duplication, add a few more tests).

@jundot jundot merged commit d59e7e8 into jundot:main Mar 29, 2026
jundot added a commit that referenced this pull request Mar 29, 2026
@jundot
Copy link
Copy Markdown
Owner

jundot commented Mar 29, 2026

@mbauer v0.3.0rc1 is out with your ContentPart fix included. https://github.com/jundot/omlx/releases/tag/v0.3.0rc1 — if you get a chance, please give it a test and let me know if anything looks off. thanks!

AlexTzk pushed a commit to AlexTzk/omlx that referenced this pull request Mar 29, 2026
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.

2 participants