ollama - 💡(How to fix) Fix Gemma 4 json output is fenced with markdown backticks [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
ollama/ollama#15595Fetched 2026-04-16 06:35:53
View on GitHub
Comments
1
Participants
2
Timeline
3
Reactions
0
Author
Participants
Timeline (top)
commented ×1mentioned ×1subscribed ×1

Code Example

> const response = await ollama.chat({
>     model: "gemma4:e4b",
>     stream: false,
>     think: false,
>     keep_alive: '24h',
>     options: {
>       temperature: 0.8,
>     },
>     format: JSON.stringify(z.toJSONSchema(InvoiceSchema)),
>     messages: [
>       { role: "system", content: t.my_message_string, },
>       { role: "user", content: t.parse_template_string.replace('{context_str}', context), },
>     ]
>   });
>
RAW_BUFFERClick to expand / collapse

Using ollama 0.20.7:

With the js library:

const response = await ollama.chat({
    model: "gemma4:e4b",
    stream: false,
    think: false,
    keep_alive: '24h',
    options: {
      temperature: 0.8,
    },
    format: JSON.stringify(z.toJSONSchema(InvoiceSchema)),
    messages: [
      { role: "system", content: t.my_message_string, },
      { role: "user", content: t.parse_template_string.replace('{context_str}', context), },
    ]
  });

The response.message.content is <code>"```json\n{\n "json details": "elided"\n}\n```"</code> which has erroneous <code>```json</code> and <code>```</code> markers.

Originally posted by @CamJN in #15315

This is also true of gemma4:31b which as an additional problem doesn't follow the response schema.

extent analysis

TL;DR

The issue can be fixed by properly handling the response format to remove erroneous JSON markers.

Guidance

  • Verify that the format option in the ollama.chat function is correctly set to expect a JSON response without additional markers.
  • Check the JSON.stringify(z.toJSONSchema(InvoiceSchema)) line to ensure it's not introducing the erroneous markers.
  • Consider parsing the response content to remove the unwanted markers before processing it further.
  • Test with a different model, like gemma4:31b, to see if the issue persists and if the response schema is being followed.

Example

const responseBody = response.message.content;
const cleanedResponse = responseBody.replace(/\\```json/g, '').replace(/\\```/g, '');

Notes

The provided code snippet and issue description suggest that the problem might be related to how the response is being formatted or parsed. However, without more information about the ollama library or the z.toJSONSchema function, it's difficult to provide a more specific solution.

Recommendation

Apply workaround: The best course of action seems to be to manually clean the response content to remove the erroneous markers, as shown in the example, until a more permanent fix can be found or the library is updated to handle this correctly.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

ollama - 💡(How to fix) Fix Gemma 4 json output is fenced with markdown backticks [1 comments, 2 participants]