@@ -416,16 +416,23 @@ def parse_conversation(messages):
416416
417417def tagged_conversation_to_messages (response_text ):
418418 """Convert a tagged conversation string or list of strings into a list of messages.
419+ If the input doesn't contain User:/Assistant: tags, return it as is.
419420
420421 Args:
421422 response_text: Either a string containing "User:" and "Assistant:" tags,
422423 or a list of such strings.
423424
424425 Returns:
425- If input is a string : A list of message dictionaries.
426- If input is a list: A list of lists of message dictionaries .
426+ If input has tags : A list of message dictionaries.
427+ If input has no tags: The original input .
427428 """
429+ def has_conversation_tags (text ):
430+ return "User:" in text or "Assistant:" in text
431+
428432 def process_single_response (text ):
433+ if not has_conversation_tags (text ):
434+ return text
435+
429436 messages = []
430437 # Split on "User:" or "Assistant:" while keeping the delimiter
431438 parts = re .split (r'(?=(User:|Assistant:))' , text .strip ())
@@ -447,7 +454,11 @@ def process_single_response(text):
447454 return messages
448455
449456 if isinstance (response_text , list ):
450- return [process_single_response (text ) for text in response_text ]
457+ processed = [process_single_response (text ) for text in response_text ]
458+ # If none of the responses had tags, return original list
459+ if all (isinstance (p , str ) for p in processed ):
460+ return response_text
461+ return processed
451462 else :
452463 return process_single_response (response_text )
453464
@@ -555,14 +566,18 @@ def proxy():
555566 except Exception as e :
556567 logger .error (f"Error processing request: { str (e )} " )
557568 return jsonify ({"error" : str (e )}), 500
558-
569+
559570 # Convert tagged conversation to messages format if needed
560571 if isinstance (response , list ):
561- response = [msg [- 1 ]['content' ] if isinstance (msg , list ) and msg else msg
562- for msg in tagged_conversation_to_messages (response )]
572+ processed_response = tagged_conversation_to_messages (response )
573+ # If processed_response is a list of message lists, extract last message content
574+ if processed_response != response : # Only process if format changed
575+ response = [msg [- 1 ]['content' ] if isinstance (msg , list ) and msg else msg
576+ for msg in processed_response ]
577+ # Otherwise keep original response
563578 else :
564579 messages = tagged_conversation_to_messages (response )
565- if messages : # Only take the last message if we have any
580+ if isinstance ( messages , list ) and messages : # Only process if format changed
566581 response = messages [- 1 ]['content' ]
567582
568583 if stream :
0 commit comments