The "Modular" approach is a total lifesaver for this.
I’ve run into this exact same headache more times than I can count! It’s super frustrating when you're 90% of the way through a project and the AI just decides to start hallucinating variable names or mangling the indentation. The reality is that ChatGPT has a "token limit," which basically means it runs out of "breath" if the script gets too long. When it hits that limit, it starts taking shortcuts to try and finish the task.
Here is the workflow I’ve been using lately that actually works for complex Python scripts:
1. Get a "Blueprint" first
Instead of asking for the full code in the first prompt, I always ask for a detailed outline or class structure. I’ll say something like: "I want to build a data scraper. First, give me a detailed architectural outline of the functions, classes, and global variables we will need. Don't write the full logic yet, just the skeleton." This forces the AI to commit to a structure that it can refer back to later.
2. Build it function-by-function
Once the blueprint is there, I have it write the code in sections. I’ll say: "Okay, now write the code for the request_manager function based on that outline." This keeps the output short enough that it won't hit the token limit, meaning you get cleaner code and perfect indentation every time.
3. Use a "Context Reset" for every new block
If the script is really long and you’re several messages deep, the AI starts to lose the "thread." To fix this, I often start a new prompt by reminding it of the variables: "We are using the 'df' variable for the dataframe and 'soup' for the BeautifulSoup object. Keeping those in mind, please write the data cleaning loop." It sounds redundant, but it stops those random variable hallucinations.
4. Fixing the "Continue Generating" indentation bug
When it cuts off and you hit that "Continue" button, it almost always breaks the formatting. My trick is to not use the button. Instead, I type: "You cut off at [copy/paste the last line it wrote]. Please pick up exactly where you left off, starting with a fresh code block and maintaining the correct indentation." This usually forces it to format the code correctly again.
5. Ask for "Placeholder" scripts
If you need to see how it all fits together at the end, ask it to output the "full" script but use placeholders for the logic you've already verified. For example: "Show me the main script structure, but use # [Logic from previous step] for the parsing function so we don't hit the character limit."
It’s definitely more "babysitting" than just getting a one-and-done script, but it’s the only way I’ve found to get high-quality, bug-free Python out of it when things get complex. Hope that helps you get that scraper finished!