Solving JSON Output Formatting Without Code Blocks in openai

Hey everyone,

I’ve been struggling with something that’s probably frustrating many of you building automation tools with the API. No matter how explicitly I instruct my assistant to generate only JSON output, it insists on wrapping the response in markdown code blocks like this:
json** { "property": "value" } **

This breaks my automation pipeline because I need raw JSON without the markdown formatting.

The Problem

Even with clear instructions like:

Mandatory technical rules for JSON format

  • Do not include markdown code blocks (```json)
  • Properly escape all special characters (", , /, \n, \r, \t)
  • Convert all line breaks to \n
  • Preserve visual structure with “\n” for line breaks and numbering for lists
  • Do not add text outside the JSON object
  • The “mensaje” field must be a single properly escaped line, but maintaining readable structure
  • Example format for lists:
    "mensaje": "Te recomiendo los siguientes productos:\n1. Heladera Comercial Línea ITALY\n2. Exhibidora Vertical\n3. Cámaras Frigoríficas\n\nPodés ver más detalles en nuestra web: https://www.defrancesco.com.ar/"
    
    

Has anyone else found other reliable solutions for this common issue?

i have found that a system record appended rather than the traditional prepend will create a needed recency for json in particular. also, the latest user record should within its reply end with clear instructions to only reply with the defined json format. Taken together the json compliancy will increase (my exp anyways)