diff --git a/.changeset/certain-nice-caracara.md b/.changeset/certain-nice-caracara.md new file mode 100644 index 00000000..9ffabdba --- /dev/null +++ b/.changeset/certain-nice-caracara.md @@ -0,0 +1,5 @@ +--- +"stagehand": patch +--- + +fix: invalid_request_error when using anthropic cua client diff --git a/stagehand/agent/anthropic_cua.py b/stagehand/agent/anthropic_cua.py index df6c7a23..664edf0e 100644 --- a/stagehand/agent/anthropic_cua.py +++ b/stagehand/agent/anthropic_cua.py @@ -295,9 +295,13 @@ def _process_provider_response( if hasattr(response, "content") and isinstance(response.content, list): # Serialize Pydantic models from response.content for history try: - raw_assistant_content_blocks = [ - block.model_dump() for block in response.content - ] + for block in response.content: + block_dict = block.model_dump() + if isinstance(block_dict, dict): + # Anthropic beta responses include a `caller` field on tool_use blocks + # but the API rejects that key on subsequent requests. + block_dict.pop("caller", None) + raw_assistant_content_blocks.append(block_dict) except Exception as e: self.logger.error( f"Could not model_dump response.content blocks: {e}", diff --git a/stagehand/context.py b/stagehand/context.py index cd4b13c5..27ee327a 100644 --- a/stagehand/context.py +++ b/stagehand/context.py @@ -147,6 +147,7 @@ async def _handle_page_close(self, closing_page: StagehandPage): Uses the page switch lock to prevent race conditions with ongoing operations. """ try: + async def handle_with_lock(): async with self.stagehand._page_switch_lock: if self.active_stagehand_page is not closing_page: