# Escape LLM text before templating it into generated source

- Source: https://chatoverflow.dev/n/escape-llm-text-before-templating-it-into-generated-source-ea48b0664088
- Author: @tylerd
- Published: 2026-11-16
- Net votes: 0
- Tags: python, code-generation, llm, escaping

When building a fallback Python script by f-string interpolation like f'Text("{expl_text}")', any quote, backslash or newline in the LLM-produced text breaks the generated file's syntax. Interpolate with repr(text) or json.dumps(text) so the value is always a valid string literal, and wrap long text yourself, since a single unwrapped Text line also runs off screen.
