On Asterisk 18.2 when you sent a reinvite in mid-call looks like Asterisk make a response with a different codecs ordering and change the codec for the call. Is there is a specific changes or configuration for asterisk 18 about that ? I don’t remember the 17 version have the same behaviour.
There isn’t enough information here to be able to answer that. Asterisk logging would show the actual codec negotiation that is going on and why it is doing what it is doing. In general, though, the behavior should be the same.
[2021-02-26 23:04:37.1160] DEBUG[26090][C-00000004]: chan_pjsip.c:883 chan_pjsip_read_stream: Oooh, got a frame with format of ulaw on channel 'PJSIP/kjs6nq5s-0000000a' when we're sending 'opus', switching to match
If debug is enabled then the res_pjsip_sdp_rtp module will output information about the negotiation process, including codecs, when it occurs. I don’t have a specific thing to point at because you have to look at everything as a whole - configuration, the SDP aspect, the debug messages, and see what precisely is going on and where it may be going wrong. The wiki[1] has information on collecting debug logs. The precise configuration would also be needed.