-
Notifications
You must be signed in to change notification settings - Fork 19.4k
feat(openai): add callable support for openai_api_key parameter #33532
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
base: master
Are you sure you want to change the base?
Conversation
386a4e4
to
1dd0e14
Compare
CodSpeed Performance ReportMerging #33532 will degrade performances by 18.61%Comparing Summary
Benchmarks breakdown
Footnotes
|
Tests passing! cc @ccurme and @mdrxy. This PR addresses the issue we discussed in Slack to add in support for a callable in OpenAI for the new v1 API. Note this is the same in OpenAI client code here https://github.com/openai/openai-python/blob/ebf32212f7bf5bec6b24cc2276ac0d9a28dd63bb/src/openai/_client.py#L102 |
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.
Thank you for this!
Hi @ccurme, would be awesome to get this merged before the v1 announcement if possible! We'd like to add these updates about EntraID to the MSFT Learn docs |
I think there's a configuration issue with codspeed, same errors are coming up on an unrelated PR that just adds a unit test: https://github.com/langchain-ai/langchain/pull/33134/checks?check_run_id=53206813137 |
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.
Pull Request Overview
This PR enables dynamic API key retrieval in OpenAI integration classes by allowing callable functions to be passed as the openai_api_key
parameter. This change is necessary to support Azure OpenAI's v1 API authentication pattern using Microsoft Entra ID tokens, which require automatic token refresh via callable token providers.
Key Changes:
- Modified type signatures to accept
Callable[[], str]
in addition toSecretStr | None
- Added runtime type checking to handle both
SecretStr
and callable API key types - Added unit tests verifying callable API key functionality
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
File | Description |
---|---|
libs/partners/openai/langchain_openai/llms/base.py |
Updated BaseOpenAI to accept callable API keys and handle extraction logic |
libs/partners/openai/langchain_openai/embeddings/base.py |
Updated OpenAIEmbeddings to accept callable API keys and handle extraction logic |
libs/partners/openai/langchain_openai/chat_models/base.py |
Updated BaseChatOpenAI to accept callable API keys and handle extraction logic |
libs/partners/openai/tests/unit_tests/test_secrets.py |
Added parametrized test verifying callable API key support across all three classes |
No worries, feel free to add the changes when you've confirmed. I'll check back in tomorrow |
@marlenezw I'll get this across, it increased in scope somewhat to support the async case but I think it's mostly done. Will come back tomorrow. |
Description
This PR adds support for passing a callable function as the
openai_api_key
parameter inChatOpenAI
,OpenAI
, andOpenAIEmbeddings
classes. This enhancement enables dynamic API key retrieval at runtime, which is needed for Azure OpenAI's new v1 API with Microsoft Entra ID (formerly Azure AD) token-based authentication.Context: Azure OpenAI v1 API Evolution
As outlined in this Azure OpenAI API version lifecycle documentation, Azure OpenAI introduced a v1 API (Generally Available as of August 2025) that brings several improvements. The most relevant here:
OpenAI()
client instead of Azure-specific clientsOpenAI()
client, removing the dependency on separate Azure clients.Key Use Case: Microsoft Entra ID Authentication
The v1 API documentation specifically shows that Microsoft Entra ID authentication now works by passing a token provider callable directly to the api_key parameter:
This PR enables LangChain to support this pattern. Previously, LangChain only accepted static strings or
[SecretStr]
parameter, which prevented users from being able to use an Azure credential.Changes Made
Updated the code in the OpenAI Base, LLMs and Emeddings files to contain this code:
I also added an environmental check to see if the user is using a key from Azure OpenAI instead of one from OpenAI
Added tests to the unit tests secret testing file.
Enhancement to support Azure OpenAI v1 API authentication patterns
Dependencies
None - uses existing dependencies