Fix Client(model_api_key=...) and remove OPENAI_API_KEY/MODEL_API_KEY env var auto loading#328
Merged
Fix Client(model_api_key=...) and remove OPENAI_API_KEY/MODEL_API_KEY env var auto loading#328
Client(model_api_key=...) and remove OPENAI_API_KEY/MODEL_API_KEY env var auto loading#328Conversation
There was a problem hiding this comment.
2 issues found across 12 files
Confidence score: 4/5
- This PR looks safe to merge overall, with risk concentrated in test stability rather than production behavior.
- The most significant issue is in
tests/test_client.py: not clearingANTHROPIC_API_KEYcan cause_resolve_model_api_keyto pick the wrong key based on runner environment, leading to non-deterministic test outcomes. - A similar environment-leak risk exists in
tests/test_sea_binary.py, where missingmonkeypatch.delenv("STAGEHAND_SEA_BINARY", raising=False)can bypass the intended monkeypatched path resolution and make the test depend on external env state. - Pay close attention to
tests/test_client.py,tests/test_sea_binary.py- ensure env vars are explicitly cleared so tests remain deterministic across different runners.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="tests/test_client.py">
<violation number="1" location="tests/test_client.py:506">
P2: This test doesn't omit `ANTHROPIC_API_KEY`, which has higher priority than `GEMINI_API_KEY` in `_MODEL_API_KEY_ENV_VARS`. If `ANTHROPIC_API_KEY` is set in the test environment, `_resolve_model_api_key` will return it instead of the intended `"gemini-key"`, causing the assertion to fail.</violation>
</file>
<file name="tests/test_sea_binary.py">
<violation number="1" location="tests/test_sea_binary.py:33">
P2: Missing `monkeypatch.delenv("STAGEHAND_SEA_BINARY", raising=False)`. If this env var is set in the runner's environment, `resolve_binary_path()` returns early before reaching the monkeypatched `_resource_binary_path`/`_copy_to_cache`, causing the test to fail.</violation>
</file>
Architecture diagram
sequenceDiagram
participant User as Developer / App
participant Client as Stagehand Client
participant Env as OS Environment
participant Manager as SeaServerManager
participant Binary as SEA Server Binary
participant GitHub as GitHub Releases API
Note over User, GitHub: Initialization & Key Resolution Flow
User->>Client: NEW: Stagehand(model_api_key=None, ...)
Client->>Env: NEW: _resolve_model_api_key()
Note right of Env: Iterates provider list:<br/>MODEL_API_KEY, OPENAI_API_KEY,<br/>ANTHROPIC_API_KEY, GEMINI_API_KEY, etc.
Env-->>Client: Return first matching value
alt No API key found in args or environment
Client-->>User: NEW: Raise StagehandError (lists all supported env vars)
end
Note over Client, Binary: Local Server Startup (Server="local")
Client->>Manager: NEW: Initialize SeaServerConfig(model_api_key=...)
Note right of Client: local_openai_api_key now<br/>maps to model_api_key
Manager->>Binary: resolve_binary_path()
opt Version check required
Binary->>GitHub: NEW: resolve_latest_server_tag()
Note right of GitHub: NEW: Filters for stable tags only<br/>(ignores -dev or +build)
GitHub-->>Binary: Latest stable stagehand-server-v3/vX.Y.Z
end
Binary-->>Manager: Local path to binary
Manager->>Manager: NEW: _build_process_env()
Note right of Manager: Sets MODEL_API_KEY in child env<br/>regardless of provider source
Manager->>Binary: CHANGED: subprocess.Popen(env=proc_env)
Binary->>Binary: Start Stagehand Server
Note over Binary: Server uses MODEL_API_KEY<br/>for all LLM provider requests
Binary-->>Manager: Server ready
Manager-->>Client: Connection established
Client-->>User: Client ready
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
839a7ef to
dd3124d
Compare
monadoid
approved these changes
Apr 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
openai_api_keyparam in favor of onlymodel_api_keyBROWSERBASE_FLOW_LOGS/BROWSERBASE_CONFIG_DIRto SEA binarySummary by cubic
Stop auto-reading model API keys from env. Only send
x-model-api-keywhenmodel_api_keyis provided, set the SEA childMODEL_API_KEYexplicitly in local mode, and force productionNODE_ENVfor the SEA process.Bug Fixes
MODEL_API_KEYor provider env vars; header is sent only ifmodel_api_keyis set.MODEL_API_KEYfor the SEA process frommodel_api_key(or empty), never infers fromOPENAI_API_KEY, and forwardsBROWSERBASE_FLOW_LOGS/BROWSERBASE_CONFIG_DIR.MODEL_API_KEYwhile leaving unrelated env (likeOPENAI_API_KEY) intact.NODE_ENV=productionto avoid dev-only dependency issues..github/publish-pypi.ymlandscripts/download-binary.pyselect the highest stablestagehand-server-v3/vX.Y.Zrelease and ignore dev/pre-release tags; binary cache version defaults to the Python package version whenSTAGEHAND_VERSIONis unset.Migration
model_api_keyexplicitly if you need LLM auth; env vars are no longer auto-read.local_openai_api_key; usemodel_api_keyinstead (examples updated).Written for commit ec6ff4d. Summary will update on new commits. Review in cubic