-
Notifications
You must be signed in to change notification settings - Fork 4.2k
feat(client): support callable api_key #2588
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
Conversation
…low bearer tokens to be updated Allow api_key to be a callable to enable refresh of keys/tokens.
… Propagate bearer_token_provider in the `copy` method.
* add tests, fix copy, add token provider to module client * fix lint * ignore for azure copy * revert change
Co-authored-by: Robert Craigie <[email protected]>
…api key values for module level client.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look great!
src/openai/__init__.py
Outdated
def api_key(self) -> str | None: | ||
return api_key | ||
def api_key(self) -> str | _t.Callable[[], str] | None: | ||
return api_key() if callable(api_key) else api_key |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like it could be brittle and cause some weird behaviour if we access self.api_key
in multiple places for the same request.
My gut is that we could just not support callable api keys for the module client?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed with @johanste offline and agreed we can remove callable api keys for the module level client.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great thank you!
Changes being requested
Make the
api_key
parameter accept atyping. Callable[[], str]
(ortyping.Callable[[], typing.Awaitable[str]]
for the async client) to allow for dynamic token refresh.Additional context & links
This is what the python_ad.py example would look like when using the base OpenAI clients + the bearer token provider.