[GH-ISSUE #798] JSON Marshal Escapes Special Characters in Prompts #46895

Closed
opened 2026-04-28 01:50:01 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @deichbewohner on GitHub (Oct 16, 2023).
Original GitHub issue: https://github.com/ollama/ollama/issues/798

When using the json.Marshal() function in llama.go, I've noticed that special characters like < and > are being automatically escaped to \u003c and \u003e, respectively. This is problematic, especially for prompts that use these characters.

Example:
Consider the following prompt:

<|system|>
</s>
<|user|>
Hi</s>
<|assistant|>

Location of the Issue:
The line responsible for this behavior is located here.

data, err := json.Marshal(predReq)

Proposed Solution:
I am planning to submit a pull request to address this issue, ensuring that special characters in templates remain unescaped when marshaled into JSON.

Originally created by @deichbewohner on GitHub (Oct 16, 2023). Original GitHub issue: https://github.com/ollama/ollama/issues/798 When using the `json.Marshal()` function in `llama.go`, I've noticed that special characters like `<` and `>` are being automatically escaped to `\u003c` and `\u003e`, respectively. This is problematic, especially for prompts that use these characters. **Example:** Consider the following prompt: ``` <|system|> </s> <|user|> Hi</s> <|assistant|> ``` **Location of the Issue:** The line responsible for this behavior is located [here](https://github.com/jmorganca/ollama/blob/06bcfbd6295b0aa0b4a63b6bd6731c0995f0802d/llm/llama.go#L547). ```go data, err := json.Marshal(predReq) ``` **Proposed Solution:** I am planning to submit a pull request to address this issue, ensuring that special characters in templates remain unescaped when marshaled into JSON.
Author
Owner

@deichbewohner commented on GitHub (Oct 16, 2023):

For better clarity, here's the output when I log string(data) immediately after the marshaling:

{"prompt":"\u003c|im_start|\u003esystem\nYou are a helpful assistant.\u003c|im_end|\u003e\n\u003c|im_start|\u003euser\nHi\u003c|im_end|\u003e\n\u003c|im_start|\u003eassistant\n","stream":true,"n_predict":500,"n_keep":20,"temperature":0.8,"top_k":40,"top_p":0.9,"tfs_z":1,"typical_p":1,"repeat_last_n":64,"repeat_penalty":1.1,"presence_penalty":0,"frequency_penalty":0,"mirostat":0,"mirostat_tau":5,"mirostat_eta":0.1,"penalize_nl":true,"seed":-1,"stop":["\u003c|im_end|\u003e","\u003c|im_start|\u003e"]}

Used template:

TEMPLATE """{{- if .First }}<|im_start|>system
{{ .System }}<|im_end|>{{- end }}
<|im_start|>user
{{ .Prompt }}<|im_end|>
<|im_start|>assistant
"""
<!-- gh-comment-id:1764216040 --> @deichbewohner commented on GitHub (Oct 16, 2023): For better clarity, here's the output when I log `string(data)` immediately after the marshaling: ``` {"prompt":"\u003c|im_start|\u003esystem\nYou are a helpful assistant.\u003c|im_end|\u003e\n\u003c|im_start|\u003euser\nHi\u003c|im_end|\u003e\n\u003c|im_start|\u003eassistant\n","stream":true,"n_predict":500,"n_keep":20,"temperature":0.8,"top_k":40,"top_p":0.9,"tfs_z":1,"typical_p":1,"repeat_last_n":64,"repeat_penalty":1.1,"presence_penalty":0,"frequency_penalty":0,"mirostat":0,"mirostat_tau":5,"mirostat_eta":0.1,"penalize_nl":true,"seed":-1,"stop":["\u003c|im_end|\u003e","\u003c|im_start|\u003e"]} ``` Used template: ``` TEMPLATE """{{- if .First }}<|im_start|>system {{ .System }}<|im_end|>{{- end }} <|im_start|>user {{ .Prompt }}<|im_end|> <|im_start|>assistant """ ```
Author
Owner

@deichbewohner commented on GitHub (Oct 17, 2023):

This issue has been resolved and the fix is now merged into the main branch via PR #799 . Thanks.

<!-- gh-comment-id:1766771753 --> @deichbewohner commented on GitHub (Oct 17, 2023): This issue has been resolved and the fix is now merged into the `main` branch via PR #799 . Thanks.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#46895