Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add refresh auth headers (sync and async) as alternate approach to allow bearer tokens to be refreshed #17

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

johanste
Copy link
Owner

  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

Provide callable to allow refresh of the bearer token. The callable must support IO calls being made and thus has to be async in an asynchronous context.

Additional context & links


@override
def _prepare_options(self, options: FinalRequestOptions) -> FinalRequestOptions:
self.refresh_auth_headers()
Copy link
Owner Author

Choose a reason for hiding this comment

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

It is not great to. have to remember to call refresh_auth_headers before accessing them. But since the current implementation is a (sync) property, we have to somehow allow for async refresh.

We could do the refresh in the sync context, but I believe that the asymmetry between sync and async is worse than splitting it apart for both sync and async clients....

@@ -122,11 +125,12 @@ def __init__(
"""
if api_key is None:
api_key = os.environ.get("OPENAI_API_KEY")
if api_key is None:
if api_key is None and bearer_token_provider is None:
raise OpenAIError(
"The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we update the error message to include bearer_token_provider?

Choose a reason for hiding this comment

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

Also, should we error if both are provided?

@property
@override
def qs(self) -> Querystring:
return Querystring(array_format="brackets")

def refresh_auth_headers(self):
bearer_token = self.bearer_token_provider() if self.bearer_token_provider else self.api_key
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't have context for the change, but it looks like if the api_key or token is an empty string, they've updated the code to not send the header: https://github.com/openai/openai-python/blob/main/src/openai/_client.py#L286

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants