Python code genration with open ai api , and storing in a file with proper indentation

Hello ,
I am able to make an API call to openai and get the python code generated based on the prompt i am passing .the repsonse is in text format . so while reading the text and storing the response (the genrated code) , the indentation is lost . Does some one faced this issue , any workaround , please suggest

Is the text completely unformatted?
How about pasting it into a decent editor like VSCode, which will at least help you format it.

Actually the plan is to take the code from the response and execute it on the fly in the backend only . But some how not able to get the text repost se to formatted code .
I tried various steps like ast , black , re . But no success

Presumably the response is in json format and so needs to be json.loaded or json.dumped before it is in a usable form?
Maybe if you share what you get when you print(response) we can help a bit more. By all means delete the code part if you don’t want to share it publicly.