feat(client): support callable api_key#2588
Merged
RobertCraigie merged 12 commits intoopenai:nextfrom Sep 3, 2025
Merged
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 <robert@craigie.dev>
…api key values for module level client.
RobertCraigie
approved these changes
Sep 2, 2025
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 |
Collaborator
There was a problem hiding this comment.
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?
Contributor
There was a problem hiding this comment.
Discussed with @johanste offline and agreed we can remove callable api keys for the module level client.
RobertCraigie
approved these changes
Sep 3, 2025
Collaborator
RobertCraigie
left a comment
There was a problem hiding this comment.
Looks great thank you!
Merged
stainless-app bot
pushed a commit
that referenced
this pull request
Sep 4, 2025
Co-authored-by: Krista Pratico <krpratic@microsoft.com>
This was referenced Sep 12, 2025
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.
Changes being requested
Make the
api_keyparameter 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.