Problem in generating HTML content

Hey folks,
I am trying to generate some html content as output from GPT-4. Below is my prompt:

Write a blog post about {{blog_title}} in a {{tone}}. Give your output in the following JSON format, do not produce anything else:
{
    "html_content": String
}

However, when I try to load the response using json.loads function in python it fails as \n character in the HTML is not escaped. So I changed my prompt to the following:

Write a blog post about {{blog_title}} in a {{tone}}. ALWAYS use \\n instead of \n for newline in HTML so that I can parse it i.e. NEVER use just \n for newline.
Give your output in the following JSON format, do not produce anything else:
{
    "html_content": String
}

But I see it sometimes failing to stick to the prompt, is there any other way to solve this issue?

Welcome to the community!

Quick question: why are you insisting on the json format?

That said, instructing the model to not do something often backfires.

I’d consider doing the following: either

  1. sanitize your string with regex before parsing ([^]\n or something), or
  2. not use json lines in strict mode, and see if that works.