def update_tool_calls(
    tool_calls: List[dict],
    tool_call_deltas: Optional[List[ChatCompletionDeltaToolCall]],
) -> List[dict]:
    """
    Update the list of tool calls with deltas.

    Args:
        tool_calls: The current list of tool calls
        tool_call_deltas: A list of deltas to update tool_calls with

    Returns:
        List[dict]: The updated tool calls

    """
    if not tool_call_deltas:
        return tool_calls

    for tool_call_delta in tool_call_deltas:
        # Convert ChatCompletionDeltaToolCall to dict
        delta_dict = {}
        if tool_call_delta.id is not None:
            delta_dict["id"] = tool_call_delta.id
        if tool_call_delta.type is not None:
            delta_dict["type"] = tool_call_delta.type
        if hasattr(tool_call_delta, "index"):
            delta_dict["index"] = tool_call_delta.index

        if (
            hasattr(tool_call_delta, "function")
            and tool_call_delta.function is not None
        ):
            delta_dict["function"] = {}
            if (
                hasattr(tool_call_delta.function, "name")
                and tool_call_delta.function.name is not None
            ):
                delta_dict["function"]["name"] = tool_call_delta.function.name
            if (
                hasattr(tool_call_delta.function, "arguments")
                and tool_call_delta.function.arguments is not None
            ):
                delta_dict["function"]["arguments"] = tool_call_delta.function.arguments

        if len(tool_calls) == 0:
            # First tool call
            tool_calls.append(delta_dict)
        else:
            # Try to find an existing tool call to update
            found_match = False
            for existing_tool in tool_calls:
                # Check by index if available
                index_match = False
                if "index" in delta_dict and "index" in existing_tool:
                    index_match = delta_dict["index"] == existing_tool["index"]

                # Check by id if available
                id_match = False
                if "id" in delta_dict and "id" in existing_tool:
                    id_match = delta_dict["id"] == existing_tool["id"]

                if index_match or id_match:
                    found_match = True
                    # Update existing tool call
                    if "function" in delta_dict:
                        if "function" not in existing_tool:
                            existing_tool["function"] = {}

                        # Update function name if present
                        if "name" in delta_dict["function"]:
                            if "name" not in existing_tool["function"]:
                                existing_tool["function"]["name"] = ""
                            existing_tool["function"]["name"] += delta_dict[
                                "function"
                            ].get("name", "")

                        # Update arguments if present
                        if "arguments" in delta_dict["function"]:
                            if "arguments" not in existing_tool["function"]:
                                existing_tool["function"]["arguments"] = ""
                            existing_tool["function"]["arguments"] += delta_dict[
                                "function"
                            ].get("arguments", "")

                    # Update ID if present
                    if "id" in delta_dict:
                        if "id" not in existing_tool:
                            existing_tool["id"] = ""
                        existing_tool["id"] += delta_dict.get("id", "")

                    # Update type if present
                    if "type" in delta_dict:
                        existing_tool["type"] = delta_dict["type"]

                    # Update index if present
                    if "index" in delta_dict:
                        existing_tool["index"] = delta_dict["index"]

                    break

            # If no match was found, add as a new tool call
            if not found_match and ("id" in delta_dict or "index" in delta_dict):
                tool_calls.append(delta_dict)

    return tool_calls

    Update the list of tool calls with deltas.

    Args:
        tool_calls: The current list of tool calls
        tool_call_deltas: A list of deltas to update tool_calls with

    Returns:
        List[dict]: The updated tool calls

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