Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix missing error in openai_request retry strategy #4802

Merged
merged 3 commits into from
May 10, 2023

Conversation

silvanocerza
Copy link
Contributor

@silvanocerza silvanocerza commented May 3, 2023

Related Issues

Proposed Changes:

When migrating to tenacity with PR #4460 I missed an error handling in openai_request function, this PR adds it back.

How did you test it?

Added unit tests to verify failing and success cases.

Notes for the reviewer

N/A

@silvanocerza silvanocerza self-assigned this May 3, 2023
@coveralls
Copy link
Collaborator

coveralls commented May 3, 2023

Coverage Status

Coverage: 36.944% (+0.07%) from 36.876% when pulling e8d9807 on fix-openai-retry into c734c58 on main.

@silvanocerza silvanocerza marked this pull request as ready for review May 8, 2023 15:08
@silvanocerza silvanocerza requested a review from a team as a code owner May 8, 2023 15:08
@silvanocerza silvanocerza requested review from bogdankostic and removed request for a team May 8, 2023 15:08
Copy link
Contributor

@bogdankostic bogdankostic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost good to go, I think we just need to account for OpenAIUnauthorizedError.

@@ -128,7 +128,8 @@ def _openai_text_completion_tokenization_details(model_name: str):


@tenacity.retry(
retry=tenacity.retry_if_exception_type(OpenAIRateLimitError),
reraise=True,
retry=tenacity.retry_if_exception_type((OpenAIRateLimitError, OpenAIError)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think specifying here only OpenAIError should be enough, as OpenAIRateLimitError inherits from OpenAIError.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To account for not retrying with OpenAIUnauthorizedError, I think we can change this line to something like this:

Suggested change
retry=tenacity.retry_if_exception_type((OpenAIRateLimitError, OpenAIError)),
retry=(tenacity.retry_if_exception_type(OpenAIError) and tenacity.retry_if_not_exception_type(OpenAIUnauthorizedError)),

Comment on lines 36 to 46
@pytest.mark.unit
@patch("haystack.utils.openai_utils.requests")
def test_openai_request_does_not_retry_on_unauthorized_error(mock_requests):
mock_requests.request.return_value.status_code = 401

with pytest.raises(OpenAIUnauthorizedError):
# We need to use a custom wait amount otherwise the test would take forever to run
# as the original wait time is exponential
openai_request.retry_with(wait=wait_none())(url="some_url", headers={}, payload={}, read_response=False)

assert mock_requests.request.call_count == 5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test name says does_not_retry_on_unauthorized_error but we check whether 5 requests have been made.

@silvanocerza
Copy link
Contributor Author

Updated with base to trigger CI again as it was stuck after yesterday's outage.

Copy link
Contributor

@bogdankostic bogdankostic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :)

@silvanocerza silvanocerza merged commit f12e5a0 into main May 10, 2023
60 checks passed
@silvanocerza silvanocerza deleted the fix-openai-retry branch May 10, 2023 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OpenAI errors not caught
3 participants