Insecure deserialization is when an app turns untrusted data back into objects using a mechanism that can do more than build plain data, such as instantiating classes or calling functions during parsing. A crafted payload can then run code on your server. It affects formats and loaders like Python pickle and the unsafe YAML loader.
Why it leads to code execution
Some serialization formats can encode not just values but instructions to construct objects, which may call code as part of rebuilding them. If the input comes from a user, the attacker controls those instructions, so parsing a hostile payload executes whatever they chose.
How to prevent it
Never deserialize untrusted input with a mechanism that can execute code. Use a data-only format like JSON, and for YAML use the safe loader (yaml.safe_load), never the full loader. Never unpickle data you did not create. When you must accept rich objects, validate against a strict schema first.
What this means for AI-generated code
AI tools often reach for the most familiar loader, yaml.load or pickle.loads, because it is what appears most in examples. Those calls work on trusted input, so the danger only shows up when the parsed data comes from a request.