[GH-ISSUE #14377] hallucination timeout #9344

Closed
opened 2026-04-12 22:12:33 -05:00 by GiteaMirror · 6 comments
Owner

Originally created by @Abdulrahman392011 on GitHub (Feb 23, 2026).
Original GitHub issue: https://github.com/ollama/ollama/issues/14377

some small models start generating random stuff indefinitely. i know that i can use request library to create a timeout. however it seems reasonable to ask for a feature like this to be natively active within ollama. the model will run forever, literally. instead it would be much better if the model just reply with timeout after two minutes or something like that. and again it should be easy to set by the user.

Originally created by @Abdulrahman392011 on GitHub (Feb 23, 2026). Original GitHub issue: https://github.com/ollama/ollama/issues/14377 some small models start generating random stuff indefinitely. i know that i can use request library to create a timeout. however it seems reasonable to ask for a feature like this to be natively active within ollama. the model will run forever, literally. instead it would be much better if the model just reply with timeout after two minutes or something like that. and again it should be easy to set by the user.
GiteaMirror added the feature request label 2026-04-12 22:12:33 -05:00
Author
Owner

@rick-github commented on GitHub (Feb 23, 2026):

num_predict

<!-- gh-comment-id:3946901282 --> @rick-github commented on GitHub (Feb 23, 2026): [`num_predict`](https://github.com/ollama/ollama/blob/main/docs/modelfile.mdx#valid-parameters-and-values:~:text=stop%20%22AI%20assistant%3A%22-,num_predict,-Maximum%20number%20of)
Author
Owner

@Abdulrahman392011 commented on GitHub (Feb 23, 2026):

thanks @rick-github , always helpful.

<!-- gh-comment-id:3946935710 --> @Abdulrahman392011 commented on GitHub (Feb 23, 2026): thanks @rick-github , always helpful.
Author
Owner

@Abdulrahman392011 commented on GitHub (Feb 23, 2026):

hallucinate.py

@rick-github it seems there is something wrong with num_predict and using a json format schema.
take a look at the file hallucinate.py add 'num_predict' : 50 in the options dictionary and it will through an error.

should i open a different issue under a different name?

<!-- gh-comment-id:3947112929 --> @Abdulrahman392011 commented on GitHub (Feb 23, 2026): [hallucinate.py](https://github.com/user-attachments/files/25499370/hallucinate.py) @rick-github it seems there is something wrong with num_predict and using a json format schema. take a look at the file hallucinate.py add 'num_predict' : 50 in the options dictionary and it will through an error. should i open a different issue under a different name?
Author
Owner

@rick-github commented on GitHub (Feb 23, 2026):

num_predict stops token generation when the limit is reached. If the JSON output hasn't completed it will be truncated, leading to a validation failure.

<!-- gh-comment-id:3947142493 --> @rick-github commented on GitHub (Feb 23, 2026): `num_predict` stops token generation when the limit is reached. If the JSON output hasn't completed it will be truncated, leading to a validation failure.
Author
Owner

@rick-github commented on GitHub (Feb 23, 2026):

--- hallucinate.py.orig	2026-02-23 21:38:37.680138079 +0100
+++ hallucinate.py	2026-02-23 21:38:28.368189496 +0100
@@ -2,7 +2,8 @@
 import ast
 from pathlib import Path
 import ollama
-from pydantic import BaseModel , create_model
+from typing import Annotated
+from pydantic import BaseModel , create_model, StringConstraints
 
 model_name = 'granite3.1-moe:1b-instruct-fp16'
 
@@ -17,7 +18,7 @@
             ]
 
 class emotional_tasks(BaseModel):
-    emotional_tasks_about_family : list[str]
+    emotional_tasks_about_family : list[Annotated[str, StringConstraints(max_length=50)]]
$ python3 ./hallucinate.py 
emotional_tasks_about_family=["Passion for Sharing Messages: The user's family is"]
<!-- gh-comment-id:3947225281 --> @rick-github commented on GitHub (Feb 23, 2026): ```diff --- hallucinate.py.orig 2026-02-23 21:38:37.680138079 +0100 +++ hallucinate.py 2026-02-23 21:38:28.368189496 +0100 @@ -2,7 +2,8 @@ import ast from pathlib import Path import ollama -from pydantic import BaseModel , create_model +from typing import Annotated +from pydantic import BaseModel , create_model, StringConstraints model_name = 'granite3.1-moe:1b-instruct-fp16' @@ -17,7 +18,7 @@ ] class emotional_tasks(BaseModel): - emotional_tasks_about_family : list[str] + emotional_tasks_about_family : list[Annotated[str, StringConstraints(max_length=50)]] ``` ```console $ python3 ./hallucinate.py emotional_tasks_about_family=["Passion for Sharing Messages: The user's family is"] ```
Author
Owner

@Abdulrahman392011 commented on GitHub (Feb 23, 2026):

you're right, when i set the code not to hallucinate and set the num_predict : 1000 it works and it doesn't throw the error.

Thanks @rick-github for clearing the confusion. i will make sure to use with try and except.

<!-- gh-comment-id:3947248219 --> @Abdulrahman392011 commented on GitHub (Feb 23, 2026): you're right, when i set the code not to hallucinate and set the num_predict : 1000 it works and it doesn't throw the error. Thanks @rick-github for clearing the confusion. i will make sure to use with try and except.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#9344