fix: support storage_state as dict, not just file path - Fixes #4257#4259
Open
rookweb1 wants to merge 2 commits intobrowser-use:mainfrom
Open
fix: support storage_state as dict, not just file path - Fixes #4257#4259rookweb1 wants to merge 2 commits intobrowser-use:mainfrom
rookweb1 wants to merge 2 commits intobrowser-use:mainfrom
Conversation
Fixes issue browser-use#4257 - storage_state incoming object does not take effect When users pass storage_state as a dict (e.g., Browser(storage_state={cookies: [...]})), the code was treating it as a file path and calling os.path.exists() which failed. Now checks if storage_state is a dict first, and uses it directly instead of trying to load from a non-existent file path. Co-authored-by: Rook <tydenweb@gmail.com>
Contributor
There was a problem hiding this comment.
1 issue found across 1 file
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="browser_use/browser/watchdogs/storage_state_watchdog.py">
<violation number="1" location="browser_use/browser/watchdogs/storage_state_watchdog.py:251">
P0: SyntaxError: orphaned `except` block without matching `try` after refactoring. The outer `try:` was removed but its corresponding `except Exception as e:` at line 328 remains.
Fix: Add a `try:` before the cookie/application logic (before line 254) to wrap the code that should have error handling, or properly restructure the logic to remove the orphaned exception handler.</violation>
</file>
Since this is your first cubic review, here's how it works:
- cubic automatically reviews your code and comments on bugs and improvements
- Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
- Add one-off context when rerunning by tagging
@cubic-dev-aiwith guidance or docs links (includingllms.txt) - Ask questions if you need clarification on any suggestion
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| import anyio | ||
| content = await anyio.Path(str(load_path)).read_text() | ||
| storage = json.loads(content) | ||
| except Exception as e: |
Contributor
There was a problem hiding this comment.
P0: SyntaxError: orphaned except block without matching try after refactoring. The outer try: was removed but its corresponding except Exception as e: at line 328 remains.
Fix: Add a try: before the cookie/application logic (before line 254) to wrap the code that should have error handling, or properly restructure the logic to remove the orphaned exception handler.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At browser_use/browser/watchdogs/storage_state_watchdog.py, line 251:
<comment>SyntaxError: orphaned `except` block without matching `try` after refactoring. The outer `try:` was removed but its corresponding `except Exception as e:` at line 328 remains.
Fix: Add a `try:` before the cookie/application logic (before line 254) to wrap the code that should have error handling, or properly restructure the logic to remove the orphaned exception handler.</comment>
<file context>
@@ -237,16 +237,24 @@ async def _load_storage_state(self, path: str | None = None) -> None:
+ import anyio
+ content = await anyio.Path(str(load_path)).read_text()
+ storage = json.loads(content)
+ except Exception as e:
+ self.logger.error(f'[StorageStateWatchdog] Failed to load storage state file: {e}')
+ return
</file context>
rookweb1
commented
Mar 2, 2026
Author
rookweb1
left a comment
There was a problem hiding this comment.
Fixed! Moved import anyio to the top of the file. Thanks for the review!
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.
Summary
Fixes issue #4257 - storage_state incoming object does not take effect.
Problem
When users pass
storage_stateas a dictionary (e.g.,Browser(storage_state={"cookies": [...]})), the code was treating it as a file path and callingos.path.exists()which failed because it was looking for a file named{"cookies": [...]}instead of using the dict directly.Solution
Modified
_load_storage_state()method instorage_state_watchdog.pyto:storage_stateis a dictThis matches how
_save_storage_state()already handles dict storage_state.Changes
browser_use/browser/watchdogs/storage_state_watchdog.py: Added dict check before file path lookupTesting
The fix allows users to pass storage_state as a dict:
AI-assisted contribution (built with assistance)
Summary by cubic
Allow storage_state to be a dict and use it directly instead of treating it as a file path. Fixes #4257 and enables initializing Browser with in-memory cookies.
Written for commit 24ffa1f. Summary will update on new commits.