learn-claude-code icon indicating copy to clipboard operation
learn-claude-code copied to clipboard

s02运行报错

Open Opao-heng opened this issue 7 months ago • 7 comments

read_file:
#!/usr/bin/env python3

Harness: tool dispatch -- expanding what the model can reach.

"""
s02_tool_use.py - Tool dispatch + message normalization

The agent loop from s01 didn't change. We added tool
Traceback (most recent call last):
File "D:\Python\PyCharm\pycharm2025.2.1.1\PyCharm 2025.2.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1648, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Python\PyCharm\pycharm2025.2.1.1\PyCharm 2025.2.1.1\plugins\python-ce\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Python-Learn\03-learn-claude-code\learn-claude-code\agents\s02_tool_use.py", line 213, in
agent_loop(history)
~~~~~~~~~~^^^^^^^^^
File "D:\Python-Learn\03-learn-claude-code\learn-claude-code\agents\s02_tool_use.py", line 183, in agent_loop
response = client.messages.create(
model=MODEL, system=SYSTEM,
messages=normalize_messages(messages),
tools=TOOLS, max_tokens=8000,
)
File "C:\Users\User.conda\envs\liuheng_Agent\Lib\site-packages\anthropic_utils_utils.py", line 282, in wrapper
return func(*args, **kwargs)
File "C:\Users\User.conda\envs\liuheng_Agent\Lib\site-packages\anthropic\resources\messages\messages.py", line 996, in create
return self._post(
~~~~~~~~~~^
"/v1/messages",
^^^^^^^^^^^^^^^
...<30 lines>...
stream_cls=Stream[RawMessageStreamEvent],
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "C:\Users\User.conda\envs\liuheng_Agent\Lib\site-packages\anthropic_base_client.py", line 1368, in post
return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User.conda\envs\liuheng_Agent\Lib\site-packages\anthropic_base_client.py", line 1141, in request
raise self._make_status_error_from_response(err.response) from None
anthropic.BadRequestError: Error code: 400 - {'error': {'message': 'unexpected messages.2.content.0: tool_use_id found in tool_result blocks:
call_00_LMj5nEG9xTn9mGaEks2vVsPB. Each tool_result block must have a corresponding tool_use block in the previous message.', 'type':
'invalid_request_error', 'param': None, 'code': 'invalid_request_error'}}
python-BaseException 。一直运行不了

Opao-heng avatar Apr 08 '26 13:04 Opao-heng

我真服了,我windows电脑跑不了,是系统的问题,这代码得在mac或者Linux系统

Opao-heng avatar Apr 09 '26 02:04 Opao-heng

s02的代码确实出错了,与你的系统无关。(尽管在Linux中,项目有更好的效果)

W15237928905 avatar Apr 09 '26 02:04 W15237928905

s02的代码确实出错了,与你的系统无关。(尽管在Linux中,项目有更好的效果)

我s02 s03都是这样报错,空字符,怪死了,怎么改都不对

Opao-heng avatar Apr 09 '26 02:04 Opao-heng

Image 这里改回去就行了

rongdanfeng avatar Apr 09 '26 05:04 rongdanfeng

这个消息规范化函数是有点问题,把TextBlock和ToolUseBlock当成dict来处理了 导致规范化后的消息丢了,我转成openapi消息,tool消息又必须对应一个tool_calls消息。。。。 先去掉这块了勉强能跑

meissdh1122 avatar Apr 09 '26 13:04 meissdh1122

This issue is fixed in PR #207.

Root cause: normalize_messages() uses isinstance(block, dict) to filter content blocks, but the Anthropic SDK stores response.content as pydantic model objects (ToolUseBlock, TextBlock), not plain dicts. This silently drops all tool_use blocks during normalization, so the subsequent tool_result references a tool_use_id the API never received — hence the 400 error.

Fix: Added a _block_to_dict() helper that converts SDK objects to plain dicts via model_dump(), ensuring all content blocks are properly serialized before sending to the API.

See: https://github.com/shareAI-lab/learn-claude-code/pull/207

wadeKeith avatar Apr 10 '26 11:04 wadeKeith

This issue is fixed in PR #207.

Root cause: normalize_messages() uses isinstance(block, dict) to filter content blocks, but the Anthropic SDK stores response.content as pydantic model objects (ToolUseBlock, TextBlock), not plain dicts. This silently drops all tool_use blocks during normalization, so the subsequent tool_result references a tool_use_id the API never received — hence the 400 error.

Fix: Added a _block_to_dict() helper that converts SDK objects to plain dicts via model_dump(), ensuring all content blocks are properly serialized before sending to the API.

See: #207

good thanks!!

xiaocc2012 avatar Apr 11 '26 08:04 xiaocc2012