4747 "AsyncClient" ,
4848]
4949
50+ _MODEL_API_KEY_ENV_VARS : tuple [str , ...] = (
51+ "MODEL_API_KEY" ,
52+ "OPENAI_API_KEY" ,
53+ "ANTHROPIC_API_KEY" ,
54+ "GEMINI_API_KEY" ,
55+ "GOOGLE_GENERATIVE_AI_API_KEY" ,
56+ "GOOGLE_API_KEY" ,
57+ "GOOGLE_VERTEX_AI_API_KEY" ,
58+ "GROQ_API_KEY" ,
59+ "CEREBRAS_API_KEY" ,
60+ "TOGETHER_AI_API_KEY" ,
61+ "MISTRAL_API_KEY" ,
62+ "DEEPSEEK_API_KEY" ,
63+ "PERPLEXITY_API_KEY" ,
64+ "AZURE_API_KEY" ,
65+ "XAI_API_KEY" ,
66+ )
67+
68+
69+ def _resolve_model_api_key (model_api_key : str | None ) -> str | None :
70+ if model_api_key is not None :
71+ return model_api_key
72+
73+ for env_var in _MODEL_API_KEY_ENV_VARS :
74+ value = os .environ .get (env_var )
75+ if value :
76+ return value
77+
78+ return None
79+
5080
5181class Stagehand (SyncAPIClient ):
5282 # client options
@@ -93,7 +123,7 @@ def __init__(
93123 This automatically infers the following arguments from their corresponding environment variables if they are not provided:
94124 - `browserbase_api_key` from `BROWSERBASE_API_KEY`
95125 - `browserbase_project_id` from `BROWSERBASE_PROJECT_ID`
96- - `model_api_key` from `MODEL_API_KEY`
126+ - `model_api_key` from `MODEL_API_KEY` or a recognized provider API key env var
97127 """
98128 self ._server_mode : Literal ["remote" , "local" ] = server
99129 self ._local_stagehand_binary_path = _local_stagehand_binary_path
@@ -113,11 +143,11 @@ def __init__(
113143 self .browserbase_api_key = browserbase_api_key
114144 self .browserbase_project_id = browserbase_project_id
115145
116- if model_api_key is None :
117- model_api_key = os .environ .get ("MODEL_API_KEY" )
146+ model_api_key = _resolve_model_api_key (model_api_key )
118147 if model_api_key is None :
119148 raise StagehandError (
120- "The model_api_key client option must be set either by passing model_api_key to the client or by setting the MODEL_API_KEY environment variable"
149+ "The model_api_key client option must be set either by passing model_api_key to the client "
150+ f"or by setting one of the supported environment variables: { ', ' .join (_MODEL_API_KEY_ENV_VARS )} "
121151 )
122152 self .model_api_key = model_api_key
123153
@@ -127,14 +157,14 @@ def __init__(
127157 if base_url is None :
128158 base_url = "http://127.0.0.1"
129159
130- openai_api_key = local_openai_api_key or os . environ . get ( "OPENAI_API_KEY" ) or model_api_key
160+ local_model_api_key = local_openai_api_key or model_api_key
131161 self ._sea_server = SeaServerManager (
132162 config = SeaServerConfig (
133163 host = local_host ,
134164 port = local_port ,
135165 headless = local_headless ,
136166 ready_timeout_s = local_ready_timeout_s ,
137- openai_api_key = openai_api_key ,
167+ model_api_key = local_model_api_key ,
138168 chrome_path = local_chrome_path ,
139169 shutdown_on_close = local_shutdown_on_close ,
140170 ),
@@ -381,7 +411,7 @@ def __init__(
381411 This automatically infers the following arguments from their corresponding environment variables if they are not provided:
382412 - `browserbase_api_key` from `BROWSERBASE_API_KEY`
383413 - `browserbase_project_id` from `BROWSERBASE_PROJECT_ID`
384- - `model_api_key` from `MODEL_API_KEY`
414+ - `model_api_key` from `MODEL_API_KEY` or a recognized provider API key env var
385415 """
386416 self ._server_mode : Literal ["remote" , "local" ] = server
387417 self ._local_stagehand_binary_path = _local_stagehand_binary_path
@@ -401,11 +431,11 @@ def __init__(
401431 self .browserbase_api_key = browserbase_api_key
402432 self .browserbase_project_id = browserbase_project_id
403433
404- if model_api_key is None :
405- model_api_key = os .environ .get ("MODEL_API_KEY" )
434+ model_api_key = _resolve_model_api_key (model_api_key )
406435 if model_api_key is None :
407436 raise StagehandError (
408- "The model_api_key client option must be set either by passing model_api_key to the client or by setting the MODEL_API_KEY environment variable"
437+ "The model_api_key client option must be set either by passing model_api_key to the client "
438+ f"or by setting one of the supported environment variables: { ', ' .join (_MODEL_API_KEY_ENV_VARS )} "
409439 )
410440 self .model_api_key = model_api_key
411441
@@ -414,14 +444,14 @@ def __init__(
414444 if base_url is None :
415445 base_url = "http://127.0.0.1"
416446
417- openai_api_key = local_openai_api_key or os . environ . get ( "OPENAI_API_KEY" ) or model_api_key
447+ local_model_api_key = local_openai_api_key or model_api_key
418448 self ._sea_server = SeaServerManager (
419449 config = SeaServerConfig (
420450 host = local_host ,
421451 port = local_port ,
422452 headless = local_headless ,
423453 ready_timeout_s = local_ready_timeout_s ,
424- openai_api_key = openai_api_key ,
454+ model_api_key = local_model_api_key ,
425455 chrome_path = local_chrome_path ,
426456 shutdown_on_close = local_shutdown_on_close ,
427457 ),
0 commit comments