Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

fix anthropic action field double-serialization

Laith Weinberger8e8ab7357501
diff --git a/browser_use/llm/anthropic/chat.py b/browser_use/llm/anthropic/chat.py
index 3e4afebc4..e09baec7d 100644
--- a/browser_use/llm/anthropic/chat.py
+++ b/browser_use/llm/anthropic/chat.py
@@ -223,15 +223,29 @@ class ChatAnthropic(BaseChatModel):
stop_reason=response.stop_reason,
)
except Exception as e:
- # If validation fails, try to parse it as JSON first
- if isinstance(content_block.input, str):
- data = json.loads(content_block.input)
- return ChatInvokeCompletion(
- completion=output_format.model_validate(data),
- usage=usage,
- stop_reason=response.stop_reason,
- )
- raise e
+ # If validation fails, try to fix common model output issues
+ _input = content_block.input
+ if isinstance(_input, str):
+ _input = json.loads(_input)
+ elif isinstance(_input, dict):
+ # Model sometimes double-serializes fields
+ for key, value in _input.items():
+ if isinstance(value, str) and value.startswith(('[', '{')):
+ try:
+ _input[key] = json.loads(value)
+ except json.JSONDecodeError:
+ cleaned = value.replace('\n', '\\n').replace('\r', '\\r').replace('\t', '\\t')
+ try:
+ _input[key] = json.loads(cleaned)
+ except json.JSONDecodeError:
+ pass
+ else:
+ raise e
+ return ChatInvokeCompletion(
+ completion=output_format.model_validate(_input),
+ usage=usage,
+ stop_reason=response.stop_reason,
+ )
# If no tool use block found, raise an error
raise ValueError('Expected tool use in response but none found')
diff --git a/browser_use/llm/aws/chat_anthropic.py b/browser_use/llm/aws/chat_anthropic.py
index 8515f5660..2213e889a 100644
--- a/browser_use/llm/aws/chat_anthropic.py
+++ b/browser_use/llm/aws/chat_anthropic.py
@@ -222,14 +222,28 @@ class ChatAnthropicBedrock(ChatAWSBedrock):
try:
return ChatInvokeCompletion(completion=output_format.model_validate(content_block.input), usage=usage)
except Exception as e:
- # If validation fails, try to parse it as JSON first
- if isinstance(content_block.input, str):
- data = json.loads(content_block.input)
- return ChatInvokeCompletion(
- completion=output_format.model_validate(data),
- usage=usage,
- )
- raise e
+ # If validation fails, try to fix common model output issues
+ _input = content_block.input
+ if isinstance(_input, str):
+ _input = json.loads(_input)
+ elif isinstance(_input, dict):
+ # Model sometimes double-serializes fields
+ for key, value in _input.items():
+ if isinstance(value, str) and value.startswith(('[', '{')):
+ try:
+ _input[key] = json.loads(value)
+ except json.JSONDecodeError:
+ cleaned = value.replace('\n', '\\n').replace('\r', '\\r').replace('\t', '\\t')
+ try:
+ _input[key] = json.loads(cleaned)
+ except json.JSONDecodeError:
+ pass
+ else:
+ raise e
+ return ChatInvokeCompletion(
+ completion=output_format.model_validate(_input),
+ usage=usage,
+ )
# If no tool use block found, raise an error
raise ValueError('Expected tool use in response but none found')