ok here is my patch: ``` # initialize the list of ...
# marvin-ai
d
ok here is my patch:
Copy code
# initialize the list of deltas with an empty delta
                # to facilitate comparison with the previous delta
                deltas = [langchain_core.messages.AIMessageChunk(content="")]
               

                def ensure_valid_message_order(messages):
                    # Check if there's a SystemMessage
                    system_message = next((msg for msg in messages if isinstance(msg, SystemMessage)), None)

                    # Check if there's any message with a "user" role
                    has_user_message = any(
                        isinstance(msg, HumanMessage) or
                        (hasattr(msg, 'role') and msg.role == 'user')
                        for msg in messages
                    )

                    new_messages = []

                    # Add SystemMessage at the beginning if it exists
                    if system_message:
                        new_messages.append(system_message)
                        messages = [msg for msg in messages if msg != system_message]

                    # If no SystemMessage, add a default one
                    else:
                        new_messages.append(SystemMessage(content="You are a helpful assistant."))

                    # If no user message found, add a HumanMessage right after the SystemMessage
                    if not has_user_message:
                        new_messages.append(HumanMessage(content="Please continue."))

                    # Add all remaining messages
                    new_messages.extend(messages)

                    return new_messages


                input_messages = ensure_valid_message_order(input_messages)

                for i, delta in enumerate(model.stream(input=input_messages, **kwargs)):
                    if i == 0:
                        snapshot = delta
                    else:
                        snapshot = snapshot + delta