X Tutup
Skip to content

fix: support storage_state as dict, not just file path - Fixes #4257#4259

Open
rookweb1 wants to merge 2 commits intobrowser-use:mainfrom
rookweb1:fix-storage-state-dict-4257
Open

fix: support storage_state as dict, not just file path - Fixes #4257#4259
rookweb1 wants to merge 2 commits intobrowser-use:mainfrom
rookweb1:fix-storage-state-dict-4257

Conversation

@rookweb1
Copy link

@rookweb1 rookweb1 commented Mar 2, 2026

Summary

Fixes issue #4257 - storage_state incoming object does not take effect.

Problem

When users pass storage_state as a dictionary (e.g., Browser(storage_state={"cookies": [...]})), the code was treating it as a file path and calling os.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 in storage_state_watchdog.py to:

  1. First check if storage_state is a dict
  2. If it is a dict, use it directly instead of treating it as a file path
  3. If it is a string/path, load from file as before

This 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 lookup

Testing

The fix allows users to pass storage_state as a dict:

browser = Browser(
    storage_state={
        "cookies": [
            {
                "name": "sessionid",
                "value": "xx",
                "domain": "xxx",
                "path": "/",
                "expires": 1777603376.257413,
                "httpOnly": True,
                "secure": False,
                "sameSite": "Lax",
            },
        ]
    },
)

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.

  • Bug Fixes
    • Handle dict input first; only read from disk when a valid path exists.
    • Add error logging on file read failures and no-op on invalid paths.

Written for commit 24ffa1f. Summary will update on new commits.

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>
@CLAassistant
Copy link

CLAassistant commented Mar 2, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-ai with guidance or docs links (including llms.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:
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Fix with Cubic

Copy link
Author

@rookweb1 rookweb1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! Moved import anyio to the top of the file. Thanks for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: browser session storage_state incoming object does not take effect

2 participants

X Tutup