Skip to content

Commit 70e7fce

Browse files
committed
Fix: Handle content list correctly when appending to user message
Resolves API error caused by incorrect modification of message content when 'append_to_last_user_message' is used and the last user message content is a list of parts (e.g., multimodal). Ensures string is appended to the last text part or added as a new text part. Also corrects a SyntaxError from previous diff application.
1 parent 434f8ba commit 70e7fce

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

cot_proxy.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -280,28 +280,55 @@ def proxy(path):
280280
json_body[key] = value
281281
logger.debug(f"Overriding LLM parameter for 'default': {key} = {value}")
282282

283-
284283
logger.info(f"Using think tags for model '{target_model_for_log}': START='{effective_think_start_tag}', END='{effective_think_end_tag}'")
285284
logger.info(f"Think tag filtering enabled: {enable_think_tag_filtering} for model '{target_model_for_log}'")
286285

287286
append_string = model_specific_config.get('append_to_last_user_message')
288-
if append_string:
287+
if append_string and json_body: # Ensure json_body exists
289288
if 'messages' not in json_body or not json_body['messages']:
290289
# No messages: create a new user message with the string
291290
json_body.setdefault('messages', [])
292291
json_body['messages'].append({"role": "user", "content": append_string})
293292
logger.debug(f"Created new user message with content: {append_string}")
294293
else:
295294
# Find the last message to append to
296-
last_message = json_body['messages'][-1]
297-
if last_message.get('role') == 'user':
298-
last_message['content'] += append_string
299-
logger.debug(f"Appended to existing user message: {append_string}")
300-
else:
301-
# Last message is not user: insert a new user message
295+
if json_body['messages']: # Ensure messages list is not empty
296+
last_message = json_body['messages'][-1]
297+
if last_message.get('role') == 'user':
298+
if isinstance(last_message.get('content'), str):
299+
last_message['content'] += append_string
300+
logger.debug(f"Appended to existing user message (string content): {append_string}")
301+
elif isinstance(last_message.get('content'), list):
302+
content_list = last_message['content']
303+
appended_to_existing_text_part = False
304+
# Iterate backwards to find the last text part to append to
305+
# This handles cases like a list of text and image parts.
306+
for i in range(len(content_list) - 1, -1, -1):
307+
part = content_list[i]
308+
if isinstance(part, dict) and part.get('type') == 'text' and 'text' in part:
309+
part['text'] += append_string
310+
appended_to_existing_text_part = True
311+
logger.debug(f"Appended to last text part of user message content list: {append_string}")
312+
break
313+
314+
if not appended_to_existing_text_part:
315+
# If no suitable text part was found (e.g. list of images, or empty list),
316+
# add a new text part.
317+
content_list.append({'type': 'text', 'text': append_string})
318+
logger.debug(f"Added new text part to user message content list: {append_string}")
319+
else:
320+
# Content is not a string or list (e.g., None or unexpected type)
321+
# Set the content to the append_string.
322+
original_content_type = type(last_message.get('content')).__name__
323+
last_message['content'] = append_string
324+
logger.warning(f"Last user message content was '{original_content_type}'. Overwritten with new string content: {append_string}")
325+
else:
326+
# Last message is not user: insert a new user message
327+
json_body['messages'].append({"role": "user", "content": append_string})
328+
logger.debug(f"Last message not 'user'. Inserted new user message with content: {append_string}")
329+
else: # messages list is empty
302330
json_body['messages'].append({"role": "user", "content": append_string})
303-
logger.debug(f"Inserted new user message with content: {append_string}")
304-
331+
logger.debug(f"Messages list was empty. Created new user message with content: {append_string}")
305332

306333
# Try to connect with a timeout
307334
try:

0 commit comments

Comments
 (0)