FoundationAgents/MetaGPT · error · ValueError

Invalid press action {step}

Error message

Invalid press action {step}

What it means

MetaGPT's browser agent parses LLM-emitted action strings (e.g. 'press [Enter]') inside execute_action in metagpt/utils/a11y_tree.py. When the action is classified as a press but the regex r'press ?\[(.+)\]' fails to match the step text, this ValueError is raised. It means the LLM output deviated from the expected 'press [<key>]' format.

Source

Thrown at metagpt/utils/a11y_tree.py:60

        if not (step.endswith("[0]") or step.endswith("[1]")):
            step += " [1]"

        match = re.search(r"type ?\[(\d+)\] ?\[(.+)\] ?\[(\d+)\]", step)
        if not match:
            raise ValueError(f"Invalid type action {step}")
        element_id, text, enter_flag = (
            match.group(1),
            match.group(2),
            match.group(3),
        )
        if enter_flag == "1":
            text += "\n"
        await click_element(page, get_backend_node_id(element_id, accessibility_tree))
        await type_text(page, text)
    elif func == "press":
        match = re.search(r"press ?\[(.+)\]", step)
        if not match:
            raise ValueError(f"Invalid press action {step}")
        key = match.group(1)
        await key_press(page, key)
    elif func == "scroll":
        # up or down
        match = re.search(r"scroll ?\[?(up|down)\]?", step)
        if not match:
            raise ValueError(f"Invalid scroll action {step}")
        direction = match.group(1)
        await scroll_page(page, direction)
    elif func == "goto":
        match = re.search(r"goto ?\[(.+)\]", step)
        if not match:
            raise ValueError(f"Invalid goto action {step}")
        url = match.group(1)
        await page.goto(url)
    elif func == "new_tab":
        page = await browser_ctx.new_page()
    elif func == "go_back":

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Check the raw LLM response and the extracted step string; log it before execute_action is called.
  2. Make the action conform: use exactly 'press [Enter]' / 'press [Control+a]' with square brackets.
  3. Tighten the prompt/instruction template so the model always emits press actions in bracket form.
  4. Pre-validate the step with your own regex before calling execute_action, and re-ask the LLM on mismatch.

Example fix

# before
await execute_action(page, 'press Enter')  # raises Invalid press action

# after
await execute_action(page, 'press [Enter]')
Defensive patterns

Strategy: validation

Validate before calling

import re

def is_valid_press(step: str) -> bool:
    return bool(re.search(r"press ?\[(.+)\]", step))

Try / catch

try:
    await execute_action(page, step)
except ValueError as e:
    if 'Invalid press action' in str(e):
        # log step, re-ask the LLM with format instructions
        ...

Prevention

When it happens

Trigger: execute_action(page, 'press') or 'press Enter' without square brackets; 'press[space' with unbalanced brackets; an LLM response like 'press "Enter"' using quotes instead of brackets; trailing characters that break the (.+) capture after the closing bracket.

Common situations: LLM action hallucination where the model omits the bracket syntax, includes extra prose around the action, or emits an empty key. Also occurs when a custom action_splitter mangles the extracted step so the leading 'press [...]' fragment is truncated.

Related errors


AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14). Data as JSON: /api/errors/1fd782bfd753ffd4. Report an issue: GitHub.