Skip to content

Bug: action field returned as JSON string instead of list when done-text contains newlines (Claude Sonnet) #4510

Closed

Description

@JNassar77

Browser Use Version

0.12.3

Bug Description, Steps to Reproduce, Screenshots

Agent crashes on the final done step when the done-text contains
newlines (multi-line summaries, markdown tables).

Steps to reproduce:

  1. Use ChatAnthropic with claude-sonnet-4-6
  2. Run a task that produces a long multi-line summary (e.g. "log in and
    describe all sections you see")
  3. Agent navigates and clicks correctly, but crashes on the done step

Error: Input should be a valid list [type=list_type] or
JSONDecodeError: Expecting ',' delimiter

Root cause: content_block.input['action'] is returned as a JSON string
instead of a list. The string contains literal chr(10) newlines inside
a JSON string value, which breaks json.loads().

Fix applied in our project: escape chr(10/13/9) before json.loads():
cleaned = action_str.replace(chr(10), chr(92)+'n')
_input['action'] = json.loads(cleaned)

Failing Python Code

from browser_use import Agent
from browser_use.llm.anthropic.chat import ChatAnthropic

agent = Agent(
    task="Log into https://example.com with password 'test123'. "
         "Then describe all sections, menus and features you see "
         "in detail with a structured summary.",
    llm=ChatAnthropic(model="claude-sonnet-4-6"),
    max_steps=15,
)
history = await agent.run()
# Crashes at the done step when the summary contains newlines

LLM Model

claude-sonnet-4-6 via ChatAnthropic

Operating System & Browser Versions

Windows 11 + Chromium via CDP (browser-use automatic)

Full DEBUG Log Output

INFO [Agent] Task: Go to https://example.com/, log in, describe sections.
INFO [Agent] Step 3: done action with multi-line text
pydantic_core.ValidationError: 1 validation error for AgentOutput
action
  Input should be a valid list [type=list_type]
  
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 3 column 45

Activity

  1. added
    bug
    on Mar 24, 2026
  2. JNassar77 commented on Mar 24, 2026

    @JNassar77
    Author

    We found a workaround that fixes the issue completely.

    Root cause: The action string contains literal newline characters (chr(10))
    inside the JSON string value. These are not valid in JSON strings, so
    json.loads() fails with "Expecting ',' delimiter".

    Fix applied in browser_use/llm/anthropic/chat.py, before model_validate():

    if isinstance(_input, dict) and 'action' in _input and isinstance(_input['action'], str):
        action_str = _input['action']
        _parsed = False
        # Stage 1: standard json.loads
        try:
            _input['action'] = json.loads(action_str)
            _parsed = True
        except (json.JSONDecodeError, TypeError):
            pass
        # Stage 2: escape literal control chars
        if not _parsed:
            try:
                _BS = chr(92)
                cleaned = action_str
                cleaned = cleaned.replace(chr(10), _BS + 'n')
                cleaned = cleaned.replace(chr(13), _BS + 'r')
                cleaned = cleaned.replace(chr(9),  _BS + 't')
                _input['action'] = json.loads(cleaned)
                _parsed = True
            except (json.JSONDecodeError, TypeError):
                pass
        # Stage 3: synthetic done action as last resort
        if not _parsed:
            import re as _re
            _m = _re.search(r'"done"\s*:\s*\{\s*"text"\s*:\s*"(.+)', action_str, _re.DOTALL)
            raw_text = _m.group(1).rstrip('}' + chr(34) + ' ' + chr(9) + chr(10) + chr(13)) if _m else action_str
            _input['action'] = [{'done': {'text': raw_text}}]
    

    After this fix: agent completes successfully, done action returns full result.

  3. majiayu000 commented on Mar 25, 2026

    @majiayu000

    Hi, I've been looking into this and traced the root cause.

    Proposed approach: In browser_use/llm/anthropic/chat.py, before model_validate() is called, check if _input is a dict with an 'action' key whose value is a str. If so, sanitize the string by replacing literal newlines (\n, \r, \t) with their escaped equivalents before passing to json.loads(). This prevents the 'Expecting , delimiter' parse failure when Claude Sonnet includes newlines in done-text within the action JSON string.

    I'll open a draft PR shortly. Happy to adjust based on your preference.

  4. added a commit that references this issue on Mar 25, 2026
    077c7bd
  5. added a commit that references this issue on Mar 25, 2026
    3459715
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions