Can I change the OpenAI API key at runtime? #16350
Replies: 5 comments 2 replies
-
🤖 Hey there, @cvansteenburg! Back at it with the good questions, I see. Let's dive into it! 😎 Based on the current design of the The If you need to use different API keys for different tasks, you might need to create separate model1 = ChatOpenAI(openai_api_key="key1")
model2 = ChatOpenAI(openai_api_key="key2")
# Use model1 for task1
response1 = model1.invoke("foo_prompt")
# Use model2 for task2
response2 = model2.invoke("bar_prompt") In this example, I hope this helps! If you have any further questions, feel free to ask. Best, Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
Beta Was this translation helpful? Give feedback.
-
Fixed this in my code. Opened issue #16567 to see if there's appetite for a PR. Closing this discussion. |
Beta Was this translation helpful? Give feedback.
-
did they fix this ? dynamic_auth_model = ChatOpenAI(openai_api_key="placeholder").configurable_fields( |
Beta Was this translation helpful? Give feedback.
-
Has anyone tried this with the new Langchain V3 version? I'd love to know if there have been any changes to how API key configuration works with the update. Thanks in advance for sharing any experiences or insights! |
Beta Was this translation helpful? Give feedback.
-
Hi, any update how can we solve this issue, I am facing the same issue |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Context
I'm working with LCEL in Python. I'm using the
ChatOpenAI
class (source) within FastAPI. The code initializes and then I want to pass different api keys at runtime, depending on the task I'm performing.ConfigurableField
(docs) allows me to change any config exceptopenai_api_key
.>> Is there something in the codebase that forbids this? How can I change the
openai_api_key
at runtime?This works
But this doesn't
(It uses "placeholderkey" instead of the desired "somevalidkey")
Thank you for any support - much appreciated!
Beta Was this translation helpful? Give feedback.
All reactions