[GH-ISSUE #10591] ollama doesn't support all sort of regex #69028

Closed
opened 2026-05-04 16:55:17 -05:00 by GiteaMirror · 9 comments
Owner

Originally created by @Ritanlisa on GitHub (May 6, 2025).
Original GitHub issue: https://github.com/ollama/ollama/issues/10591

What is the issue?

reges express \s and \S will not be transfered correctly
also, it cracks when got (?:ExpA|ExpB) Regex Expression

Relevant log output

parse: error parsing grammar: unknown escape at \S\s]* "\n<\/think>\n") "\"" space
space ::= | " " | "\n"{1,2} [ \t]{0,20}


root ::= "\"" ("<think>\n" [\S\s]* "\n<\/think>\n") "\"" space
space ::= | " " | "\n"{1,2} [ \t]{0,20}

llama_grammar_init_impl: failed to parse grammar

OS

Windows

GPU

Nvidia

CPU

Intel

Ollama version

0.6.6

Originally created by @Ritanlisa on GitHub (May 6, 2025). Original GitHub issue: https://github.com/ollama/ollama/issues/10591 ### What is the issue? reges express ```\s``` and ```\S``` will not be transfered correctly also, it cracks when got (?:ExpA|ExpB) Regex Expression ### Relevant log output ```shell parse: error parsing grammar: unknown escape at \S\s]* "\n<\/think>\n") "\"" space space ::= | " " | "\n"{1,2} [ \t]{0,20} root ::= "\"" ("<think>\n" [\S\s]* "\n<\/think>\n") "\"" space space ::= | " " | "\n"{1,2} [ \t]{0,20} llama_grammar_init_impl: failed to parse grammar ``` ### OS Windows ### GPU Nvidia ### CPU Intel ### Ollama version 0.6.6
GiteaMirror added the bug label 2026-05-04 16:55:17 -05:00
Author
Owner

@rick-github commented on GitHub (May 6, 2025):

Grammar parsing is done by llama.cpp, so probably better to file an issue over there.

<!-- gh-comment-id:2854898824 --> @rick-github commented on GitHub (May 6, 2025): Grammar parsing is done by llama.cpp, so probably better to file an issue [over there](https://github.com/ggml-org/llama.cpp/issues).
Author
Owner

@Ritanlisa commented on GitHub (May 7, 2025):

Grammar parsing is done by llama.cpp, so probably better to file an issue over there.

I created a issue there on this problem
However, the most important issue is that ollama crashed with only a extra word "Exception“ in the console(when launched by "ollama serve" command). I mean it might be a security problem, you can crash a server just by post a request with regex (?:ExpA|ExpB) format when they're using ollama to provide the service.

<!-- gh-comment-id:2856723609 --> @Ritanlisa commented on GitHub (May 7, 2025): > Grammar parsing is done by llama.cpp, so probably better to file an issue [over there](https://github.com/ggml-org/llama.cpp/issues). I created a issue there on this problem However, the most important issue is that ollama crashed with only a extra word "Exception“ in the console(when launched by "ollama serve" command). I mean it might be a security problem, you can crash a server just by post a request with regex (?:ExpA|ExpB) format when they're using ollama to provide the service.
Author
Owner

@rick-github commented on GitHub (May 7, 2025):

Example of post?

<!-- gh-comment-id:2857384562 --> @rick-github commented on GitHub (May 7, 2025): Example of post?
Author
Owner

@Ritanlisa commented on GitHub (May 7, 2025):

Example of post?

I used powershell to trigger this , bash version may work same

curl -X POST http://localhost:11434/api/chat -H @{'Content-Type': 'application/json'} -d '{
"model": "[llama3.1](deepseek-r1:8b)",
"messages": [{"role": "user", "content": "Tell me about Canada."}],
"stream": false,
"format": {
"type": "string",
"pattern": "^(?:[0-9 ]|Canada)+$"}
}'
Invoke-WebRequest -Uri "http://localhost:11434/api/chat" `
-Method POST `
-Headers @{ "Content-Type" = "application/json" } `
-Body (@{
    "model"    = "deepseek-r1:8b"
    "messages" = @(
        @{
            "role"    = "user"
            "content" = "Tell me about Canada."
        }
    )
    "stream"   = $false
    "format"   = @{
        "type"    = "string"
        "pattern" = "^(?:[0-9 ]|Canada)+$"
    }
} | ConvertTo-Json -Depth 5)
<!-- gh-comment-id:2858296956 --> @Ritanlisa commented on GitHub (May 7, 2025): > Example of post? ### I used powershell to trigger this , bash version may work same ```bash curl -X POST http://localhost:11434/api/chat -H @{'Content-Type': 'application/json'} -d '{ "model": "[llama3.1](deepseek-r1:8b)", "messages": [{"role": "user", "content": "Tell me about Canada."}], "stream": false, "format": { "type": "string", "pattern": "^(?:[0-9 ]|Canada)+$"} }' ``` ```powershell Invoke-WebRequest -Uri "http://localhost:11434/api/chat" ` -Method POST ` -Headers @{ "Content-Type" = "application/json" } ` -Body (@{ "model" = "deepseek-r1:8b" "messages" = @( @{ "role" = "user" "content" = "Tell me about Canada." } ) "stream" = $false "format" = @{ "type" = "string" "pattern" = "^(?:[0-9 ]|Canada)+$" } } | ConvertTo-Json -Depth 5) ```
Author
Owner

@Ritanlisa commented on GitHub (May 7, 2025):

i upgrade my ollama version to 0.6.8 now to fix another issue, this bug still unfixed in 0.6.8

<!-- gh-comment-id:2858300430 --> @Ritanlisa commented on GitHub (May 7, 2025): i upgrade my ollama version to 0.6.8 now to fix another issue, this bug still unfixed in 0.6.8
Author
Owner

@rick-github commented on GitHub (May 8, 2025):

https://github.com/ggml-org/llama.cpp/pull/13391 for the alternation issue. Can you provide an example of a post that causes the "unknown escape" error?

<!-- gh-comment-id:2864288865 --> @rick-github commented on GitHub (May 8, 2025): https://github.com/ggml-org/llama.cpp/pull/13391 for the alternation issue. Can you provide an example of a post that causes the "unknown escape" error?
Author
Owner

@Ritanlisa commented on GitHub (May 15, 2025):

the "unknown escape" is just caused by regex expression \s and \S (also \d )
the engine cannot treat these backslash expressions correctly
\s , \S and \d normally could be a valid simplify of expression [ \t\n\r\f\v] , [^ \t\n\r\f\v] and [0-9]
i think it might also be a problem of llama.cpp

<!-- gh-comment-id:2881925738 --> @Ritanlisa commented on GitHub (May 15, 2025): the "unknown escape" is just caused by regex expression ```\s``` and ```\S``` (also ```\d``` ) the engine cannot treat these backslash expressions correctly ```\s``` , ```\S``` and ```\d``` normally could be a valid simplify of expression ```[ \t\n\r\f\v]``` , ```[^ \t\n\r\f\v]``` and ```[0-9]``` i think it might also be a problem of llama.cpp
Author
Owner

@Ritanlisa commented on GitHub (May 15, 2025):

maybe the engine treat this blackslash as an escape signal for something as \| , to make this part of expression work as normal character | rather than a grammar expression to give a option to select, but the common letter do not need to be escape so that the engine raise a error

<!-- gh-comment-id:2881925832 --> @Ritanlisa commented on GitHub (May 15, 2025): maybe the engine treat this blackslash as an escape signal for something as ```\|``` , to make this part of expression work as normal character ```|``` rather than a grammar expression to give a option to select, but the common letter do not need to be escape so that the engine raise a error
Author
Owner

@rick-github commented on GitHub (May 15, 2025):

\s and \S are not supported expressions in llama.cpp. Supported expressions differ between implementations.

<!-- gh-comment-id:2881955297 --> @rick-github commented on GitHub (May 15, 2025): \s and \S are not supported expressions in llama.cpp. Supported expressions differ between implementations.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#69028