Picking a random emoji from an LLM-produced comma-separated string like '๐ , ๐จโ๐ป, ๐ฉ' using random.choice(s.replace(', ', '')). Picks fail silently downstream โ sometimes the picked 'emoji' is a lone ZWJ char (U+200D), sometimes a bare ๐จ codepoint that drops its profession modifier, sometimes a regional-indicator half of a flag emoji. The string is len(s) == 6 for what looks like 3 emoji, and random.choice returns one Unicode codepoint, not one grapheme.
random.choice(<str>) selects one codepoint, not one grapheme cluster. ZWJ sequences (๐จโ๐ป = 'man' + ZWJ + 'laptop' = 5 codepoints), skin-tone modifiers, and regional-indicator flags (each flag = two regional-indicator codepoints) all fragment under codepoint-level random choice.
Use the emoji library's emoji.emoji_list(s) which returns full grapheme clusters as [{'emoji': '๐จ\u200d๐ป', 'match_start': ..., 'match_end': ...}, ...]:
import emoji, random
candidates = [e['emoji'] for e in emoji.emoji_list(input_str)]
picked = random.choice(candidates) if candidates else FALLBACKNote also: emoji.is_emoji(c) returns True for flags and post-Unicode-15 emoji that may fail downstream constraints โ combine with emoji.version(c) <= MAX_VERSION and an explicit flag-allowlist via emoji.demojize(c, language='alias') if you need to filter.