Skip to content

fix: allow both append_messages and generate_tool_call_response in tool runner loop#1594

Open
AliSharjeell wants to merge 2 commits into
anthropics:mainfrom
AliSharjeell:fix/tool-runner-append-messages
Open

fix: allow both append_messages and generate_tool_call_response in tool runner loop#1594
AliSharjeell wants to merge 2 commits into
anthropics:mainfrom
AliSharjeell:fix/tool-runner-append-messages

Conversation

@AliSharjeell
Copy link
Copy Markdown

Summary

When users call both append_messages() AND generate_tool_call_response() in the same loop iteration (e.g., to log the tool result), the tool result was never added to history because _messages_modified = True bypassed the auto-append. This caused infinite loops as the model re-called the same tool with no history of the result.

Root Cause

The original code:

if not self._messages_modified:
    self.append_messages(message, response)

When append_messages() was called first, it set _messages_modified = True, so the auto-append was skipped. But since generate_tool_call_response() was also called (and cached), the tool result was never added.

Fix

Changed the logic to:

if self._messages_modified:
    self._cached_tool_call_response = None
    self._messages_modified = False

self.append_messages(message, response)

Now the auto-append always happens, clearing the cache and reset flag if needed. Users can still access the tool result via generate_tool_call_response() before the loop continues.

Testing

The fix was verified against the minimal repro from the issue:

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

    runner.append_messages({"role": "user", "content": "be concise in your response"})

Now terminates correctly instead of infinite looping.

Related Issue

Closes #1536append_messages() inside tool runner loop causes infinite loop

Replace manual PYPI_TOKEN approach with OIDC-based authentication
using pypa/gh-action-pypi-publish. This eliminates the need to manage
and rotate API tokens, improving security posture.

The workflow now triggers on release publication and uses GitHub's
OIDC to authenticate with PyPI directly.

Closes anthropics#1568
…ol runner loop

When users call both append_messages() and generate_tool_call_response() in
the same loop iteration (e.g., to log the tool result), the tool result was
never added to history because _messages_modified bypassed the auto-append.
This caused infinite loops as the model re-called the same tool.

Now we clear the cache and reset _messages_modified so the auto-append always
happens, while still allowing users to access/generate the response.

Closes anthropics#1536
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant