Skip to content

fix(session): ignore unsupported Playwright cookie fields#1335

Closed
enigmAsad wants to merge 3 commits intoapify:masterfrom
enigmAsad:fix/playwright-cookie-crash
Closed

fix(session): ignore unsupported Playwright cookie fields#1335
enigmAsad wants to merge 3 commits intoapify:masterfrom
enigmAsad:fix/playwright-cookie-crash

Conversation

@enigmAsad
Copy link
Copy Markdown

### Problem

Crawlee currently crashes when importing cookies from Playwright due to unsupported fields like partitionKey and _crHasCrossSiteAncestor, which are Chromium-specific and not part of the standard cookie format.

Error:
TypeError: SessionCookies.set() got an unexpected keyword argument 'partitionKey'

## Root Cause

Recent versions of Playwright (especially with Chromium) include internal browser metadata fields in the cookie dictionary. Crawlee's SessionCookies.set() method does not accept these extra keys.

Affected fields:

  • partitionKey
  • _crHasCrossSiteAncestor

## Fix

Modified the set_cookies_from_playwright_format() method in sessions/_cookies.py to:

  • Remove any field starting with _
  • Explicitly remove partitionKey if present

This ensures compatibility and future-proofs against additional Chromium-internal fields.

cookie_param = {
    k: v for k, v in self._from_playwright(pw_cookie).items()
    if not k.startswith('_')
}
cookie_param.pop('partitionKey', None)
self.set(**cookie_param)

### Tests

Added a unit test in tests/unit/sessions/test_cookies.py to verify that cookies with unsupported fields are cleaned and do not raise exceptions when passed to set_cookies_from_playwright_format().

### Environment

  • Crawlee version: 0.6.11
  • Python version: 3.12
  • OS: Windows 11
  • Browser: Chromium via Playwright

### Related Issue
Closes #1333

Playwright recently added internal fields like _crHasCrossSiteAncestor and partitionKey to cookie objects, which are not accepted by Crawlee's SessionCookies.set() method.

This commit filters out any cookie keys starting with _ and removes partitionKey explicitly to prevent a TypeError.

Fixes issue apify#1333"
….Adds a unit test in tests/unit/sessions/test_cookies.py to ensure that unsupported Playwright cookie fields like `_crHasCrossSiteAncestor` and `partitionKey` are filtered out correctly.

Verifies that SessionCookies.set() does not raise errors when such fields are present in input cookies.

Builds upon previous fix in set_cookies_from_playwright_format().
@enigmAsad enigmAsad changed the title Fix: Ignore unsupported Playwright cookie fields to prevent Crawlee crash fix(session): ignore unsupported Playwright cookie fields Jul 30, 2025
@vdusek vdusek requested a review from Mantisus July 30, 2025 08:13
@Mantisus
Copy link
Copy Markdown
Collaborator

Hey, @enigmAsad

Thank you for your PR and interest in Crawlee. I really appreciate it.

Due to the fact that the _crHasCrossSiteAncestor key for Playwright is not described in the Playwright-python data models - https://github.com/microsoft/playwright-python/blob/main/playwright/_impl/_api_structures.py

I would prefer to explicitly ignore any unknown parameters that may be passed to the cookie constructor. This would help avoid similar issues in the future.

I have implemented this in PR #1336.

@Mantisus Mantisus closed this Jul 30, 2025
@enigmAsad
Copy link
Copy Markdown
Author

"Thanks @Mantisus for the update! That makes perfect sense, happy to see the issue resolved in a more general way. Glad I could help flag it!"

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.

Playwright cookie import fails due to unsupported 'partitionKey' and '_crHasCrossSiteAncestor' fields

2 participants