Skip to content

Commit 8438376

Browse files
committed
Update optillm.py
1 parent 0b045a3 commit 8438376

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

optillm.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,29 @@ def get_config():
118118

119119
plugin_approaches = {}
120120

121+
def normalize_message_content(messages):
122+
"""
123+
Ensure all message content fields are strings, not lists.
124+
Some models don't handle list-format content correctly.
125+
"""
126+
normalized_messages = []
127+
for message in messages:
128+
normalized_message = message.copy()
129+
content = message.get('content', '')
130+
131+
# Convert list content to string if needed
132+
if isinstance(content, list):
133+
# Extract text content from the list
134+
text_content = ' '.join(
135+
item.get('text', '') for item in content
136+
if isinstance(item, dict) and item.get('type') == 'text'
137+
)
138+
normalized_message['content'] = text_content
139+
140+
normalized_messages.append(normalized_message)
141+
142+
return normalized_messages
143+
121144
def none_approach(
122145
client: Any,
123146
model: str,
@@ -143,10 +166,13 @@ def none_approach(
143166
model = model[5:]
144167

145168
try:
146-
# Make the direct completion call with original messages and parameters
169+
# Normalize message content to ensure it's always string
170+
normalized_messages = normalize_message_content(original_messages)
171+
172+
# Make the direct completion call with normalized messages and parameters
147173
response = client.chat.completions.create(
148174
model=model,
149-
messages=original_messages,
175+
messages=normalized_messages,
150176
**kwargs
151177
)
152178

0 commit comments

Comments
 (0)