Skip to content

[Go] Models wrap JSON in backticks breaking structured output parsing #4679

@dmeremyanin

Description

@dmeremyanin

Describe the bug
When using structured output with output.format: json in dotprompt files, the model sometimes wraps the JSON response in markdown code blocks (```json), which causes JSON parsing to fail.

Error message:

data is not valid JSON: invalid character '`' looking for beginning of value

To Reproduce

  1. Create a dotprompt file with structured output:
---
input:
  schema: MyInput
output:
  format: json
  schema: MyOutput
---
Your prompt text here
  1. Execute the prompt with prompt.Execute(ctx, input).
  2. The model occasionally returns the JSON wrapped in backticks:
```json
{"field": "value"}

instead of raw JSON:

{"field": "value"}
  1. This causes JSON unmarshaling to fail.

Expected behavior
The model should return raw JSON without markdown formatting when output.format: json is specified.

Screenshots

Image Image

Runtime (please complete the following information):

  • OS: macOS
  • Version: 15.7.3

Go version:

  • go1.26.0 darwin/arm64

Additional context
I'm using Go SDK, but the problem likely affects TS and Python SDKs as well, since they all share the same JSON output instructions.

This behavior was observed with the openai/gpt-4o-mini model. Adding an explicit instruction at the end of the prompt fixes the issue:

Return ONLY raw JSON. Do NOT wrap the response in backticks.

Probably, Genkit should add this instruction automatically.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggo

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions