[GH-ISSUE #2905] Sometimes no http response is received from the ollama-rest-api /chat #63818

Closed
opened 2026-05-03 15:04:42 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @CtrlAiDel on GitHub (Mar 4, 2024).
Original GitHub issue: https://github.com/ollama/ollama/issues/2905

Originally assigned to: @BruceMacD on GitHub.

With ollama-rest-api /chat {format: 'json'}
sometimes no http response is received.

{
    "model": "llama2",
    "stream": false,
    "format": "json",
    "messages": [
        {
            "role": "user",
            "content": "Rose are red"
        },
        {
            "role": "assistant",
            "content": "{\"Rose are red, violets are blue,\"\n    :\n\"Sugar is sweet, and so are you!\"}"
        },
        {
            "role": "user",
            "content": "who wrote that poem ?"
        }
    ]
}

The third and last log are manually canceled request. We can see that they are not in the postman logs..

image

It might be code related. It's working for us more than half the time, and then other let's say 20-30% of the times, we don't have anything in the logs and no response.

Thank you for the amazing project!

Originally created by @CtrlAiDel on GitHub (Mar 4, 2024). Original GitHub issue: https://github.com/ollama/ollama/issues/2905 Originally assigned to: @BruceMacD on GitHub. With ollama-rest-api /chat {format: 'json'} sometimes no http response is received. ``` Json { "model": "llama2", "stream": false, "format": "json", "messages": [ { "role": "user", "content": "Rose are red" }, { "role": "assistant", "content": "{\"Rose are red, violets are blue,\"\n :\n\"Sugar is sweet, and so are you!\"}" }, { "role": "user", "content": "who wrote that poem ?" } ] } ``` The third and last log are manually canceled request. We can see that they are not in the postman logs.. ![image](https://github.com/ollama/ollama/assets/40178706/78095e88-c70d-447d-8e29-e11353550a82) It might be code related. It's working for us more than half the time, and then other let's say 20-30% of the times, we don't have anything in the logs and no response. Thank you for the amazing project!
Author
Owner

@BruceMacD commented on GitHub (Mar 4, 2024):

Hi @CtrlAiDel, this is a pretty common problem. The issue is the prompt, try adding a system message to respond in JSON.

{
    "model": "llama2",
    "stream": false,
    "format": "json",
    "messages": [
        {
            "role": "system",
            "content": "Respond only in JSON format."
        },
        {
            "role": "user",
            "content": "Rose are red"
        },
        {
            "role": "assistant",
            "content": "{\"Rose are red, violets are blue,\"\n    :\n\"Sugar is sweet, and so are you!\"}"
        },
        {
            "role": "user",
            "content": "who wrote that poem ?"
        }
    ]
}

The LLM needs to be told to respond in JSON since the format flag only filters the output. Let me know if that helps.

<!-- gh-comment-id:1975556713 --> @BruceMacD commented on GitHub (Mar 4, 2024): Hi @CtrlAiDel, this is a pretty common problem. The issue is the prompt, try adding a system message to respond in JSON. ```json { "model": "llama2", "stream": false, "format": "json", "messages": [ { "role": "system", "content": "Respond only in JSON format." }, { "role": "user", "content": "Rose are red" }, { "role": "assistant", "content": "{\"Rose are red, violets are blue,\"\n :\n\"Sugar is sweet, and so are you!\"}" }, { "role": "user", "content": "who wrote that poem ?" } ] } ``` The LLM needs to be told to respond in JSON since the `format` flag only filters the output. Let me know if that helps.
Author
Owner

@CtrlAiDel commented on GitHub (Mar 4, 2024):

Ok, my friend/dev get what you mean. But he's asking why sometimes there are no problem at all. Also could you maybe give us an exemple of the solution that you are suggesting us please?

Thanks again, it's really appreciated

<!-- gh-comment-id:1975565107 --> @CtrlAiDel commented on GitHub (Mar 4, 2024): Ok, my friend/dev get what you mean. But he's asking why sometimes there are no problem at all. Also could you maybe give us an exemple of the solution that you are suggesting us please? Thanks again, it's really appreciated
Author
Owner

@marklysze commented on GitHub (Mar 9, 2024):

I am also experiencing the same issue. If I run with "format" : "json" then it hangs with Mistral (and many others). If I remove that it runs fine.

This always runs okay first time but not subsequent times:
curl -X POST http://localhost:11434/api/chat -d @curl_command.txt

curl_command.txt:

  "model": "mistral:instruct",
  "messages": [
    {
      "content": "For currency exchange tasks, \n only use the functions you have been provided with. \n Output 'BAZINGA!' when an answer has been provided. \n Do not include the function name or result in the JSON.\n Example of the return JSON is: \n {\n \"parameter_1_name\": 100.00,\n \"parameter_2_name\": \"ABC\",\n \"parameter_3_name\": \"DEF\"\n }. \n Another example of the return JSON is: \n {\n \"parameter_1_name\": \"GHI\",\n \"parameter_2_name\": \"ABC\",\n \"parameter_3_name\": \"DEF\",\n \"parameter_4_name\": 123.00\n }. Produce JSON OUTPUT ONLY! The following functions are available to you:\n{\"type\": \"function\", \"function\": {\"description\": \"Currency exchange calculator.\", \"name\": \"currency_calculator\", \"parameters\": {\"type\": \"object\", \"properties\": {\"base_amount\": {\"type\": \"number\", \"description\": \"Amount of currency in base_currency\"}, \"base_currency\": {\"enum\": [\"USD\", \"EUR\"], \"type\": \"string\", \"default\": \"USD\", \"description\": \"Base currency\"}, \"quote_currency\": {\"enum\": [\"USD\", \"EUR\"], \"type\": \"string\", \"default\": \"EUR\", \"description\": \"Quote currency\"}}, \"required\": [\"base_amount\"]}}}",
      "role": "system"
    },
    {
      "content": "How much is 123.45 EUR in USD?",
      "role": "user"
    }
  ],
  "options": {},
  "stream": false,
  "format": "json"
}```

If I remove the `"format" : "json"` then I can run that command continuously without issue.
<!-- gh-comment-id:1986761141 --> @marklysze commented on GitHub (Mar 9, 2024): I am also experiencing the same issue. If I run with `"format" : "json"` then it hangs with Mistral (and many others). If I remove that it runs fine. This always runs okay first time but not subsequent times: `curl -X POST http://localhost:11434/api/chat -d @curl_command.txt` curl_command.txt: ```{ "model": "mistral:instruct", "messages": [ { "content": "For currency exchange tasks, \n only use the functions you have been provided with. \n Output 'BAZINGA!' when an answer has been provided. \n Do not include the function name or result in the JSON.\n Example of the return JSON is: \n {\n \"parameter_1_name\": 100.00,\n \"parameter_2_name\": \"ABC\",\n \"parameter_3_name\": \"DEF\"\n }. \n Another example of the return JSON is: \n {\n \"parameter_1_name\": \"GHI\",\n \"parameter_2_name\": \"ABC\",\n \"parameter_3_name\": \"DEF\",\n \"parameter_4_name\": 123.00\n }. Produce JSON OUTPUT ONLY! The following functions are available to you:\n{\"type\": \"function\", \"function\": {\"description\": \"Currency exchange calculator.\", \"name\": \"currency_calculator\", \"parameters\": {\"type\": \"object\", \"properties\": {\"base_amount\": {\"type\": \"number\", \"description\": \"Amount of currency in base_currency\"}, \"base_currency\": {\"enum\": [\"USD\", \"EUR\"], \"type\": \"string\", \"default\": \"USD\", \"description\": \"Base currency\"}, \"quote_currency\": {\"enum\": [\"USD\", \"EUR\"], \"type\": \"string\", \"default\": \"EUR\", \"description\": \"Quote currency\"}}, \"required\": [\"base_amount\"]}}}", "role": "system" }, { "content": "How much is 123.45 EUR in USD?", "role": "user" } ], "options": {}, "stream": false, "format": "json" }``` If I remove the `"format" : "json"` then I can run that command continuously without issue.
Author
Owner

@seanmavley commented on GitHub (Mar 9, 2024):

Related to issue #1910

<!-- gh-comment-id:1986842772 --> @seanmavley commented on GitHub (Mar 9, 2024): Related to issue #1910
Author
Owner

@jmorganca commented on GitHub (Mar 12, 2024):

Merging with https://github.com/ollama/ollama/issues/1910

<!-- gh-comment-id:1989747977 --> @jmorganca commented on GitHub (Mar 12, 2024): Merging with https://github.com/ollama/ollama/issues/1910
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#63818