I’m working with LangChain and LangSmith for testing my language model prompts. Everything was functioning properly until I had to regenerate my OpenAI API key.
I updated the new key in my environment file and confirmed it works by testing with OpenAI’s chat completion directly. I also made sure to update it in LangSmith through the playground settings and verified it appears correctly in my organization secrets.
But when I run arun_on_dataset, I get this authentication error:
AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: sk-O6ZSB***************************************TNxS. You can find your API key at https://platform.openai.com/account/api-keys.'
The error shows my old API key prefix, not the new one. I can’t figure out where else this old key might be cached or stored. My code hasn’t changed at all.
Where else should I look to update this API key? Any suggestions would be helpful.
check ~/.langsmith or hidden folders, langsmith might cache keys locally. try setting the OpenAI key in your script with os.environ['OPENAI_API_KEY'] = 'your_new_key' before running arun_on_dataset to override any cached values.
definitely sounds like a caching issue. try logging out of LangSmith and then back in, or clear your browser’s cache. also, check if there’s an .env file higher up in your directory, it might be using the old key still. good luck!
I hit this exact issue last month. LangSmith was grabbing the API key from multiple places and I’d only updated some of them. Check if you’ve got the key hardcoded in project config files or if there’s a separate config file LangSmith uses. Also do a full restart of your dev environment - old environment variables can stick around in memory even after you update the .env file. I had to kill my Jupyter kernel completely and restart before the new key worked. That error showing your old key prefix means it’s definitely still reading from somewhere you missed.
Had the same frustrating issue recently. LangSmith stores API keys at the workspace level, separate from org settings. Check your specific workspace settings instead of just org-level secrets - there might be an override using your old key. Also check for LangChain config files like langchain.json in your project root. These sometimes cache credentials independently. Do a global search for that old key prefix across your entire project - it might be hiding somewhere weird. Since direct OpenAI calls work but LangSmith doesn’t, it’s definitely a LangSmith config issue, not your environment variables.