The purpose of any contraception is not to “raise offspring”. There is nothing there that applies uniquely to Onan and Thamar, but not to a regular marriage.
The levirical marriage, as cited earlier says that the surviving brother has to marry the widow. While the purpose of the marriage is indeed to raise a son in the deceased’s name, that is not that different from any other marriage, which is always connected to the overriding commandment to “procreate and fill the earth”. We saw from by emphasis earlier that Thamar was a full-fledged wife, not a sperm recipient. Now, implicit in marriage is to have sex. If, for example, Onan and Thamar did not produce an offspring despite trying, there would be no transgression.
Onan’s behavior was exactly contracepted sex and he was punished precisely for contracepting.
This took place before the Law. Onan’s father gave him specific instructions (”raise up offspring for your brother), which he specifically defied (”so as not to give offspring to his brother”). Why? “Onan knew that the offspring would not be his...”
That is not analogous to a man & his wife choosing not to have kids during a specific period of time.