MCPcopy Create free account
hub / github.com/anthropics/anthropic-sdk-python / _run_with_tool_changes

Function _run_with_tool_changes

tests/lib/tools/test_runners.py:1644–1674  ·  view source on GitHub ↗

Drive a tool runner to the end, running `script[message.id]` while each message is being handled.

(
    client: Anthropic | AsyncAnthropic,
    *,
    tools: list[Any],
    script: _ToolChangeScript,
    before_first_request: Callable[[Any], None] | None = None,
    max_iterations: int | Omit = omit,
    messages: list[BetaMessageParam] | None = None,
    stream: bool = False,
)

Source from the content-addressed store, hash-verified

1642
1643
1644async def _run_with_tool_changes(
1645 client: Anthropic | AsyncAnthropic,
1646 *,
1647 tools: list[Any],
1648 script: _ToolChangeScript,
1649 before_first_request: Callable[[Any], None] | None = None,
1650 max_iterations: int | Omit = omit,
1651 messages: list[BetaMessageParam] | None = None,
1652 stream: bool = False,
1653) -> None:
1654 """Drive a tool runner to the end, running `script[message.id]` while each message is being handled."""
1655 runner: Any = client.beta.messages.tool_runner(
1656 max_tokens=1024,
1657 model="claude-haiku-4-5",
1658 tools=tools,
1659 messages=messages or [{"role": "user", "content": "What time is it, and what is the weather in SF?"}],
1660 max_iterations=max_iterations,
1661 stream=stream,
1662 # The tests assert on whole request bodies; on a message thread a request carries only the new messages.
1663 thread=False,
1664 )
1665 if before_first_request is not None:
1666 before_first_request(runner)
1667 if isinstance(client, Anthropic):
1668 for item in runner:
1669 message = item.get_final_message() if stream else item
1670 script.get(message.id, _leave_tools_alone)(runner)
1671 else:
1672 async for item in runner:
1673 message = await item.get_final_message() if stream else item
1674 script.get(message.id, _leave_tools_alone)(runner)
1675
1676
1677def _leave_tools_alone(runner: Any) -> None:

Calls 3

tool_runnerMethod · 0.45
get_final_messageMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected