anthropics / anthropics/anthropic-sdk-python

[Bug] append_messages() inside tool runner loop causes infinite loop (advanced usage docs example)

Đang mở
#1,536 4 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
3.9k
Fork
853
Merge trung bình
1 ngày 11 giờ
Pull request đã merge (30 ngày)
10

Mô tả

[Link to Advanced Usage Docs Example
](https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-runner#:~:text=(block.text)-,Advanced%20usage,-Within%20the%20loop)
# Root cause

Calling `runner.append_messages(user_message)` inside the loop sets
`_messages_modified = True`, which causes `__run__` to skip the auto-append
of the assistant message + tool result (line 281 of `_beta_runner.py`).
The next iteration sends a request with no tool result in history, so Claude
sees the original question unanswered and makes the tool call again.

## Minimal repro

Compacted and consolidated from the website

```
from anthropic import beta_tool, Anthropic

client = Anthropic()

@beta_tool
def get_weather(location: str, unit: str = 'fahrenheit') -> str:
return json.dumps({
'temperature': '20°c', 'condition': 'sunny'
})

runner = client.beta.messages.tool_runner(
model="claude-haiku-4-5",
max_tokens=1024,
tools=[get_weather],
messages=[{
"role": "user",
"content": "what's the weather in San Francisco?"
}]
)

for message in runner:
tool_response = runner.generate_tool_call_response()
if tool_response:
print(f"Tool result: {tool_response}")

# increase tokens for next request
runner.set_messages_params(
lambda params: {
**params,
"max_tokens": 2048,
}
)

# add additional messages
runner.append_messages(
{"role": "user", "content": "be concise in your response"}
)
```

## Expected behavior
The "be concise" user message is appended, tool results are still
auto-appended, loop terminates.

## Actual behavior
Infinite loop — tool result never reaches history.

## Note
The "Modifying tool results" example on the same page correctly calls
`runner.append_messages(message, tool_response)` (both the assistant
message and the tool result), which is the pattern that works correctly.

The "Advanced usage" example is inconsistent with this.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.