[GH-ISSUE #9696] Output adding extra backslashes #68387

Closed
opened 2026-05-04 13:43:52 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @Leahh02 on GitHub (Mar 12, 2025).
Original GitHub issue: https://github.com/ollama/ollama/issues/9696

What is the issue?

I'm using the ollama api with my container. I'm trying to get the model to respond in JSON format, but there are extra backslashes being added. I'm wondering if there's something going on with the output parsing.

When I run curl http://localhost:11434/api/generate -d '{"model": "llama3.3", "prompt": "What color is the sky at different times of the day? Respond using JSON", "format": "json", "stream": false}'
The response looks like:

"response":"{
 \"times_of_day\": [
 {
 \"time\": \"Sunrise\",
 \"sky_color\": \"Orange/Pink\"
 },
 {
 \"time\": \"Morning\",
 \"sky_color\": \"Blue\"
 },
 {
 \"time\": \"Noon\",
 \"sky_color\": \"Bright Blue\"
 },
 {
 \"time\": \"Afternoon\",
 \"sky_color\": \"Light Blue\"
 },
 {
 \"time\": \"Sunset\",
 \"sky_color\": \"Orange/Red/Pink\"
 },
 {
 \"time\": \"Dusk\",
 \"sky_color\": \"Purple/Blue\"
 },
 {
 \"time\": \"Night\",
 \"sky_color\": \"Dark Blue/Black\"
 }
 ]
}"

I also tried some prompt engineering:
curl http://localhost:11434/api/generate -d '{"model": "llama3.3", "prompt": "What color is the sky at different times of the day? Return a valid JSON object with no additional escaping or wrapping.", "format": "json", "stream": false}'
And still had no luck, my response looked like:

"response":"{
 \"dawn\": \"pink/orange\",
 \"sunrise\": \"orange/red\",
 \"morning\": \"blue\",
 \"noon\": \"bright blue\",
 \"afternoon\": \"light blue\",
 \"sunset\": \"orange/pink\",
 \"dusk\": \"purple/blue\",
 \"night\": \"black/dark blue\"
}"

I'm expecting it to look like the output from the example here.

{
  "morning": {
    "color": "blue"
  },
  "noon": {
    "color": "blue-gray"
  },
  "afternoon": {
    "color": "warm gray"
  },
  "evening": {
    "color": "orange"
  }
}

Relevant log output


OS

No response

GPU

No response

CPU

No response

Ollama version

0.5.13

Originally created by @Leahh02 on GitHub (Mar 12, 2025). Original GitHub issue: https://github.com/ollama/ollama/issues/9696 ### What is the issue? I'm using the ollama api with my container. I'm trying to get the model to respond in JSON format, but there are extra backslashes being added. **I'm wondering if there's something going on with the output parsing.** When I run `curl http://localhost:11434/api/generate -d '{"model": "llama3.3", "prompt": "What color is the sky at different times of the day? Respond using JSON", "format": "json", "stream": false}'` The response looks like: ``` "response":"{ \"times_of_day\": [ { \"time\": \"Sunrise\", \"sky_color\": \"Orange/Pink\" }, { \"time\": \"Morning\", \"sky_color\": \"Blue\" }, { \"time\": \"Noon\", \"sky_color\": \"Bright Blue\" }, { \"time\": \"Afternoon\", \"sky_color\": \"Light Blue\" }, { \"time\": \"Sunset\", \"sky_color\": \"Orange/Red/Pink\" }, { \"time\": \"Dusk\", \"sky_color\": \"Purple/Blue\" }, { \"time\": \"Night\", \"sky_color\": \"Dark Blue/Black\" } ] }" ``` I also tried some prompt engineering: `curl http://localhost:11434/api/generate -d '{"model": "llama3.3", "prompt": "What color is the sky at different times of the day? Return a valid JSON object with no additional escaping or wrapping.", "format": "json", "stream": false}'` And still had no luck, my response looked like: ``` "response":"{ \"dawn\": \"pink/orange\", \"sunrise\": \"orange/red\", \"morning\": \"blue\", \"noon\": \"bright blue\", \"afternoon\": \"light blue\", \"sunset\": \"orange/pink\", \"dusk\": \"purple/blue\", \"night\": \"black/dark blue\" }" ``` I'm expecting it to look like the output from the example [here](https://github.com/ollama/ollama/blob/main/docs/api.md#request-4). ``` { "morning": { "color": "blue" }, "noon": { "color": "blue-gray" }, "afternoon": { "color": "warm gray" }, "evening": { "color": "orange" } } ``` ### Relevant log output ```shell ``` ### OS _No response_ ### GPU _No response_ ### CPU _No response_ ### Ollama version 0.5.13
GiteaMirror added the question label 2026-05-04 13:43:52 -05:00
Author
Owner

@rick-github commented on GitHub (Mar 12, 2025):

The quotes are escaped with backslashes because they are inside a string.

{"response":"{ \"key\": \"value\" }" }

If you de-reference the response field the backslashes are interpreted:

$ python -c 'out={"response":"{ \"key\": \"value\" }" }; print(out["response"])'
{ "key": "value" }

You can use jq to de-ref the response field in the curl output:

$ curl -s http://localhost:11434/api/generate -d '{"model": "llama3.2", "prompt": "What color is the sky at different times of the day? Respond using JSON", "format": "json", "stream": false}' | jq -r .response
{"times": ["sunset", "dusk", "daytime", "twilight", "night"], "colors": [
    {"time": "sunset", "color": "Orange-Pink"},
    {"time": "dusk", "color": "Light Purple-Blue"},
    {"time": "daytime", "color": "Blue"},
    {"time": "twilight", "color": "Pink-Purple"},
    {"time": "night", "color": "Dark Blue"}
]}
<!-- gh-comment-id:2718669019 --> @rick-github commented on GitHub (Mar 12, 2025): The quotes are escaped with backslashes because they are inside a string. ``` {"response":"{ \"key\": \"value\" }" } ``` If you de-reference the `response` field the backslashes are interpreted: ```console $ python -c 'out={"response":"{ \"key\": \"value\" }" }; print(out["response"])' { "key": "value" } ``` You can use `jq` to de-ref the `response` field in the `curl` output: ```console $ curl -s http://localhost:11434/api/generate -d '{"model": "llama3.2", "prompt": "What color is the sky at different times of the day? Respond using JSON", "format": "json", "stream": false}' | jq -r .response {"times": ["sunset", "dusk", "daytime", "twilight", "night"], "colors": [ {"time": "sunset", "color": "Orange-Pink"}, {"time": "dusk", "color": "Light Purple-Blue"}, {"time": "daytime", "color": "Blue"}, {"time": "twilight", "color": "Pink-Purple"}, {"time": "night", "color": "Dark Blue"} ]} ```
Author
Owner

@pdevine commented on GitHub (Mar 12, 2025):

Going to close the issue as answered (thanks @rick-github !)

<!-- gh-comment-id:2718822017 --> @pdevine commented on GitHub (Mar 12, 2025): Going to close the issue as answered (thanks @rick-github !)
Author
Owner

@Leahh02 commented on GitHub (Mar 12, 2025):

Going to close the issue as answered (thanks @rick-github !)

Wait, sorry I have another question on this.

@rick-github so when I was printing out the representation of the string I got from the curl requests it looked like

{"response":"{ \\"key\\": \\"value\\" }" }

and then the printed version looked like

{"response":"{ \"key\": \"value\" }" }

So one pair of the backslashes seemed like it was being used to escape the quotes, and the other one seemed unnecessary.

What you suggested did print it out with no backslashes, but now the new lines and possible indentations have been lost. For example this is what was printed for me:

{ "dawn": "pink/orange", "sunrise": "orange/red", "morning": "blue", "noon": "bright blue", "afternoon": "blue", "sunset": "orange/pink", "dusk": "purple/blue", "night": "black/dark blue" }

Thank you for your help as I'm learning more about this stuff.

