TLDR Web Dev · ARTICLES & TUTORIALS

DYNAMICALLY PATCH A PYTHON FUNCTION'S SOURCE CODE AT RUNTIME

ericmjl.github.io12 min read

Summary from the TLDR Web Dev newsletter

Python functions can be dynamically patched at runtime using `compile` and `exec` for building flexible AI bots. This can be used to generate and execute code with access to the current environment, enabling useful functionalities such as manipulating dataframes without pre-defined tools. However, this also has security risks, as malicious LLM outputs could harm the system.