def prepare_chat_params(
    model: str, messages: Sequence[ChatMessage], **kwargs: Any
) -> tuple[types.Content, ChatParams]:
    """
    Prepare common parameters for chat creation.

    Args:
        messages: Sequence of chat messages
        **kwargs: Additional keyword arguments

    Returns:
        tuple containing:
        - next_msg: the next message to send
        - chat_kwargs: processed keyword arguments for chat creation

    """
    merged_messages = merge_neighboring_same_role_messages(messages)
    initial_history = list(map(chat_message_to_gemini, merged_messages))

    # merge tool messages into a single tool message
    # while maintaining the tool names
    history = []
    for idx, msg in enumerate(initial_history):
        if idx < 1:
            history.append(msg)
            continue

        # Skip if the role is different or not a tool message
        if msg.parts and not any(
            part.function_response is not None for part in msg.parts
        ):
            history.append(msg)
            continue

        last_msg = history[-1]

        # Skip if the last message is not a tool message
        if last_msg.parts and not any(
            part.function_response is not None for part in last_msg.parts
        ):
            history.append(msg)
            continue

        # Skip if the role is different
        if last_msg.role != msg.role:
            history.append(msg)
            continue

        # Merge the tool messages
        last_msg.parts.extend(msg.parts or [])

    # Separate the next message from the history
    next_msg = history.pop()

    tools: types.Tool | list[types.Tool] | None = kwargs.pop("tools", None)
    if tools and not isinstance(tools, list):
        tools = [tools]

    config: Union[types.GenerateContentConfig, dict] = kwargs.pop(
        "generation_config", {}
    )
    if not isinstance(config, dict):
        config = config.model_dump()

    chat_kwargs: ChatParams = {"model": model, "history": history}

    if tools:
        if not config.get("automatic_function_calling"):
            config["automatic_function_calling"] = types.AutomaticFunctionCallingConfig(
                disable=True, maximum_remote_calls=None
            )

        if not config.get("tool_config"):
            config["tool_config"] = kwargs.pop("tool_config", None)

        if not config.get("tools"):
            config["tools"] = tools

    chat_kwargs["config"] = types.GenerateContentConfig(**config)

    return next_msg, chat_kwargs

    Prepare common parameters for chat creation.

    Args:
        messages: Sequence of chat messages
        **kwargs: Additional keyword arguments

    Returns:
        tuple containing:
        - next_msg: the next message to send
        - chat_kwargs: processed keyword arguments for chat creation

    
llama-index-integrations/llms/llama-index-llms-google-genai/llama_index/llms/google_genai/utils.py