<!-- gh-comment-id:2718844982 --> @Leahh02 commented on GitHub (Mar 12, 2025): > Going to close the issue as answered (thanks [@rick-github](https://github.com/rick-github) !) Wait, sorry I have another question on this. @rick-github so when I was printing out the representation of the string I got from the curl requests it looked like ``` {"response":"{ \\"key\\": \\"value\\" }" } ``` and then the printed version looked like ``` {"response":"{ \"key\": \"value\" }" } ``` So one pair of the backslashes seemed like it was being used to escape the quotes, and the other one seemed unnecessary. What you suggested did print it out with no backslashes, but now the new lines and possible indentations have been lost. For example this is what was printed for me: ``` { "dawn": "pink/orange", "sunrise": "orange/red", "morning": "blue", "noon": "bright blue", "afternoon": "blue", "sunset": "orange/pink", "dusk": "purple/blue", "night": "black/dark blue" } ``` Thank you for your help as I'm learning more about this stuff.
Author
Owner

@rick-github commented on GitHub (Mar 12, 2025):

If you want to pretty-print the output, run it through jq again. The first jq extracts the field and processes escaped characters, and second formats it nicely.

$ curl -s http://localhost:11434/api/generate -d '{"model": "llama3.2", "prompt": "What color is the sky at different times of the day? Respond using JSON", "format": "json", "stream": false}' | jq -r .response | jq 
{
  "times": [
    {
      "time": "Sunrise",
      "color": "Soft Pink"
    },
    {
      "time": "Morning",
      "color": "Light Blue"
    },
    {
      "time": "Daytime",
      "color": "Blue"
    },
    {
      "time": "Afternoon",
      "color": "Light Blue"
    },
    {
      "time": "Evening",
      "color": "Soft Orange-Pink"
    },
    {
      "time": "Sunset",
      "color": "Deep Orange-Red"
    }
  ]
}
<!-- gh-comment-id:2718900870 --> @rick-github commented on GitHub (Mar 12, 2025): If you want to pretty-print the output, run it through `jq` again. The first `jq` extracts the field and processes escaped characters, and second formats it nicely. ```console $ curl -s http://localhost:11434/api/generate -d '{"model": "llama3.2", "prompt": "What color is the sky at different times of the day? Respond using JSON", "format": "json", "stream": false}' | jq -r .response | jq { "times": [ { "time": "Sunrise", "color": "Soft Pink" }, { "time": "Morning", "color": "Light Blue" }, { "time": "Daytime", "color": "Blue" }, { "time": "Afternoon", "color": "Light Blue" }, { "time": "Evening", "color": "Soft Orange-Pink" }, { "time": "Sunset", "color": "Deep Orange-Red" } ] } ```
Author
Owner

@Leahh02 commented on GitHub (Mar 12, 2025):

If you want to pretty-print the output, run it through jq again. The first jq extracts the field and processes escaped characters, and second formats it nicely.

$ curl -s http://localhost:11434/api/generate -d '{"model": "llama3.2", "prompt": "What color is the sky at different times of the day? Respond using JSON", "format": "json", "stream": false}' | jq -r .response | jq
{
"times": [
{
"time": "Sunrise",
"color": "Soft Pink"
},
{
"time": "Morning",
"color": "Light Blue"
},
{
"time": "Daytime",
"color": "Blue"
},
{
"time": "Afternoon",
"color": "Light Blue"
},
{
"time": "Evening",
"color": "Soft Orange-Pink"
},
{
"time": "Sunset",
"color": "Deep Orange-Red"
}
]
}

Ah that works, thank you!

<!-- gh-comment-id:2718969759 --> @Leahh02 commented on GitHub (Mar 12, 2025): > If you want to pretty-print the output, run it through `jq` again. The first `jq` extracts the field and processes escaped characters, and second formats it nicely. > > $ curl -s http://localhost:11434/api/generate -d '{"model": "llama3.2", "prompt": "What color is the sky at different times of the day? Respond using JSON", "format": "json", "stream": false}' | jq -r .response | jq > { > "times": [ > { > "time": "Sunrise", > "color": "Soft Pink" > }, > { > "time": "Morning", > "color": "Light Blue" > }, > { > "time": "Daytime", > "color": "Blue" > }, > { > "time": "Afternoon", > "color": "Light Blue" > }, > { > "time": "Evening", > "color": "Soft Orange-Pink" > }, > { > "time": "Sunset", > "color": "Deep Orange-Red" > } > ] > } Ah that works, thank you!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#68387