Decode structured output from a code fence with unpaired backticks

Merged
AN
7d ago
+63-0

When a model breaks the backticks of a code fence, decodeStructuredOutput() fails every fallback and returns [], so $response->structured comes back empty even though $response->text holds a valid object.

z-ai/glm-5.3-flash does this in response_format: json_object mode (we call it through OpenRouter), in about 4–7% of replies in our runs. The shapes we see, with values simplified:

``{"ok":true}

{"ok":true}``

``{"ok":true}``

{"ok":true}
``{/* asked for the rest */}

They look like a ```json fence that lost part of its delimiters; GLM drops the token json in JSON mode (zai-org/GLM-5#133). None of them is a complete fence, so extractJsonCodeFence() doesn't match, and the other fallbacks fail too.

The decoder now has one more fallback after the three existing ones. It strips the backticks at the start of the payload, and a json tag right after them, then decodes the rest as a whole, or the text before the first or the last remaining pair of backticks.

// before: $response->structured === []
// after:  ['ok' => true]

The new fallback only runs when the payload already failed to decode, so valid JSON, complete fences and the key-quote repair behave exactly as before. Each candidate has to decode as a whole, so it never picks an object out of surrounding text: two objects in a row, an object followed by prose, or an example before the answer still return []. Anything after the closing backticks is ignored, as it already is after a complete fence. Trying the last pair too keeps backticks inside string values intact. It uses no regex, so a large or whitespace-heavy payload can't run into pcre.backtrack_limit.

Added decoder tests for each shape, an unclosed fence, and backticks inside a string value, all of which fail on 1.x, plus one that text around an object still decodes to []. I also ran the decoder against 700k random payloads built from backticks, braces, prose and UTF-8: no result other than the whole text between the stray backticks, and no change for any payload that decodes today.