feat(ollama): add max_retries to chat generator#2899
Open
Keyur-S-Patel wants to merge 1 commit intodeepset-ai:mainfrom
Open
feat(ollama): add max_retries to chat generator#2899Keyur-S-Patel wants to merge 1 commit intodeepset-ai:mainfrom
Keyur-S-Patel wants to merge 1 commit intodeepset-ai:mainfrom
Conversation
ae3c423 to
83278a1
Compare
Contributor
Author
Keyur-S-Patel
commented
Mar 3, 2026
Contributor
Author
Keyur-S-Patel
left a comment
There was a problem hiding this comment.
Similar to this one #2875
bogdankostic
requested changes
Mar 5, 2026
Contributor
bogdankostic
left a comment
There was a problem hiding this comment.
Thanks for your PR @Keyur-S-Patel! I left two comments regarding the retry logic. Also, it would be great if we could add tests for run_async.
| @retry( | ||
| reraise=True, | ||
| stop=stop_after_attempt(self.max_retries + 1), | ||
| retry=retry_if_exception_type(Exception), |
Contributor
There was a problem hiding this comment.
Retrying on all kind of exceptions is too broad.
Contributor
Author
There was a problem hiding this comment.
Retry on 429 and 5xx?
Lemme know if you want to retry on something else as well.
Comment on lines
+527
to
+545
| @retry( | ||
| reraise=True, | ||
| stop=stop_after_attempt(self.max_retries + 1), | ||
| retry=retry_if_exception_type(Exception), | ||
| wait=wait_exponential(), | ||
| ) | ||
| def chat_with_retry() -> ChatResponse | Iterator[ChatResponse]: | ||
| return self._client.chat( | ||
| model=self.model, | ||
| messages=ollama_messages, | ||
| tools=ollama_tools, | ||
| stream=is_stream, # type: ignore[call-overload] # Ollama expects Literal[True] or Literal[False], not bool | ||
| keep_alive=self.keep_alive, | ||
| options=generation_kwargs, | ||
| format=self.response_format, | ||
| think=self.think, | ||
| ) | ||
|
|
||
| response = chat_with_retry() |
Contributor
There was a problem hiding this comment.
Can we have this not nested inside of run?
Contributor
Author
There was a problem hiding this comment.
@Retry annotation requires function so can
- use wrapper function or
- Build new method and put retry annotation there
Contributor
Author
There was a problem hiding this comment.
Will separate out function in next commit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR :
feat/ollama-chat-max-retriesRelated Issues
Proposed Changes:
max_retriessupport toOllamaChatGenerator.tenacity-based retries for both sync and async chat calls.wait_exponential().run()/run_async()using@retry-decorated callables instead of separate helper methods.tenacityto the Ollama integration package dependencies.How did you test it?
hatch run fmt-checkhatch run test:typeshatch run test:unitResult:
34 passed)Notes for the reviewer
tenacity.stop_after_attempt(...)uses total attempts, so the implementation usesmax_retries + 1to preserve the expected semantics of “initial call + N retries”.Exceptionfrom Ollama chat calls, matching the previous broad retry behavior while switching to a standard retry library.test:allstill depends on a running local Ollama instance for integration tests; only unit tests were used for branch validation.Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:.