|
1 | 1 | """ |
2 | | -Agentic sampling loop that calls the Anthropic API and local implementation of anthropic-defined computer use tools. |
| 2 | +Agentic sampling loop that calls the Claude API and local implementation of anthropic-defined computer use tools. |
3 | 3 | """ |
4 | 4 |
|
5 | 5 | import platform |
@@ -169,15 +169,17 @@ async def sampling_loop( |
169 | 169 | tool_result_content: list[BetaToolResultBlockParam] = [] |
170 | 170 | for content_block in response_params: |
171 | 171 | output_callback(content_block) |
172 | | - if content_block["type"] == "tool_use": |
| 172 | + if isinstance(content_block, dict) and content_block.get("type") == "tool_use": |
| 173 | + # Type narrowing for tool use blocks |
| 174 | + tool_use_block = cast(BetaToolUseBlockParam, content_block) |
173 | 175 | result = await tool_collection.run( |
174 | | - name=content_block["name"], |
175 | | - tool_input=cast(dict[str, Any], content_block["input"]), |
| 176 | + name=tool_use_block["name"], |
| 177 | + tool_input=cast(dict[str, Any], tool_use_block.get("input", {})), |
176 | 178 | ) |
177 | 179 | tool_result_content.append( |
178 | | - _make_api_tool_result(result, content_block["id"]) |
| 180 | + _make_api_tool_result(result, tool_use_block["id"]) |
179 | 181 | ) |
180 | | - tool_output_callback(result, content_block["id"]) |
| 182 | + tool_output_callback(result, tool_use_block["id"]) |
181 | 183 |
|
182 | 184 | if not tool_result_content: |
183 | 185 | return messages |
@@ -277,7 +279,8 @@ def _inject_prompt_caching( |
277 | 279 | {"type": "ephemeral"} |
278 | 280 | ) |
279 | 281 | else: |
280 | | - content[-1].pop("cache_control", None) |
| 282 | + if isinstance(content[-1], dict) and "cache_control" in content[-1]: |
| 283 | + del content[-1]["cache_control"] # type: ignore |
281 | 284 | # we'll only every have one extra turn per loop |
282 | 285 | break |
283 | 286 |
|
|
0 commit comments