anthropics / anthropic-quickstarts

A collection of projects designed to help developers quickly get started with building deployable applications using the Anthropic API
MIT License
6.55k stars 904 forks source link

You need to trap errors encountered during tool_use #135

Open p-i- opened 1 week ago

p-i- commented 1 week ago

Consider the following crash dump:

> ./run.sh
Container started with ID: 55e7749387c5f72b4b5a05efc8ee9184d547391bf28d95e65d910b2ef8f44da3
Waiting for container 55e7749387c5f72b4b5a05efc8ee9184d547391bf28d95e65d910b2ef8f44da3 to reach running state...
Container 55e7749387c5f72b4b5a05efc8ee9184d547391bf28d95e65d910b2ef8f44da3 is now running.

Xvfb started successfully on display :1
Xvfb PID: 9
starting tint2 on display :1 ...
starting mutter
starting vnc
PORT=5904
starting noVNC
noVNC started successfully
✨ Computer Use Demo is ready!
➡️  Open http://localhost:8084 in your browser to begin
2024-10-29 14:44:49.092 Uncaught app exception
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/dist-packages/streamlit/runtime/scriptrunner/exec_code.py", line 88, in exec_func_with_error_handling
    result = func()
             ^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/streamlit/runtime/scriptrunner/script_runner.py", line 579, in code_to_exec
    exec(code, module.__dict__)
  File "/host/src/computer_use_demo/streamlit.py", line 384, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.11/asyncio/runners.py", line 188, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/runners.py", line 120, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/base_events.py", line 650, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/host/src/computer_use_demo/streamlit.py", line 214, in main
    st.session_state.messages = await sampling_loop(
                                ^^^^^^^^^^^^^^^^^^^^
  File "/host/src/computer_use_demo/loop.py", line 165, in sampling_loop
    result = await tool_collection.run(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/host/src/computer_use_demo/tools/collection.py", line 32, in run
    return await tool(**tool_input)
                 ^^^^^^^^^^^^^^^^^^
TypeError: EditTool.__call__() missing 1 required keyword-only argument: 'path'

The agent loop has used a tool incorrectly, and this has crashed the process.

This means the conversation is now lost. I encounter this kind of crash several times a day.

This kind of error should be trapped, so that the next agent cycle has an opportunity to invoke the tool correctly.

I'm connecting this issue with https://github.com/anthropics/anthropic-quickstarts/issues/121 as that represents another error that should be trapped thus allowing the agent loop to continue.

p-i- commented 1 week ago

Might be as simple as:

diff --git a/src/computer_use_demo/tools/collection.py b/src/computer_use_demo/tools/collection.py
index c4e8c95..ef17ed5 100644
--- a/src/computer_use_demo/tools/collection.py
+++ b/src/computer_use_demo/tools/collection.py
@@ -32,3 +32,9 @@ class ToolCollection:
             return await tool(**tool_input)
         except ToolError as e:
             return ToolFailure(error=e.message)
+        except TypeError as e:
+            # Handle missing required arguments
+            return ToolFailure(error=f"Invalid tool arguments: {str(e)}")
+        except Exception as e:
+            # Catch all other exceptions and wrap them
+            return ToolFailure(error=f"Tool execution failed: {str(e)}")
p-i- commented 1 week ago
Screenshot 2024-10-29 at 17 08 08

Ya Hah! 🐛🔨That was a crash before.

x5a commented 1 week ago

Caused by this now resolved incident. I'll keep this one open until we address the unhandled exception in the demo. @p-i- would you be willing to open a PR?

p-i- commented 1 week ago

@x5a Please wire it in yourself, simpler!

libertyteeth commented 6 days ago

Caused by this now resolved incident. I'll keep this one open until we address the unhandled exception in the demo. @p-i- would you be willing to open a PR?

I believe that I am experiencing this issue. The below happened just an hour or two ago. I see this text in the link you provided:

"Between 21:00 UTC on October 28 through 04:15 UTC on October 30, API calls invoking tool use intermittently failed to include all parameters, including required parameters. This issue primarily affected requests to claude-3-5-sonnet-20241022 and claude-3-haiku-20240307, and has now been resolved."

According to https://www.utctime.net/ , the announcement indicates that this was supposed to have been resolved about 25 1/2 hours ago.

Here is the error, from here to the end of the comment. Thank you for your assistance.

File "/home/computeruse/computer_use_demo/loop.py", line 132, in sampling_loop raw_response = client.beta.messages.with_raw_response.create( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/home/computeruse/.pyenv/versions/3.11.6/lib/python3.11/site-packages/anthropic/_legacy_response.py", line 377, in wrapped return cast(LegacyAPIResponse[R], func(*args, **kwargs)) ^^^^^^^^^^^^^^^^^^^^^

File "/home/computeruse/.pyenv/versions/3.11.6/lib/python3.11/site-packages/anthropic/_utils/_utils.py", line 274, in wrapper return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^

File "/home/computeruse/.pyenv/versions/3.11.6/lib/python3.11/site-packages/anthropic/resources/beta/messages/messages.py", line 896, in create return self._post( ^^^^^^^^^^^

File "/home/computeruse/.pyenv/versions/3.11.6/lib/python3.11/site-packages/anthropic/_base_client.py", line 1277, in post return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/home/computeruse/.pyenv/versions/3.11.6/lib/python3.11/site-packages/anthropic/_base_client.py", line 954, in request return self._request( ^^^^^^^^^^^^^^

File "/home/computeruse/.pyenv/versions/3.11.6/lib/python3.11/site-packages/anthropic/_base_client.py", line 1058, in _request raise self._make_status_error_from_response(err.response) from None

anthropic.BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages.78: Did not find 1 tool_result block(s) at the beginning of this message. Messages following tool_use blocks must begin with a matching number of tool_result blocks.'}

x5a commented 6 days ago

Caused by this now resolved incident. I'll keep this one open until we address the unhandled exception in the demo. @p-i- would you be willing to open a PR?

I believe that I am experiencing this issue. The below happened just an hour or two ago. I see this text in the link you provided:

"Between 21:00 UTC on October 28 through 04:15 UTC on October 30, API calls invoking tool use intermittently failed to include all parameters, including required parameters. This issue primarily affected requests to claude-3-5-sonnet-20241022 and claude-3-haiku-20240307, and has now been resolved."

According to https://www.utctime.net/ , the announcement indicates that this was supposed to have been resolved about 25 1/2 hours ago.

Here is the error, from here to the end of the comment. Thank you for your assistance.

File "/home/computeruse/computer_use_demo/loop.py", line 132, in sampling_loop raw_response = client.beta.messages.with_raw_response.create( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/home/computeruse/.pyenv/versions/3.11.6/lib/python3.11/site-packages/anthropic/_legacy_response.py", line 377, in wrapped return cast(LegacyAPIResponse[R], func(*args, **kwargs)) ^^^^^^^^^^^^^^^^^^^^^

File "/home/computeruse/.pyenv/versions/3.11.6/lib/python3.11/site-packages/anthropic/_utils/_utils.py", line 274, in wrapper return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^

File "/home/computeruse/.pyenv/versions/3.11.6/lib/python3.11/site-packages/anthropic/resources/beta/messages/messages.py", line 896, in create return self._post( ^^^^^^^^^^^

File "/home/computeruse/.pyenv/versions/3.11.6/lib/python3.11/site-packages/anthropic/_base_client.py", line 1277, in post return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/home/computeruse/.pyenv/versions/3.11.6/lib/python3.11/site-packages/anthropic/_base_client.py", line 954, in request return self._request( ^^^^^^^^^^^^^^

File "/home/computeruse/.pyenv/versions/3.11.6/lib/python3.11/site-packages/anthropic/_base_client.py", line 1058, in _request raise self._make_status_error_from_response(err.response) from None

anthropic.BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages.78: Did not find 1 tool_result block(s) at the beginning of this message. Messages following tool_use blocks must begin with a matching number of tool_result blocks.'}

@libertyteeth the error you're seeing is not symptomatic of the outage on Tuesday and instead indicates that your client (the demo) is sending a malformed request to the API. My best guess that you are encountering a known issue that results in the human turn request after a client side tool failure failing. If you have more information about the tool error that occurred before the API error, feel free to open a new issue.

Quasimondo commented 2 days ago

I am seeing problem with all tool use again this morning: image

BadRequestError

Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': "messages.1.content.1.tooluse.name: String should match pattern '^[a-zA-Z0-9-]{1,64}$'"}}

Is this again something that happens on the anthropic API side or a local problem of mine?