Repository navigation
Conversation
_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.
|
|
There was a problem hiding this comment.
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
| 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) |
There was a problem hiding this comment.
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>
| 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] |
|
👋 This PR has been automatically marked as stale because it hasn't had activity for 45 days. To keep this PR open:
This will be automatically closed in 14 days if no further activity occurs. Thanks for contributing to browser-use! 🤖 |
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:
Optional[BrowserSession]works. The same happens forFileSystem | None,BaseChatModel | Noneandlist[str] | None(available_file_paths).Why
_normalize_action_function_signatureunwraps an optional only whenget_origin(annotation) is Union. Before Python 3.14,get_origin(X | None)istypes.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).UnionTypewas already imported inregistry/service.py.browser_use/sandbox/sandbox.pyalready accepts both forms; this was the only place with the narrower check.Demo
Registering each signature with
Registry().action('demo'), Python 3.12:Tests
Added to
tests/ci/infrastructure/test_registry_validation.py(TestValidationRules):X | Noneonbrowser_session,file_system,page_extraction_llmandavailable_file_pathsregisters. Fails onmain, passes with the fix.typing.Optional[...]still registers. The annotation is set after the function is defined, because pre-commit's pyupgrade would rewrite anOptional[X]written in the signature intoX | None, and nothing else covers this spelling.| None(browser_session: str | None) is still rejected.Local results (Python 3.12):
tests/ci/infrastructure: 142 passed, 8 skipped.tests/ciwith-n 8: 1215 passed. 5 failures, unrelated to this change:integrations/test_anthropic_browser_use.py, anImportErrorbecause the locally installedanthropicSDK has noBetaBrowserScreenshotResulttest_beta_agent_initializes_tools_and_action_models, which passes when run on its ownpre-commitpasses on both files.