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)

For me, I just instruct with “raw json”, and that remove the code block.

why not use a sanitizer? you can force the structure of the output however u want with one

Why dont you use the json strucured output feature?

I explored numerous solutions, including several that your team suggested (thank you very much for that), but it still occasionally returns raw JSON and sometimes in markdown format.

However, I utilized a regex and sanitized it to ensure everything is fine :slight_smile: I truly appreciate all your responses.