Skip to content

Commit 69ef3f5

Browse files
psousa50claude
andcommitted
fix: filter extra fields from tool_use blocks to prevent Anthropic API rejection
Anthropic's API returns a `caller` field in `tool_use` response blocks but rejects this field when sent back in subsequent requests. This filters `tool_use` blocks to only include accepted fields: `type`, `id`, `name`, and `input`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0ec5197 commit 69ef3f5

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

stagehand/agent/anthropic_cua.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626

2727
class AnthropicCUAClient(AgentClient):
28+
ALLOWED_TOOL_USE_FIELDS = {"type", "id", "name", "input"}
29+
2830
ANTHROPIC_KEY_MAPPING = {
2931
"return": "Enter",
3032
"enter": "Enter",
@@ -283,6 +285,15 @@ def _format_initial_messages(
283285
messages.append({"role": "user", "content": user_content})
284286
return messages
285287

288+
def _sanitise_content_block(self, block_data: dict[str, Any]) -> dict[str, Any]:
289+
if block_data.get("type") == "tool_use":
290+
return {
291+
k: v
292+
for k, v in block_data.items()
293+
if k in self.ALLOWED_TOOL_USE_FIELDS
294+
}
295+
return block_data
296+
286297
def _process_provider_response(
287298
self, response: Any # Anthropic API response object
288299
) -> tuple[Optional[AgentAction], Optional[str], bool, list[dict[str, Any]]]:
@@ -293,10 +304,10 @@ def _process_provider_response(
293304

294305
raw_assistant_content_blocks = []
295306
if hasattr(response, "content") and isinstance(response.content, list):
296-
# Serialize Pydantic models from response.content for history
297307
try:
298308
raw_assistant_content_blocks = [
299-
block.model_dump() for block in response.content
309+
self._sanitise_content_block(block.model_dump())
310+
for block in response.content
300311
]
301312
except Exception as e:
302313
self.logger.error(

0 commit comments

Comments
 (0)