Json schema pattern breaks on tabs and newlines

Environment

  • Surface: API, Azure
  • Model: gpt-5.6-terra and sol

Bug

Using \t and \n as part of json schema patterns makes the model stop outputting at that point. This works in gpt-4.1 but does not work in gpt-5.6-terra or sol. Substituting the \t and \n with ‘⇥’;
‘↵’ does work so an ugly workaround is possible.

Reproduction

: Create a json schema output with a pattern that contains “\t” or “\n”

Diagnostics

  • Exact error and HTTP status: No errors, the model just stops outputting more for that field.
  • When it happened (with timezone) and how often: Consistently reproducible

Hi: your info isn’t quite enough for me to infer what you are up to.

Is this on the Responses API, or Chat Completions?

Are you using text.format with json_output or json_schema? Is this a strict structured output format?

Where exactly is a tab or linefeed being used in the schema?

Confusion: The schema that is transmitted when using the response_format for making structured output is rewritten when placed into AI context, and a custom grammar is formed to enforce it when strict. The unseen input to the AI model that acts as instructions is stripped of any beautified formatting, and the output grammar enforces only one style of indented per-line output formatting. The only place the AI could write a tab or a linefeed “incorrectly” then would be within one of the strings.

Do you have a similar reproduction API call?

Thanks. Answers in order, then a minimal repro.

API: Chat Completions (POST /openai/v1/chat/completions) on Azure OpenAI.

Format: response_format: {“type”: “json_schema”, “json_schema”: {“strict”: true, …}}. Strict structured output.

The tabs and linefeeds are inside a regex pattern on a string property. We want a string value shaped like
Heart…Lungs…, so the property is:

“status”: { “type”: “string”, “pattern”: “^Heart\t[^\t\n]\n\nLungs\t[^\t\n]$” }

That is the regex escapes \t and \n (JSON-encoded as \t, \n). So yes, the only place a tab or LF appears in the output is inside that one string, which is exactly where we expect it.

What happens: as soon as the model emits the JSON escape \t inside the constrained string, the grammar lets the string close, even though the value cannot match the pattern yet. We get {“status”:“Heart\t” } with finish_reason: “stop”, every time. The value violates the schema’s own pattern, which strict mode should make impossible.

┌───────────────┬───────────────────────────┬─────────────────────────────────────────────────────┬────────────────────────────┐
│ Deployment │ Pattern with \t/\n │ Same layout using ⇥ (U+21E5) and ↵ (U+21B5) instead │ No pattern │
├───────────────┼───────────────────────────┼─────────────────────────────────────────────────────┼────────────────────────────┤
│ gpt-5.6-terra │ {“status”:“Heart\t” } 3/3 │ full value, matches │ full value with real \t/\n │
├───────────────┼───────────────────────────┼─────────────────────────────────────────────────────┼────────────────────────────┤
│ gpt-5.6-luna │ {“status”:“Heart\t” } 3/3 │ full value, matches │ full value with real \t/\n │
├───────────────┼───────────────────────────┼─────────────────────────────────────────────────────┼────────────────────────────┤
│ gpt-6-sol {“status”:“Heart\t” } 3/3 │ full value, matches │ full value with real \t/\n │
├───────────────┼───────────────────────────┼─────────────────────────────────────────────────────┼────────────────────────────┤
│ gpt-4.1 │ full value, matches │ full value, matches │ full value │
└───────────────┴───────────────────────────┴─────────────────────────────────────────────────────┴────────────────────────────┘

Note the model has no trouble writing \t/\n inside the string when there is no pattern, and gpt-4.1 satisfies the escaped pattern. Replacing the control characters in the pattern with ordinary non-ASCII code points avoids the problem completely on the same deployments, which is our current workaround.

Repro (Python stdlib, key in AZURE_OPENAI_API_KEY, replace ):

import json, os, urllib.request
URL = “https://.cognitiveservices.azure.com/openai/v1/chat/completions”
body = {
“model”: “gpt-5.6-terra”,
“messages”: [
{“role”: “system”, “content”: “Return a clinical note as JSON. Write the field ‘status’ as:
HeartfindingsLungsfindings.”},
{“role”: “user”, “content”: “Heart: regular rhythm, no murmurs. Lungs: clear on both sides, no crackles.”},
],
“response_format”: {“type”: “json_schema”, “json_schema”: {
“name”: “note”, “strict”: True,
“schema”: {
“type”: “object”,
“properties”: {“status”: {“type”: “string”, “pattern”: r"^Heart\t[^\t\n]\n\nLungs\t[^\t\n]$"}},
“required”: [“status”], “additionalProperties”: False}}},
“max_completion_tokens”: 2000,
}
req = urllib.request.Request(URL, data=json.dumps(body).encode(),
headers={“Content-Type”: “application/json”, “api-key”: os.environ[“AZURE_OPENAI_API_KEY”]})
d = json.loads(urllib.request.urlopen(req).read())
print(d[“choices”][0][“finish_reason”], d[“choices”][0][“message”][“content”])

Output on gpt-5.6-terra, gpt-5.6-luna and gpt-6-sol:

stop {“status”:“Heart\t” }

Streaming and non-streaming behave the same.

Unfortunately I don’t have any credits on openai.com directly so it is of course possible that this is only broken on azure, but 4.1 works there

I created a reproduction on my own API call platform that allows better construction of arbitrary JSON schemas, and lets you view the true results.

Since the purpose of the regex as “pattern” field is a bit obtuse, and we don’t want the AI to try to solve problems, I just had AI repeat text including tabs:

Heart	P

Lungs	F

The faulty response is included.

You can press “code” to see what was sent, and can delete the assistant response and make your own call if you want to input your own temporary project API key to make a direct API call.

The notable thing: remove “pattern” and the AI can echo the string in full.

Replicated on both Chat Completions and Responses.

I suspect that the regex is too much to unpack into a context-free grammar for an AI string. You might want to use Lark and the custom output format, and see if what you want can even be produced in such a “strict” schema, which will likely not allow recursive or variable length outputs unless there is an array being produced.

Or, just make this output easy for the AI with individual fields, and translate it into the destination combined JSON format.

Note that this worked just fine on 4.1. (I have have not tried all models between), and the resulting behaviour produces output which is not legal according to the supplied pattern. While this isn’t very well documented, it feels like a pretty clear bug to me.

Breaking up into a myriad of small fields is another possible workaround, sure, but it would be a significant change in what the model is asked to produce compared to just putting a thumb on the scale at inference. Whether this would give better or worse results would require further testing.

another FYI: Chat Completions has a broken JSON response format on newer models. It will not enforce or encode the correct field order, which is quite problematic when it is a chain-of-thought. OpenAI has not addressed or fixed this prior bug report I made.

Hey _ama, welcome to the community! Thanks for sharing the GPT‑4.1 comparison and the workaround you found.

Microsoft’s Azure Structured Outputs guide lists pattern as unsupported for strings. Could you share the request ID and timestamp (with time zone) for one Azure call that stopped after the first tab, along with the model version shown for that deployment so that I can have a closer look?

Chirag

Do you work for Microsoft? Can you view Azure Cognitive Services AI model deployments by Foundry roles and inspect their per-call use? I suspect not. An epoch time of a call does not need a time zone.

If you wanted a reproduction call, I gave the shape of one in my linked completed demo share online. I suspect the AI responding in this forum cannot follow that link, though, it can only write hypothetical answers.

I can run the identical call again and provide you OpenAI’s version of the same fault. See “content” below so you can take affirmative action and push forward an investigation of the fault in regex patterns in later reasoning models.

A switch of the same call to gpt-4.1 results in success, as previously reported and as I replicate on OpenAI.

x-request-id: req_282f96b172b149ffb5427174278e710c

API call

POST https://api.openai.com/v1/chat/completions

{
  "model": "gpt-5.6-terra",
  "messages": [
    {
      "role": "developer",
      "content": [
        {
          "type": "text",
          "text": "You are an AI model that has one job: produce a structured output JSON response based on the user task.\n# schema test task - Reproduction only\nCopy the user's entire message verbatim into status. Preserve every character, including whitespace."
        }
      ]
    },
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "Heart\tP\n\nLungs\tF"
        }
      ]
    }
  ],
  "max_completion_tokens": 16384,
  "store": false,
  "prompt_cache_key": "myplayground",
  "safety_identifier": "myplayground",
  "moderation": {
    "model": "omni-moderation-latest",
    "policy": {
      "input": {
        "mode": "block"
      },
      "output": {
        "mode": "score"
      }
    }
  },
  "prompt_cache_options": {
    "mode": "explicit",
    "ttl": "30m"
  },
  "reasoning_effort": "low",
  "response_format": {
    "type": "json_schema",
    "json_schema": {
      "name": "simulator_self_test",
      "strict": true,
      "schema": {
        "type": "object",
        "description": "Status reported by a two-channel simulator.",
        "properties": {
          "status": {
            "type": "string",
            "pattern": "^Heart\t[^\t\n]\n\nLungs\t[^\t\n]$",
            "description": "The Heart and Lungs status lines, including their original tabs and intervening blank line."
          }
        },
        "required": [
          "status"
        ],
        "additionalProperties": false
      }
    }
  }
}

response

{
  "id": "chatcmpl-Eq3O24Abd8Tl6Usl7YadJfYfeteNf",
  "object": "chat.completion",
  "created": 1796074782,
  "model": "gpt-5.6-terra",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "{\"status\":\"Heart\\t\"}",
        "refusal": null,
        "annotations": []
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 121,
    "completion_tokens": 15,
    "total_tokens": 136,
    "prompt_tokens_details": {
      "cached_tokens": 0,
      "cache_write_tokens": 0,
      "audio_tokens": 0
    },
    "completion_tokens_details": {
      "reasoning_tokens": 0,
      "audio_tokens": 0,
      "accepted_prediction_tokens": 0,
      "rejected_prediction_tokens": 0
    }
  },
  "service_tier": "default",
  "moderation": {
    "input": {
      "type": "moderation_results",
      "model": "omni-moderation-latest",
      "results": [
        {
          "type": "moderation_result",
          "model": "omni-moderation-latest",
          "flagged": false,
          "categories": {
            "harassment": false,
            "harassment/threatening": false,
            "sexual": false,
            "hate": false,
            "hate/threatening": false,
            "illicit": false,
            "illicit/violent": false,
            "self-harm/intent": false,
            "self-harm/instructions": false,
            "self-harm": false,
            "sexual/minors": false,
            "violence": false,
            "violence/graphic": false
          },
          "category_scores": {
            "harassment": 0.0005881933939738148,
            "harassment/threatening": 0.000024156629828672456,
            "sexual": 0.00010162440410680168,
            "hate": 0.00001941131867789626,
            "hate/threatening": 8.939696304799168e-7,
            "illicit": 0.000021318756129065198,
            "illicit/violent": 0.00001941131867789626,
            "self-harm/intent": 0.0002484179442990331,
            "self-harm/instructions": 0.0002209901360927295,
            "self-harm": 0.0005146468316961163,
            "sexual/minors": 0.000009314593042575101,
            "violence": 0.01016894759352803,
            "violence/graphic": 0.0023996708618247643
          },
          "category_applied_input_types": {
            "harassment": [
              "text"
            ],
            "harassment/threatening": [
              "text"
            ],
            "sexual": [
              "text"
            ],
            "hate": [
              "text"
            ],
            "hate/threatening": [
              "text"
            ],
            "illicit": [
              "text"
            ],
            "illicit/violent": [
              "text"
            ],
            "self-harm/intent": [
              "text"
            ],
            "self-harm/instructions": [
              "text"
            ],
            "self-harm": [
              "text"
            ],
            "sexual/minors": [
              "text"
            ],
            "violence": [
              "text"
            ],
            "violence/graphic": [
              "text"
            ]
          }
        }
      ]
    },
    "output": {
      "type": "moderation_results",
      "model": "omni-moderation-latest",
      "results": [
        {
          "type": "moderation_result",
          "model": "omni-moderation-latest",
          "flagged": false,
          "categories": {
            "harassment": false,
            "harassment/threatening": false,
            "sexual": false,
            "hate": false,
            "hate/threatening": false,
            "illicit": false,
            "illicit/violent": false,
            "self-harm/intent": false,
            "self-harm/instructions": false,
            "self-harm": false,
            "sexual/minors": false,
            "violence": false,
            "violence/graphic": false
          },
          "category_scores": {
            "harassment": 0.000004198630823683514,
            "harassment/threatening": 2.561282210758673e-7,
            "sexual": 0.000013982207564732913,
            "hate": 0.0000020785170435063726,
            "hate/threatening": 4.450850519411503e-8,
            "illicit": 0.0000011478768127080353,
            "illicit/violent": 9.516251694168764e-7,
            "self-harm/intent": 0.00000201456908529364,
            "self-harm/instructions": 1.9333584585546466e-7,
            "self-harm": 0.0000034268490542555014,
            "sexual/minors": 5.77192003016332e-7,
            "violence": 0.000009610241549947397,
            "violence/graphic": 0.000003321419494792514
          },
          "category_applied_input_types": {
            "harassment": [
              "text"
            ],
            "harassment/threatening": [
              "text"
            ],
            "sexual": [
              "text"
            ],
            "hate": [
              "text"
            ],
            "hate/threatening": [
              "text"
            ],
            "illicit": [
              "text"
            ],
            "illicit/violent": [
              "text"
            ],
            "self-harm/intent": [
              "text"
            ],
            "self-harm/instructions": [
              "text"
            ],
            "self-harm": [
              "text"
            ],
            "sexual/minors": [
              "text"
            ],
            "violence": [
              "text"
            ],
            "violence/graphic": [
              "text"
            ]
          }
        }
      ]
    }
  },
  "system_fingerprint": null
}

Could gpt-4.1 work because “pattern” is not actually working? Let’s have it try to repeat back a message as pattern and see if freeform text can be written. Result: pattern regex is still followed:

Hi _j, Thanks for sharing the request ID and complete reproduction. I’ve escalated this case to our specialized team for further investigation.

Chirag