fix(models): support dict-style messages in get_clean_message_list #1552
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: Support Dict-Style Messages in
get_clean_message_list
Summary
This pull request updates the
get_clean_message_list()
method inmodels.py
to support dictionary-style messages in addition to structured Message objects. This improves developer experience and alignsTransformersModel
withInferenceClientModel
, which already supports dict input formats.Problem
Passing a dictionary-style message to TransformersModel raises an AttributeError, due to direct attribute access (message.role) instead of dictionary key access:
AttributeError: 'dict' object has no attribute 'role'
This breaks many natural use cases like:
model([{"role": "user", "content": [{"type": "text", "text": "Hello"}]}])
What Was Changed
Replaced attribute-style access (message.role, message.content) with dictionary-style access (message['role'], message['content']) throughout the get_clean_message_list() function.
Ensured message merging, image processing, and content flattening still function correctly.
Prevented crashes for users providing standard dict inputs, improving ease of use and compatibility.
Also added a test case to validate this functionality.
Test passed locally with
pytest -k test_transformers_model_with_dict_message
Happy to add documentation or additional test coverage if needed.