stagehand icon indicating copy to clipboard operation
stagehand copied to clipboard

Added support for Invalid LLM API KEYS

Open yash1744 opened this issue 1 year ago • 3 comments

why

Previously it was displaying a StagehandDefaultError when LLM api keys are not present which does not provide concise details about the error.

what changed

It now displays invalid LLM API keys with a clean structured message. Added error handling in createChatCompletion method for various LLM Clients ( OpenAI, Grok, Anthropic, Cerebras ). Created new InvalidLLMKeyError class for custom Invalid api message

test plan

yash1744 avatar Apr 05 '25 03:04 yash1744

🦋 Changeset detected

Latest commit: 8c7c3751b2afaded658fb84499d92b35d7573979

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@browserbasehq/stagehand Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Apr 05 '25 03:04 changeset-bot[bot]

thanks so much for contributing to stagehand! instead of modifying each client individually, can we instead change LLMClient such that it can call createChatCompletion and catch it with an error, then throw a ChatCompletionError?

also, please add a changeset - just run npx changeset, and this is a minor PR

kamath avatar Apr 05 '25 04:04 kamath

Yeah, that sounds like a good idea. But since createChatCompletion is an abstract method, we can’t directly instantiate it Instead, I can create a new method like safeCreateChatCompletion that wraps the logic and handles errors. It can throw a common ChatCompletionError instead of introducing a new InvalidLLMKey class and currently two methods in operatorHandler.ts file point to createChatCompletion which we need to modify it to new one.

However, even with that, we’ll still need to check which client is being used — like OpenAI, Groq, Cerebras (which are similar using OPEN AI client), or Anthropic, which has a different API and error pattern and handle both errors.

So in the future, if we add a new client, we’ll need to update this part again to support its error format.

Do you want me to move in this way!

yash1744 avatar Apr 05 '25 04:04 yash1744