def _parse_chat_history(history: Any, is_gemini: bool) -> Any:
    """
    Parse a sequence of messages into history.

    Args:
        history: The list of messages to re-create the history of the chat.

    Returns:
        A parsed chat history.

    Raises:
        ValueError: If a sequence of message has a SystemMessage not at the
        first place.

    """
    vertex_messages, context = [], None
    for i, message in enumerate(history):
        if i == 0 and message.role == MessageRole.SYSTEM:
            if is_gemini:
                raise ValueError("Gemini model doesn't support system messages")
            context = message.content
        elif message.role in (
            MessageRole.MODEL,
            MessageRole.USER,
        ):
            if is_gemini:
                from llama_index.llms.vertex.gemini_utils import (
                    convert_chat_message_to_gemini_content,
                )

                context = message.content
                vertex_messages.append(
                    convert_chat_message_to_gemini_content(
                        message=message, is_history=True
                    )
                )
            else:
                vertex_message = VertexChatMessage(
                    content=message.content,
                    author=(
                        "bot"
                        if message.role in (MessageRole.ASSISTANT, MessageRole.MODEL)
                        else MessageRole.USER
                    ),
                )
                vertex_messages.append(vertex_message)
        else:
            # Roles are consolidated to two roles in merge_neighboring_same_role_messages
            # Other roles will cause error
            raise ValueError(
                f"Unexpected message with role {message.role} at the position {i}."
            )

    if len(vertex_messages) % 2 != 0:
        raise ValueError("total no of messages should be even")

    return {"context": context, "message_history": vertex_messages}

    Parse a sequence of messages into history.

    Args:
        history: The list of messages to re-create the history of the chat.

    Returns:
        A parsed chat history.

    Raises:
        ValueError: If a sequence of message has a SystemMessage not at the
        first place.

    
llama-index-integrations/llms/llama-index-llms-vertex/llama_index/llms/vertex/utils.py