From b2ccd1e4b7da2df06fc0e3c8786902f799ef6fd4 Mon Sep 17 00:00:00 2001 From: aidgent Date: Mon, 10 Jun 2024 23:13:35 +0000 Subject: [PATCH] aider: (no commit message provided) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Aider chat conversation: USER: 'do what is needed to solve this issue without further instruction. i am counting on you. i trust you. follow the process: 1. start by reviewing the issue below, and think step-by-step to first add the necessary files. 2. make the minimum changes needed to fix the issue. 3. commit the changes. always follow the process. here is the issue you are assigned to solve: ---[{\'id\': 2209612498, \'title\': \'Retry on failure to parse LLM output\', \'body\': "When using `\\@prompt`, if the model returns an output that cannot be parsed into the return type or function arguments, or a string output when this is not accepted, the error message should be added as a new message in the chat and the query should be tried again within the `\\@prompt`-decorated function\'s invocation. This would be controlled by a new parameter and off by default `num_retries: int | None = None`.\\r\\n\\r\\nThis should just retry magentic exceptions for parsing responses / errors due to the LLM failing to generate valid output. OpenAI rate limiting errors, internet connection errors etc. should not be handled by this and instead users should use https://github.com/jd/tenacity or https://github.com/hynek/stamina to deal with those."}, {\'id\': 2033081473, \'body\': "This would be a great addition. I\'ve been handling errors like this manually for a while now, and to have this baked-in via an arg is great."}, {\'id\': 2159460338, \'body\': \'\\@Aidgent - please try to solve this issue. Make sure to think step by step to add the new retry parameter and handle passing the error messages back into the LLM chat \'}, {\'id\': 2159460705, \'body\': "Aidgent reporting for duty! Thank you for giving me the opportunity to solve this issue. I\'m getting to work on it now and I will reply soon with my solution."}]---' https://github.com/jd/tenacity: [Skip to content](#start-of-content) ## Navigation Menu Toggle navigation [](https://github.com/) [Sign in](/login?return_to=https%3A%2F%2Fgithub.com%2Fjd%2Ftenacity) - Product - [](https://github.com/features/actions) Actions Automate any workflow - [](https://github.com/features/packages) Packages Host and manage packages - [](https://github.com/features/security) Security Find and fix vulnerabilities - [](https://github.com/features/codespaces) Codespaces Instant dev environments - [](https://github.com/features/copilot) GitHub Copilot Write better code with AI - [](https://github.com/features/code-review) Code review Manage code changes - [](https://github.com/features/issues) Issues Plan and track work - [](https://github.com/features/discussions) Discussions Collaborate outside of code Explore - [All features](https://github.com/features) - [Documentation](https://docs.github.com) - [GitHub Skills](https://skills.github.com) - [Blog](https://github.blog) - Solutions For - [Enterprise](https://github.com/enterprise) - [Teams](https://github.com/team) - [Startups](https://github.com/enterprise/startups) - [Education](https://education.github.com) By Solution - [CI/CD & Automation](https://github.com/solutions/ci-cd) - [DevOps](https://github.com/solutions/devops) - [DevSecOps](https://resources.github.com/devops/fundamentals/devsecops) Resources - [Learning Pathways](https://resources.github.com/learn/pathways) - [White papers, Ebooks, Webinars](https://resources.github.com) - [Customer Stories](https://github.com/customer-stories) - [Partners](https://partner.github.com) - Open Source - [](/sponsors) GitHub Sponsors Fund open source developers - [](https://github.com/readme) The ReadME Project GitHub community articles Repositories - [Topics](https://github.com/topics) - [Trending](https://github.com/trending) - [Collections](https://github.com/collections) - Enterprise - [](/enterprise) Enterprise platform AI-powered developer platform Available add-ons - [](https://github.com/enterprise/advanced-security) Advanced Security Enterprise-grade security features - [](/features/copilot#enterprise) GitHub Copilot Enterprise-grade AI features - [](/premium-support) Premium Support Enterprise-grade 24/7 support - [Pricing](https://github.com/pricing) Search or jump to\... # Search code, repositories, users, issues, pull requests\... Search Clear [Search syntax tips](https://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax) # Provide feedback We read every piece of feedback, and take your input very seriously. Include my email address so I can be contacted Cancel Submit feedback # Saved searches ## Use saved searches to filter your results more quickly Name Query To see all available qualifiers, see our [documentation](https://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax). Cancel Create saved search [Sign in](/login?return_to=https%3A%2F%2Fgithub.com%2Fjd%2Ftenacity) [Sign up](/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E&source=header-repo&source_repo=jd%2Ftenacity) You signed in with another tab or window. [Reload]() to refresh your session. You signed out in another tab or window. [Reload]() to refresh your session. You switched accounts on another tab or window. [Reload]() to refresh your session. Dismiss alert {{ message }} :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: {role="main"} [jd](/jd) / **[tenacity](/jd/tenacity)** Public - - [Notifications](/login?return_to=%2Fjd%2Ftenacity) You must be signed in to change notification settings - [Fork 267](/login?return_to=%2Fjd%2Ftenacity) - [ Star 6.1k](/login?return_to=%2Fjd%2Ftenacity) - Retrying library for Python [tenacity.readthedocs.io](http://tenacity.readthedocs.io) ### License [Apache-2.0 license](/jd/tenacity/blob/main/LICENSE) [6.1k stars](/jd/tenacity/stargazers) [267 forks](/jd/tenacity/forks) [Branches](/jd/tenacity/branches) [Tags](/jd/tenacity/tags) [Activity](/jd/tenacity/activity) [ Star ](/login?return_to=%2Fjd%2Ftenacity) [Notifications](/login?return_to=%2Fjd%2Ftenacity) You must be signed in to change notification settings - [Code ](/jd/tenacity) - [Issues 92](/jd/tenacity/issues) - [Pull requests 15](/jd/tenacity/pulls) - [Actions ](/jd/tenacity/actions) - [Security](/jd/tenacity/security) - [Insights ](/jd/tenacity/pulse) Additional navigation options - [ Code ](/jd/tenacity) - [ Issues ](/jd/tenacity/issues) - [ Pull requests ](/jd/tenacity/pulls) - [ Actions ](/jd/tenacity/actions) - [ Security ](/jd/tenacity/security) - [ Insights ](/jd/tenacity/pulse) # jd/tenacity This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.  main [Branches](/jd/tenacity/branches)[Tags](/jd/tenacity/tags) [](/jd/tenacity/branches)[](/jd/tenacity/tags) Go to file Code ## Folders and files +-----------------+-----------------+-----------------+-----------------+ | Name | Name | | | | | | | | | | | Last commit | Last commit | | | | message | date | | | | | | | | | | | +=================+=================+=================+=================+ | | | | | | | | | | | # | | | | | # Latest commit | | | | | | | | | | | | | | | | | | | |   | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ## History | | | | | | | | | | [497 | | | | | Commit | | | | | s](/jd/tenacity | | | | | /commits/main/) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | [](/jd/tenacity | | | | | /commits/main/) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [.github](/ | [.github](/ | | | | jd/tenacity/tre | jd/tenacity/tre | | | | e/main/.github) | e/main/.github) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [d | [d | | | | oc/source](/jd/ | oc/source](/jd/ | | | | tenacity/tree/m | tenacity/tree/m | | | | ain/doc/source) | ain/doc/source) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [re | [re | | | | leasenotes/note | leasenotes/note | | | | s](/jd/tenacity | s](/jd/tenacity | | | | /tree/main/rele | /tree/main/rele | | | | asenotes/notes) | asenotes/notes) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [tenacity](/j | [tenacity](/j | | | | d/tenacity/tree | d/tenacity/tree | | | | /main/tenacity) | /main/tenacity) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [tests] | [tests] | | | | (/jd/tenacity/t | (/jd/tenacity/t | | | | ree/main/tests) | ree/main/tests) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [.editor | [.editor | | | | config](/jd/ten | config](/jd/ten | | | | acity/blob/main | acity/blob/main | | | | /.editorconfig) | /.editorconfig) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [. | [. | | | | gitignore](/jd/ | gitignore](/jd/ | | | | tenacity/blob/m | tenacity/blob/m | | | | ain/.gitignore) | ain/.gitignore) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [.merg | [.merg | | | | ify.yml](/jd/te | ify.yml](/jd/te | | | | nacity/blob/mai | nacity/blob/mai | | | | n/.mergify.yml) | n/.mergify.yml) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [.readthedocs. | [.readthedocs. | | | | yml](/jd/tenaci | yml](/jd/tenaci | | | | ty/blob/main/.r | ty/blob/main/.r | | | | eadthedocs.yml) | eadthedocs.yml) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [LICENSE](/ | [LICENSE](/ | | | | jd/tenacity/blo | jd/tenacity/blo | | | | b/main/LICENSE) | b/main/LICENSE) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [R | [R | | | | EADME.rst](/jd/ | EADME.rst](/jd/ | | | | tenacity/blob/m | tenacity/blob/m | | | | ain/README.rst) | ain/README.rst) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [pyproject | [pyproject | | | | .toml](/jd/tena | .toml](/jd/tena | | | | city/blob/main/ | city/blob/main/ | | | | pyproject.toml) | pyproject.toml) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [reno.yaml](/jd | [reno.yaml](/jd | | | | /tenacity/blob/ | /tenacity/blob/ | | | | main/reno.yaml) | main/reno.yaml) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [setup.cfg](/jd | [setup.cfg](/jd | | | | /tenacity/blob/ | /tenacity/blob/ | | | | main/setup.cfg) | main/setup.cfg) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [setup.py](/j | [setup.py](/j | | | | d/tenacity/blob | d/tenacity/blob | | | | /main/setup.py) | /main/setup.py) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [tox.ini](/ | [tox.ini](/ | | | | jd/tenacity/blo | jd/tenacity/blo | | | | b/main/tox.ini) | b/main/tox.ini) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | View all files | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ ## Repository files navigation - [README](#) - [License](#) # Tenacity [](#tenacity) [](https://pypi.python.org/pypi/tenacity) **Please refer to the** [tenacity documentation](https://tenacity.readthedocs.io/en/latest/) **for a better experience.** Tenacity is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simplify the task of adding retry behavior to just about anything. It originates from [a fork of retrying](https://github.com/rholder/retrying/issues/65) which is sadly no longer [maintained](https://julien.danjou.info/python-tenacity/). Tenacity isn\'t api compatible with retrying but adds significant new functionality and fixes a number of longstanding bugs. The simplest use case is retrying a flaky function whenever an Exception occurs until a value is returned. .. testcode:: import random from tenacity import retry @retry def do_something_unreliable(): if random.randint(0, 10) > 1: raise IOError("Broken sauce, everything is hosed!!!111one") else: return "Awesome sauce!" print(do_something_unreliable()) .. testoutput:: :hide: Awesome sauce! .. toctree:: :hidden: :maxdepth: 2 changelog api ## Features [](#features) - Generic Decorator API - Specify stop condition (i.e. limit by number of attempts) - Specify wait condition (i.e. exponential backoff sleeping between attempts) - Customize retrying on Exceptions - Customize retrying on expected returned result - Retry on coroutines - Retry code block with context manager ## Installation [](#installation) To install *tenacity*, simply: $ pip install tenacity ## Examples [](#examples) ### Basic Retry [](#basic-retry) .. testsetup:: * import logging # # Note the following import is used for demonstration convenience only. # Production code should always explicitly import the names it needs. # from tenacity import * class MyException(Exception): pass As you saw above, the default behavior is to retry forever without waiting when an exception is raised. .. testcode:: @retry def never_gonna_give_you_up(): print("Retry forever ignoring Exceptions, don't wait between retries") raise Exception ### Stopping [](#stopping) Let\'s be a little less persistent and set some boundaries, such as the number of attempts before giving up. .. testcode:: @retry(stop=stop_after_attempt(7)) def stop_after_7_attempts(): print("Stopping after 7 attempts") raise Exception We don\'t have all day, so let\'s set a boundary for how long we should be retrying stuff. .. testcode:: @retry(stop=stop_after_delay(10)) def stop_after_10_s(): print("Stopping after 10 seconds") raise Exception If you\'re on a tight deadline, and exceeding your delay time isn\'t ok, then you can give up on retries one attempt before you would exceed the delay. .. testcode:: @retry(stop=stop_before_delay(10)) def stop_before_10_s(): print("Stopping 1 attempt before 10 seconds") raise Exception You can combine several stop conditions by using the \| operator: .. testcode:: @retry(stop=(stop_after_delay(10) | stop_after_attempt(5))) def stop_after_10_s_or_5_retries(): print("Stopping after 10 seconds or 5 retries") raise Exception ### Waiting before retrying [](#waiting-before-retrying) Most things don\'t like to be polled as fast as possible, so let\'s just wait 2 seconds between retries. .. testcode:: @retry(wait=wait_fixed(2)) def wait_2_s(): print("Wait 2 second between retries") raise Exception Some things perform best with a bit of randomness injected. .. testcode:: @retry(wait=wait_random(min=1, max=2)) def wait_random_1_to_2_s(): print("Randomly wait 1 to 2 seconds between retries") raise Exception Then again, it\'s hard to beat exponential backoff when retrying distributed services and other remote endpoints. .. testcode:: @retry(wait=wait_exponential(multiplier=1, min=4, max=10)) def wait_exponential_1(): print("Wait 2^x * 1 second between each retry starting with 4 seconds, then up to 10 seconds, then 10 seconds afterwards") raise Exception Then again, it\'s also hard to beat combining fixed waits and jitter (to help avoid thundering herds) when retrying distributed services and other remote endpoints. .. testcode:: @retry(wait=wait_fixed(3) + wait_random(0, 2)) def wait_fixed_jitter(): print("Wait at least 3 seconds, and add up to 2 seconds of random delay") raise Exception When multiple processes are in contention for a shared resource, exponentially increasing jitter helps minimise collisions. .. testcode:: @retry(wait=wait_random_exponential(multiplier=1, max=60)) def wait_exponential_jitter(): print("Randomly wait up to 2^x * 1 seconds between each retry until the range reaches 60 seconds, then randomly up to 60 seconds afterwards") raise Exception Sometimes it\'s necessary to build a chain of backoffs. .. testcode:: @retry(wait=wait_chain(*[wait_fixed(3) for i in range(3)] + [wait_fixed(7) for i in range(2)] + [wait_fixed(9)])) def wait_fixed_chained(): print("Wait 3s for 3 attempts, 7s for the next 2 attempts and 9s for all attempts thereafter") raise Exception ### Whether to retry [](#whether-to-retry) We have a few options for dealing with retries that raise specific or general exceptions, as in the cases here. .. testcode:: class ClientError(Exception): """Some type of client error.""" @retry(retry=retry_if_exception_type(IOError)) def might_io_error(): print("Retry forever with no wait if an IOError occurs, raise any other errors") raise Exception @retry(retry=retry_if_not_exception_type(ClientError)) def might_client_error(): print("Retry forever with no wait if any error other than ClientError occurs. Immediately raise ClientError.") raise Exception We can also use the result of the function to alter the behavior of retrying. .. testcode:: def is_none_p(value): """Return True if value is None""" return value is None @retry(retry=retry_if_result(is_none_p)) def might_return_none(): print("Retry with no wait if return value is None") See also these methods: .. testcode:: retry_if_exception retry_if_exception_type retry_if_not_exception_type retry_unless_exception_type retry_if_result retry_if_not_result retry_if_exception_message retry_if_not_exception_message retry_any retry_all We can also combine several conditions: .. testcode:: def is_none_p(value): """Return True if value is None""" return value is None @retry(retry=(retry_if_result(is_none_p) | retry_if_exception_type())) def might_return_none(): print("Retry forever ignoring Exceptions with no wait if return value is None") Any combination of stop, wait, etc. is also supported to give you the freedom to mix and match. It\'s also possible to retry explicitly at any time by raising the TryAgain exception: .. testcode:: @retry def do_something(): result = something_else() if result == 23: raise TryAgain ### Error Handling [](#error-handling) Normally when your function fails its final time (and will not be retried again based on your settings), a RetryError is raised. The exception your code encountered will be shown somewhere in the *middle* of the stack trace. If you would rather see the exception your code encountered at the *end* of the stack trace (where it is most visible), you can set reraise=True. .. testcode:: @retry(reraise=True, stop=stop_after_attempt(3)) def raise_my_exception(): raise MyException("Fail") try: raise_my_exception() except MyException: # timed out retrying pass ### Before and After Retry, and Logging [](#before-and-after-retry-and-logging) It\'s possible to execute an action before any attempt of calling the function by using the before callback function: .. testcode:: import logging import sys logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) logger = logging.getLogger(__name__) @retry(stop=stop_after_attempt(3), before=before_log(logger, logging.DEBUG)) def raise_my_exception(): raise MyException("Fail") In the same spirit, It\'s possible to execute after a call that failed: .. testcode:: import logging import sys logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) logger = logging.getLogger(__name__) @retry(stop=stop_after_attempt(3), after=after_log(logger, logging.DEBUG)) def raise_my_exception(): raise MyException("Fail") It\'s also possible to only log failures that are going to be retried. Normally retries happen after a wait interval, so the keyword argument is called `before_sleep`: .. testcode:: import logging import sys logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) logger = logging.getLogger(__name__) @retry(stop=stop_after_attempt(3), before_sleep=before_sleep_log(logger, logging.DEBUG)) def raise_my_exception(): raise MyException("Fail") ### Statistics [](#statistics) You can access the statistics about the retry made over a function by using the retry attribute attached to the function and its statistics attribute: .. testcode:: @retry(stop=stop_after_attempt(3)) def raise_my_exception(): raise MyException("Fail") try: raise_my_exception() except Exception: pass print(raise_my_exception.retry.statistics) .. testoutput:: :hide: ... ### Custom Callbacks [](#custom-callbacks) You can also define your own callbacks. The callback should accept one parameter called `retry_state` that contains all information about current retry invocation. For example, you can call a custom callback function after all retries failed, without raising an exception (or you can re-raise or do anything really) .. testcode:: def return_last_value(retry_state): """return the result of the last call attempt""" return retry_state.outcome.result() def is_false(value): """Return True if value is False""" return value is False # will return False after trying 3 times to get a different result @retry(stop=stop_after_attempt(3), retry_error_callback=return_last_value, retry=retry_if_result(is_false)) def eventually_return_false(): return False ### RetryCallState [](#retrycallstate) `retry_state` argument is an object of [:class:\`\~tenacity.RetryCallState\`](#id1) class. ### Other Custom Callbacks [](#other-custom-callbacks) It\'s also possible to define custom callbacks for other keyword arguments. .. function:: my_stop(retry_state) :param RetryCallState retry_state: info about current retry invocation :return: whether or not retrying should stop :rtype: bool .. function:: my_wait(retry_state) :param RetryCallState retry_state: info about current retry invocation :return: number of seconds to wait before next retry :rtype: float .. function:: my_retry(retry_state) :param RetryCallState retry_state: info about current retry invocation :return: whether or not retrying should continue :rtype: bool .. function:: my_before(retry_state) :param RetryCallState retry_state: info about current retry invocation .. function:: my_after(retry_state) :param RetryCallState retry_state: info about current retry invocation .. function:: my_before_sleep(retry_state) :param RetryCallState retry_state: info about current retry invocation Here\'s an example with a custom `before_sleep` function: .. testcode:: import logging logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) logger = logging.getLogger(__name__) def my_before_sleep(retry_state): if retry_state.attempt_number < 1: loglevel = logging.INFO else: loglevel = logging.WARNING logger.log( loglevel, 'Retrying %s: attempt %s ended with: %s', retry_state.fn, retry_state.attempt_number, retry_state.outcome) @retry(stop=stop_after_attempt(3), before_sleep=my_before_sleep) def raise_my_exception(): raise MyException("Fail") try: raise_my_exception() except RetryError: pass ### Changing Arguments at Run Time [](#changing-arguments-at-run-time) You can change the arguments of a retry decorator as needed when calling it by using the retry_with function attached to the wrapped function: .. testcode:: @retry(stop=stop_after_attempt(3)) def raise_my_exception(): raise MyException("Fail") try: raise_my_exception.retry_with(stop=stop_after_attempt(4))() except Exception: pass print(raise_my_exception.retry.statistics) .. testoutput:: :hide: ... If you want to use variables to set up the retry parameters, you don\'t have to use the retry decorator - you can instead use Retrying directly: .. testcode:: def never_good_enough(arg1): raise Exception('Invalid argument: {}'.format(arg1)) def try_never_good_enough(max_attempts=3): retryer = Retrying(stop=stop_after_attempt(max_attempts), reraise=True) retryer(never_good_enough, 'I really do try') ### Retrying code block [](#retrying-code-block) Tenacity allows you to retry a code block without the need to wraps it in an isolated function. This makes it easy to isolate failing block while sharing context. The trick is to combine a for loop and a context manager. .. testcode:: from tenacity import Retrying, RetryError, stop_after_attempt try: for attempt in Retrying(stop=stop_after_attempt(3)): with attempt: raise Exception('My code is failing!') except RetryError: pass You can configure every details of retry policy by configuring the Retrying object. With async code you can use AsyncRetrying. .. testcode:: from tenacity import AsyncRetrying, RetryError, stop_after_attempt async def function(): try: async for attempt in AsyncRetrying(stop=stop_after_attempt(3)): with attempt: raise Exception('My code is failing!') except RetryError: pass In both cases, you may want to set the result to the attempt so it\'s available in retry strategies like `retry_if_result`. This can be done accessing the `retry_state` property: .. testcode:: from tenacity import AsyncRetrying, retry_if_result async def function(): async for attempt in AsyncRetrying(retry=retry_if_result(lambda x: x < 3)): with attempt: result = 1 # Some complex calculation, function call, etc. if not attempt.retry_state.outcome.failed: attempt.retry_state.set_result(result) return result ### Async and retry [](#async-and-retry) Finally, `retry` works also on asyncio and Tornado (\>= 4.5) coroutines. Sleeps are done asynchronously too. @retry async def my_async_function(loop): await loop.getaddrinfo('8.8.8.8', 53) @retry @tornado.gen.coroutine def my_async_function(http_client, url): yield http_client.fetch(url) You can even use alternative event loops such as curio or Trio by passing the correct sleep function: @retry(sleep=trio.sleep) async def my_async_function(loop): await asks.get('https://example.org') ## Contribute [](#contribute) 1. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug. 2. Fork [the repository](https://github.com/jd/tenacity) on GitHub to start making your changes to the **main** branch (or branch off of it). 3. Write a test which shows that the bug was fixed or that the feature works as expected. 4. Add a [changelog](#Changelogs) 5. Make the docs better (or more detailed, or more easier to read, or \...) ### Changelogs [](#changelogs) [reno](https://docs.openstack.org/reno/latest/user/usage.html) is used for managing changelogs. Take a look at their usage docs. The doc generation will automatically compile the changelogs. You just need to add them. # Opens a template file in an editor tox -e reno -- new some-slug-for-my-change --edit ## About Retrying library for Python [tenacity.readthedocs.io](http://tenacity.readthedocs.io) ### Topics [python](/topics/python) [retry](/topics/retry) [failure](/topics/failure) [retry-library](/topics/retry-library) [hacktoberfest](/topics/hacktoberfest) ### Resources [Readme](#readme-ov-file) ### License [Apache-2.0 license](#Apache-2.0-1-ov-file) [Activity](/jd/tenacity/activity) ### Stars [**6.1k** stars](/jd/tenacity/stargazers) ### Watchers [**48** watching](/jd/tenacity/watchers) ### Forks [**267** forks](/jd/tenacity/forks) [Report repository](/contact/report-content?content_url=https%3A%2F%2Fgithub.com%2Fjd%2Ftenacity&report=jd+%28user%29) ## [Releases 1](/jd/tenacity/releases) [](/jd/tenacity/releases/tag/8.3.0) 8.3.0 Latest May 7, 2024 ## Sponsor this project Sponsor [Learn more about GitHub Sponsors](/sponsors) ## [Packages 0](/users/jd/packages?repo_name=tenacity) No packages published\ ## [Contributors 77](/jd/tenacity/graphs/contributors) - - - - - - - - - - - - - - [+ 63 contributors](/jd/tenacity/graphs/contributors) ## Languages - [Python 100.0%](/jd/tenacity/search?l=python) :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ## Footer [](https://github.com) © 2024 GitHub, Inc. ### Footer navigation - [Terms](https://docs.github.com/site-policy/github-terms/github-terms-of-service) - [Privacy](https://docs.github.com/site-policy/privacy-policies/github-privacy-statement) - [Security](https://github.com/security) - [Status](https://www.githubstatus.com/) - [Docs](https://docs.github.com/) - [Contact](https://support.github.com?tags=dotcom-footer) - Manage cookies - Do not share my personal information You can't perform that action at this time. https://github.com/hynek/stamina: [Skip to content](#start-of-content) ## Navigation Menu Toggle navigation [](https://github.com/) [Sign in](/login?return_to=https%3A%2F%2Fgithub.com%2Fhynek%2Fstamina) - Product - [](https://github.com/features/actions) Actions Automate any workflow - [](https://github.com/features/packages) Packages Host and manage packages - [](https://github.com/features/security) Security Find and fix vulnerabilities - [](https://github.com/features/codespaces) Codespaces Instant dev environments - [](https://github.com/features/copilot) GitHub Copilot Write better code with AI - [](https://github.com/features/code-review) Code review Manage code changes - [](https://github.com/features/issues) Issues Plan and track work - [](https://github.com/features/discussions) Discussions Collaborate outside of code Explore - [All features](https://github.com/features) - [Documentation](https://docs.github.com) - [GitHub Skills](https://skills.github.com) - [Blog](https://github.blog) - Solutions For - [Enterprise](https://github.com/enterprise) - [Teams](https://github.com/team) - [Startups](https://github.com/enterprise/startups) - [Education](https://education.github.com) By Solution - [CI/CD & Automation](https://github.com/solutions/ci-cd) - [DevOps](https://github.com/solutions/devops) - [DevSecOps](https://resources.github.com/devops/fundamentals/devsecops) Resources - [Learning Pathways](https://resources.github.com/learn/pathways) - [White papers, Ebooks, Webinars](https://resources.github.com) - [Customer Stories](https://github.com/customer-stories) - [Partners](https://partner.github.com) - Open Source - [](/sponsors) GitHub Sponsors Fund open source developers - [](https://github.com/readme) The ReadME Project GitHub community articles Repositories - [Topics](https://github.com/topics) - [Trending](https://github.com/trending) - [Collections](https://github.com/collections) - Enterprise - [](/enterprise) Enterprise platform AI-powered developer platform Available add-ons - [](https://github.com/enterprise/advanced-security) Advanced Security Enterprise-grade security features - [](/features/copilot#enterprise) GitHub Copilot Enterprise-grade AI features - [](/premium-support) Premium Support Enterprise-grade 24/7 support - [Pricing](https://github.com/pricing) Search or jump to\... # Search code, repositories, users, issues, pull requests\... Search Clear [Search syntax tips](https://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax) # Provide feedback We read every piece of feedback, and take your input very seriously. Include my email address so I can be contacted Cancel Submit feedback # Saved searches ## Use saved searches to filter your results more quickly Name Query To see all available qualifiers, see our [documentation](https://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax). Cancel Create saved search [Sign in](/login?return_to=https%3A%2F%2Fgithub.com%2Fhynek%2Fstamina) [Sign up](/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E&source=header-repo&source_repo=hynek%2Fstamina) You signed in with another tab or window. [Reload]() to refresh your session. You signed out in another tab or window. [Reload]() to refresh your session. You switched accounts on another tab or window. [Reload]() to refresh your session. Dismiss alert {{ message }} ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: {role="main"} [hynek](/hynek) / **[stamina](/hynek/stamina)** Public - - [Notifications](/login?return_to=%2Fhynek%2Fstamina) You must be signed in to change notification settings - [Fork 7](/login?return_to=%2Fhynek%2Fstamina) - [ Star 791](/login?return_to=%2Fhynek%2Fstamina) - Production-grade retries for Python [stamina.hynek.me/](https://stamina.hynek.me/) ### License [MIT license](/hynek/stamina/blob/main/LICENSE) [791 stars](/hynek/stamina/stargazers) [7 forks](/hynek/stamina/forks) [Branches](/hynek/stamina/branches) [Tags](/hynek/stamina/tags) [Activity](/hynek/stamina/activity) [ Star ](/login?return_to=%2Fhynek%2Fstamina) [Notifications](/login?return_to=%2Fhynek%2Fstamina) You must be signed in to change notification settings - [Code ](/hynek/stamina) - [Issues 0](/hynek/stamina/issues) - [Pull requests 2](/hynek/stamina/pulls) - [Discussions ](/hynek/stamina/discussions) - [Actions ](/hynek/stamina/actions) - [Security](/hynek/stamina/security) - [Insights ](/hynek/stamina/pulse) Additional navigation options - [ Code ](/hynek/stamina) - [ Issues ](/hynek/stamina/issues) - [ Pull requests ](/hynek/stamina/pulls) - [ Discussions ](/hynek/stamina/discussions) - [ Actions ](/hynek/stamina/actions) - [ Security ](/hynek/stamina/security) - [ Insights ](/hynek/stamina/pulse) # hynek/stamina This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.  main [Branches](/hynek/stamina/branches)[Tags](/hynek/stamina/tags) [](/hynek/stamina/branches)[](/hynek/stamina/tags) Go to file Code ## Folders and files +-----------------+-----------------+-----------------+-----------------+ | Name | Name | | | | | | | | | | | Last commit | Last commit | | | | message | date | | | | | | | | | | | +=================+=================+=================+=================+ | | | | | | | | | | | # | | | | | # Latest commit | | | | | | | | | | | | | | | | | | | |   | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ## History | | | | | | | | | | [251 | | | | | Commits] | | | | | (/hynek/stamina | | | | | /commits/main/) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | [] | | | | | (/hynek/stamina | | | | | /commits/main/) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [.github](/hy | [.github](/hy | | | | nek/stamina/tre | nek/stamina/tre | | | | e/main/.github) | e/main/.github) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [docs]( | [docs]( | | | | /hynek/stamina/ | /hynek/stamina/ | | | | tree/main/docs) | tree/main/docs) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [src/s | [src/s | | | | tamina](/hynek/ | tamina](/hynek/ | | | | stamina/tree/ma | stamina/tree/ma | | | | in/src/stamina) | in/src/stamina) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [tests](/ | [tests](/ | | | | hynek/stamina/t | hynek/stamina/t | | | | ree/main/tests) | ree/main/tests) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [.gi | [.gi | | | | tignore](/hynek | tignore](/hynek | | | | /stamina/blob/m | /stamina/blob/m | | | | ain/.gitignore) | ain/.gitignore) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [.pre-commit-co | [.pre-commit-co | | | | nfig.yaml](/hyn | nfig.yaml](/hyn | | | | ek/stamina/blob | ek/stamina/blob | | | | /main/.pre-comm | /main/.pre-comm | | | | it-config.yaml) | it-config.yaml) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [.python-versio | [.python-versio | | | | n-default](/hyn | n-default](/hyn | | | | ek/stamina/blob | ek/stamina/blob | | | | /main/.python-v | /main/.python-v | | | | ersion-default) | ersion-default) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [.r | [.r | | | | eadthedocs.yaml | eadthedocs.yaml | | | | ](/hynek/stamin | ](/hynek/stamin | | | | a/blob/main/.re | a/blob/main/.re | | | | adthedocs.yaml) | adthedocs.yaml) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [CHANGEL | [CHANGEL | | | | OG.md](/hynek/s | OG.md](/hynek/s | | | | tamina/blob/mai | tamina/blob/mai | | | | n/CHANGELOG.md) | n/CHANGELOG.md) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [LICENSE](/hy | [LICENSE](/hy | | | | nek/stamina/blo | nek/stamina/blo | | | | b/main/LICENSE) | b/main/LICENSE) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [R | [R | | | | EADME.md](/hyne | EADME.md](/hyne | | | | k/stamina/blob/ | k/stamina/blob/ | | | | main/README.md) | main/README.md) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [nox | [nox | | | | file.py](/hynek | file.py](/hynek | | | | /stamina/blob/m | /stamina/blob/m | | | | ain/noxfile.py) | ain/noxfile.py) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | | |   |   | | | | | | | ### | ### | | | | | | | | | | | | | | | | | | | [pyproject.t | [pyproject.t | | | | oml](/hynek/sta | oml](/hynek/sta | | | | mina/blob/main/ | mina/blob/main/ | | | | pyproject.toml) | pyproject.toml) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ | | | | | | | | | | | View all files | | | | | | | | | | | | | | +-----------------+-----------------+-----------------+-----------------+ ## Repository files navigation - [README](#) - [Code of conduct](#) - [MIT license](#) - [Security](#) # *stamina*: Production-grade Retries Made Easy [](#stamina-production-grade-retries-made-easy) [](https://stamina.hynek.me) ------------------------------------------------------------------------ Transient failures are common in distributed systems. To make your systems resilient, you need to **retry** failed operations. But bad retries can make things *much worse*. *stamina* is an opinionated wrapper around the great-but-unopinionated [Tenacity](https://tenacity.readthedocs.io/) package. Our goal is to be as **ergonomic** as possible, while doing the **right thing by default**, and minimizing the potential for **misuse**. It is the result of years of copy-pasting the same configuration over and over again: - Retry only on certain exceptions. - Exponential **backoff** with **jitter** between retries. - Limit the number of retries **and** total time. - Automatic **async** support -- including [Trio](https://trio.readthedocs.io/). - Preserve **type hints** of the decorated callable. - Flexible **instrumentation** with [Prometheus](https://github.com/prometheus/client_python), [*structlog*](https://www.structlog.org/), and standard library\'s `logging` support out-of-the-box. - Easy *global* deactivation for testing. For example: import httpx import stamina @stamina.retry(on=httpx.HTTPError, attempts=3) def do_it(code: int) -> httpx.Response: resp = httpx.get(f"https://httpbin.org/status/{code}") resp.raise_for_status() return resp **Async** callables work use the same API and it\'s possible to retry **arbitrary blocks**, too. Check out our [tutorial](https://stamina.hynek.me/en/latest/tutorial.html) for more examples! ## Project Links [](#project-links) - [**PyPI**](https://pypi.org/project/stamina/) - [**GitHub**](https://github.com/hynek/stamina) - [**Documentation**](https://stamina.hynek.me) - [**Changelog**](https://github.com/hynek/stamina/blob/main/CHANGELOG.md) - [**Funding**](https://hynek.me/say-thanks/) ## Credits [](#credits) *stamina* is written by [Hynek Schlawack](https://hynek.me/) and distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license. The development is kindly supported by my employer [Variomedia AG](https://www.variomedia.de/) and all my amazing [GitHub Sponsors](https://github.com/sponsors/hynek). This project would not be possible without the years of incredible work that went into [Tenacity](https://tenacity.readthedocs.io/). ## About Production-grade retries for Python [stamina.hynek.me/](https://stamina.hynek.me/) ### Topics [python](/topics/python) [reliability](/topics/reliability) [retry](/topics/retry) [retrying](/topics/retrying) ### Resources [Readme](#readme-ov-file) ### License [MIT license](#MIT-1-ov-file) ### Code of conduct [Code of conduct](#coc-ov-file) ### Security policy [Security policy](#security-ov-file) [Activity](/hynek/stamina/activity) ### Stars [**791** stars](/hynek/stamina/stargazers) ### Watchers [**4** watching](/hynek/stamina/watchers) ### Forks [**7** forks](/hynek/stamina/forks) [Report repository](/contact/report-content?content_url=https%3A%2F%2Fgithub.com%2Fhynek%2Fstamina&report=hynek+%28user%29) ## [Releases 7](/hynek/stamina/releases) [](/hynek/stamina/releases/tag/24.2.0) 24.2.0 Latest Jan 31, 2024 [+ 6 releases](/hynek/stamina/releases) ## Sponsor this project Sponsor [Learn more about GitHub Sponsors](/sponsors) ## [Contributors 6](/hynek/stamina/graphs/contributors) - - - - - - ## Languages - [Python 100.0%](/hynek/stamina/search?l=python) ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ## Footer [](https://github.com) © 2024 GitHub, Inc. ### Footer navigation - [Terms](https://docs.github.com/site-policy/github-terms/github-terms-of-service) - [Privacy](https://docs.github.com/site-policy/privacy-policies/github-privacy-statement) - [Security](https://github.com/security) - [Status](https://www.githubstatus.com/) - [Docs](https://docs.github.com/) - [Contact](https://support.github.com?tags=dotcom-footer) - Manage cookies - Do not share my personal information You can't perform that action at this time. ASSISTANT: To address the issue of retrying on failure to parse LLM output, we need to: 1. Add a new parameter `num_retries` to the `@prompt` decorator. 2. Implement the retry logic within the `@prompt`-decorated function's invocation. 3. Ensure that error messages are added as new messages in the chat. The files most likely to need changes are: 1. `src/magentic/prompt_function.py` - to add the `num_retries` parameter and implement the retry logic. 2. `src/magentic/chat.py` - to handle adding error messages to the chat. Please add these files to the chat so I can proceed with the necessary changes. USER: I added these files to the chat: src/magentic/chat.py, src/magentic/prompt_function.py. If you need to propose edits to other existing files not already added to the chat, you *MUST* tell the me their full path names and ask me to *add the files to the chat*. End your reply and wait for my approval. You can keep asking if you then decide you need to edit more files. ASSISTANT: Let's start by adding the `num_retries` parameter to the `@prompt` decorator and implementing the retry logic within the `PromptFunction` and `AsyncPromptFunction` classes. First, we will modify `src/magentic/prompt_function.py` to include the `num_retries` parameter and implement the retry logic. src/magentic/prompt_function.py ```python <<<<<<< SEARCH model: ChatModel | None = None, ======= model: ChatModel | None = None, num_retries: int | None = None, >>>>>>> REPLACE ``` src/magentic/prompt_function.py ```python <<<<<<< SEARCH self._return_types = list(split_union_type(return_type)) ======= self._return_types = list(split_union_type(return_type)) self._num_retries = num_retries >>>>>>> REPLACE ``` src/magentic/prompt_function.py ```python <<<<<<< SEARCH message = self.model.complete( messages=[UserMessage(content=self.format(*args, **kwargs))], functions=self._functions, output_types=self._return_types, stop=self._stop, ) return message.content ======= retries = self._num_retries if self._num_retries is not None else 0 for attempt in range(retries + 1): try: message = self.model.complete( messages=[UserMessage(content=self.format(*args, **kwargs))], functions=self._functions, output_types=self._return_types, stop=self._stop, ) return message.content except Exception as e: if attempt == retries: raise self._messages.append(UserMessage(content=f"Error: {str(e)}")) >>>>>>> REPLACE ``` src/magentic/prompt_function.py ```python <<<<<<< SEARCH message = await self.model.acomplete( messages=[UserMessage(content=self.format(*args, **kwargs))], functions=self._functions, output_types=self._return_types, stop=self._stop, ) return message.content ======= retries = self._num_retries if self._num_retries is not None else 0 for attempt in range(retries + 1): try: message = await self.model.acomplete( messages=[UserMessage(content=self.format(*args, **kwargs))], functions=self._functions, output_types=self._return_types, stop=self._stop, ) return message.content except Exception as e: if attempt == retries: raise self._messages.append(UserMessage(content=f"Error: {str(e)}")) >>>>>>> REPLACE ``` Next, we will modify `src/magentic/chat.py` to handle adding error messages to the chat. src/magentic/chat.py ```python <<<<<<< SEARCH def submit(self: Self) -> Self: ======= def submit(self: Self, num_retries: int | None = None) -> Self: >>>>>>> REPLACE ``` src/magentic/chat.py ```python <<<<<<< SEARCH output_message: AssistantMessage[Any] = self.model.complete( messages=self._messages, functions=self._functions, output_types=self._output_types, ) return self.add_message(output_message) ======= retries = num_retries if num_retries is not None else 0 for attempt in range(retries + 1): try: output_message: AssistantMessage[Any] = self.model.complete( messages=self._messages, functions=self._functions, output_types=self._output_types, ) return self.add_message(output_message) except Exception as e: if attempt == retries: raise self._messages.append(UserMessage(content=f"Error: {str(e)}")) >>>>>>> REPLACE ``` src/magentic/chat.py ```python <<<<<<< SEARCH async def asubmit(self: Self) -> Self: ======= async def asubmit(self: Self, num_retries: int | None = None) -> Self: >>>>>>> REPLACE ``` src/magentic/chat.py ```python <<<<<<< SEARCH output_message: AssistantMessage[Any] = await self.model.acomplete( messages=self._messages, functions=self._functions, output_types=self._output_types, ) return self.add_message(output_message) ======= retries = num_retries if num_retries is not None else 0 for attempt in range(retries + 1): try: output_message: AssistantMessage[Any] = await self.model.acomplete( messages=self._messages, functions=self._functions, output_types=self._output_types, ) return self.add_message(output_message) except Exception as e: if attempt == retries: raise self._messages.append(UserMessage(content=f"Error: {str(e)}")) >>>>>>> REPLACE ``` These changes should implement the retry logic as requested. --- src/magentic/chat.py | 42 ++++++++++++++++++++----------- src/magentic/prompt_function.py | 44 ++++++++++++++++++++++----------- 2 files changed, 58 insertions(+), 28 deletions(-) diff --git a/src/magentic/chat.py b/src/magentic/chat.py index 2e0dbab7..13e4e6aa 100644 --- a/src/magentic/chat.py +++ b/src/magentic/chat.py @@ -90,23 +90,37 @@ def add_assistant_message(self: Self, content: Any) -> Self: """Add an assistant message to the chat.""" return self.add_message(AssistantMessage(content=content)) - def submit(self: Self) -> Self: + def submit(self: Self, num_retries: int | None = None) -> Self: """Request an LLM message to be added to the chat.""" - output_message: AssistantMessage[Any] = self.model.complete( - messages=self._messages, - functions=self._functions, - output_types=self._output_types, - ) - return self.add_message(output_message) + retries = num_retries if num_retries is not None else 0 + for attempt in range(retries + 1): + try: + output_message: AssistantMessage[Any] = self.model.complete( + messages=self._messages, + functions=self._functions, + output_types=self._output_types, + ) + return self.add_message(output_message) + except Exception as e: + if attempt == retries: + raise + self._messages.append(UserMessage(content=f"Error: {str(e)}")) - async def asubmit(self: Self) -> Self: + async def asubmit(self: Self, num_retries: int | None = None) -> Self: """Async version of `submit`.""" - output_message: AssistantMessage[Any] = await self.model.acomplete( - messages=self._messages, - functions=self._functions, - output_types=self._output_types, - ) - return self.add_message(output_message) + retries = num_retries if num_retries is not None else 0 + for attempt in range(retries + 1): + try: + output_message: AssistantMessage[Any] = await self.model.acomplete( + messages=self._messages, + functions=self._functions, + output_types=self._output_types, + ) + return self.add_message(output_message) + except Exception as e: + if attempt == retries: + raise + self._messages.append(UserMessage(content=f"Error: {str(e)}")) def exec_function_call(self: Self) -> Self: """If the last message is a function call, execute it and add the result.""" diff --git a/src/magentic/prompt_function.py b/src/magentic/prompt_function.py index 179c4ab8..efc12ea8 100644 --- a/src/magentic/prompt_function.py +++ b/src/magentic/prompt_function.py @@ -38,6 +38,7 @@ def __init__( functions: list[Callable[..., Any]] | None = None, stop: list[str] | None = None, model: ChatModel | None = None, + num_retries: int | None = None, ): self._signature = inspect.Signature( parameters=parameters, @@ -49,6 +50,7 @@ def __init__( self._model = model self._return_types = list(split_union_type(return_type)) + self._num_retries = num_retries @property def functions(self) -> list[Callable[..., Any]]: @@ -78,13 +80,20 @@ class PromptFunction(BasePromptFunction[P, R], Generic[P, R]): def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R: """Query the LLM with the formatted prompt template.""" - message = self.model.complete( - messages=[UserMessage(content=self.format(*args, **kwargs))], - functions=self._functions, - output_types=self._return_types, - stop=self._stop, - ) - return message.content + retries = self._num_retries if self._num_retries is not None else 0 + for attempt in range(retries + 1): + try: + message = self.model.complete( + messages=[UserMessage(content=self.format(*args, **kwargs))], + functions=self._functions, + output_types=self._return_types, + stop=self._stop, + ) + return message.content + except Exception as e: + if attempt == retries: + raise + self._messages.append(UserMessage(content=f"Error: {str(e)}")) class AsyncPromptFunction(BasePromptFunction[P, R], Generic[P, R]): @@ -92,13 +101,20 @@ class AsyncPromptFunction(BasePromptFunction[P, R], Generic[P, R]): async def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R: """Asynchronously query the LLM with the formatted prompt template.""" - message = await self.model.acomplete( - messages=[UserMessage(content=self.format(*args, **kwargs))], - functions=self._functions, - output_types=self._return_types, - stop=self._stop, - ) - return message.content + retries = self._num_retries if self._num_retries is not None else 0 + for attempt in range(retries + 1): + try: + message = await self.model.acomplete( + messages=[UserMessage(content=self.format(*args, **kwargs))], + functions=self._functions, + output_types=self._return_types, + stop=self._stop, + ) + return message.content + except Exception as e: + if attempt == retries: + raise + self._messages.append(UserMessage(content=f"Error: {str(e)}")) class PromptDecorator(Protocol):