This question, in particular the word “safe” is somewhat ambiguous. The original asker clarified that their intent was to ask for the largest UDP packet size that could be used without incurring IP fragmentation. There are two considerations that bear on this: fragmentation of IP frames along a path across the Internet, and the ability of the hosts at either end to defragment successfully.
The largest IPv4 datagram that can be guaranteed never to suffer fragmentation is very small - from RFC 791:
Every internet module must be able to forward a datagram of 68 octets without further fragmentation. This is because an internet header may be up to 60 octets, and the minimum fragment is 8 octets.
IPv6 is more demanding and requires that every link support an MTU of at least 1280 octets; routers are free to use links with physically smaller MTUs but must reassemble any fragments before forwarding a complete IPv6 frame again.
However, it is very unlikely for any path through the Internet to hit a link with such a low MTU. Generally we can expect that the physical links in use in the public Internet to have MTUs of 1500 octets. This value is the default MTU for 802.3 Ethernet, although there are extensions to support much larger MTUs, such as 9,000 octets (so called jumbo frames). However the way that IP is actually carried over such links often involves tunnels of various kinds, such as VLANs, MPLS, and VPNs; these all add small amounts of overhead to each packet, and so the MTU is often anything from 4 to 12 or more octets smaller than 1500.
In practice, the only reliable way to determine the MTU for a path is to use MTU discovery; this is a complex enough topic in its own right and one which we do not discuss further here.
In IPv4, hosts are required to be able to reassemble IP frames up to 576 octets in length; in IPv6, the minimum is the same as the minimum MTU of 1280 octets. In practice, most hosts can reassemble much larger IP frames.
So to answer the question, a “safe” UDP packet size to use would be one which will avoid any fragmentation; unfortunately that is simply not possible over IPv4, since any UDP packet with payload could potentially be fragmented - very unlikely, but possible. Given that, PMTUD (path MTU discovery) is the best way to minimize the likelihood of fragmentation. Even doing that can still incur fragmentation, if routing updates change the path to include a link with smaller MTU. Thus “safe” can be interpreted to mean “guaranteed to be able to be reassembled, if fragmented”, to which the answer is 576 for IPv4 and 1280 for IPv6.