Skip to content

fix(registry): accept X | None annotations on special action params - #6023

Open
adimalkar wants to merge 1 commit into
browser-use:mainfrom
adimalkar:fix/registry-pep604-special-params
Open

adimalkar wants to merge 1 commit into
browser-use:mainfrom
adimalkar:fix/registry-pep604-special-params

Conversation

@adimalkar

@adimalkar adimalkar commented Oct 9, 2026 •

Copy link
Copy Markdown

Fixes #5905

What

On Python 3.11–3.13, a custom action that declares a special injected parameter with a PEP 604 optional fails to register:

@registry.action('demo')
async def demo(browser_session: BrowserSession | None = None): ...
# ValueError: Action 'demo' parameter 'browser_session: BrowserSession | None'
# conflicts with special argument injected by tools: 'browser_session: BrowserSession'

Optional[BrowserSession] works. The same happens for FileSystem | None, BaseChatModel | None and list[str] | None (available_file_paths).

Why

_normalize_action_function_signature unwraps an optional only when get_origin(annotation) is Union. Before Python 3.14, get_origin(X | None) is types.UnionType, so the union was never unwrapped and was compared to the bare expected type. Python 3.14 merged the two types, which is why this only shows up on the older supported versions (requires-python >= 3.11).

Fix

origin in (Union, UnionType). UnionType was already imported in registry/service.py. browser_use/sandbox/sandbox.py already accepts both forms; this was the only place with the narrower check.

Demo

Registering each signature with Registry().action('demo'), Python 3.12:

                                            main (c75e8476e)   this PR
Optional[BrowserSession]                    OK                 OK
BrowserSession | None                       FAIL (ValueError)  OK
FileSystem | None                           FAIL (ValueError)  OK
list[str] | None (available_file_paths)     FAIL (ValueError)  OK
Optional[list[str]] (available_file_paths)  OK                 OK

Tests

Added to tests/ci/infrastructure/test_registry_validation.py (TestValidationRules):

  • X | None on browser_session, file_system, page_extraction_llm and available_file_paths registers. Fails on main, passes with the fix.
  • typing.Optional[...] still registers. The annotation is set after the function is defined, because pre-commit's pyupgrade would rewrite an Optional[X] written in the signature into X | None, and nothing else covers this spelling.
  • A wrong inner type under | None (browser_session: str | None) is still rejected.

Local results (Python 3.12):

  • tests/ci/infrastructure: 142 passed, 8 skipped.
  • Full tests/ci with -n 8: 1215 passed. 5 failures, unrelated to this change:
    • 4 in integrations/test_anthropic_browser_use.py, an ImportError because the locally installed anthropic SDK has no BetaBrowserScreenshotResult
    • 1, test_beta_agent_initializes_tools_and_action_models, which passes when run on its own

pre-commit passes on both files.

_normalize_action_function_signature unwrapped an optional special
parameter only when get_origin() returned typing.Union. Before Python
3.14 a PEP 604 union such as BrowserSession | None has origin
types.UnionType, so it was compared to the bare expected type and
registration failed with 'conflicts with special argument injected by
tools', while Optional[BrowserSession] worked (browser-use#5905).

Unwrap both origins. UnionType is already imported in the module.
Copilot AI balanced review requested due to automatic review settings October 9, 2026 05:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 issue found across 2 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="browser_use/tools/registry/service.py">

<violation number="1" location="browser_use/tools/registry/service.py:121">
P3: The unwrap picks the first non-`None` argument without confirming the union is actually `X | None`. As a result, a non-Optional multi-type union such as `browser_session: BrowserSession | int` now registers as if it were `BrowserSession`, where it was previously rejected (a `types.UnionType` failed the compatibility check). Restrict normalization to genuine single-type optionals, and let other unions fall through to the existing type check.</violation>
</file>

Reply to a comment to ask cubic a question or push back. It learns from your replies.
Click Fix with cubic on a comment, or reply @cubic-dev-ai fix this, and cubic writes the fix.

View guided diff | Re-trigger cubic

Comment on lines +121 to 124
if origin in (Union, UnionType):
args = get_args(param_type)
# Find non-None type
param_type = next((arg for arg in args if arg is not type(None)), param_type)

@cubic-dev-ai cubic-dev-ai Bot Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P3: The unwrap picks the first non-None argument without confirming the union is actually X | None. As a result, a non-Optional multi-type union such as browser_session: BrowserSession | int now registers as if it were BrowserSession, where it was previously rejected (a types.UnionType failed the compatibility check). Restrict normalization to genuine single-type optionals, and let other unions fall through to the existing type check.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At browser_use/tools/registry/service.py, line 121:

<comment>The unwrap picks the first non-`None` argument without confirming the union is actually `X | None`. As a result, a non-Optional multi-type union such as `browser_session: BrowserSession | int` now registers as if it were `BrowserSession`, where it was previously rejected (a `types.UnionType` failed the compatibility check). Restrict normalization to genuine single-type optionals, and let other unions fall through to the existing type check.</comment>

<file context>
@@ -114,10 +114,11 @@ def _normalize_action_function_signature(
 					param_type = param.annotation
 					origin = get_origin(param_type)
-					if origin is Union:
+					if origin in (Union, UnionType):
 						args = get_args(param_type)
 						# Find non-None type
</file context>
Suggested change
if origin in (Union, UnionType):
args = get_args(param_type)
# Find non-None type
param_type = next((arg for arg in args if arg is not type(None)), param_type)
if origin in (Union, UnionType):
args = get_args(param_type)
# Find non-None type, but only unwrap a true `X | None` optional
non_none_args = [arg for arg in args if arg is not type(None)]
if len(non_none_args) == 1:
param_type = non_none_args[0]
Fix with cubic

@github-actions

Copy link
Copy Markdown

👋 This PR has been automatically marked as stale because it hasn't had activity for 45 days.

To keep this PR open:

  • Rebase against the latest main branch
  • Address any review feedback or merge conflicts
  • Add a comment explaining the current status
  • Add the work-in-progress label if you're still actively working on this

This will be automatically closed in 14 days if no further activity occurs.

Thanks for contributing to browser-use! 🤖

@github-actions github-actions Bot added the stale label Nov 24, 2026

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Registry special-param check rejects PEP 604 X | None (only unwraps typing.Union)

3 participants