anthropics / anthropics/claude-cookbooks
[BUG] Summarization cookbook fails due to use of assistant message prefill
- 主要言語
- Jupyter Notebook
- スター
- 52.7k
- フォーク
- 6.3k
- 平均マージ
- 25分
- マージ済み PR(30日)
- 6
説明
### Affected Notebook/File
capabilities/summarization/guide.ipynb
### Bug Description
Cell 8 in this notebook results in a BadRequestError due to the use of assistant message prefill (as expected since that is now deprecated).
### Steps to Reproduce
Run cells 1-8 of the notebook
### Error Message
```shell
---------------------------------------------------------------------------
BadRequestError Traceback (most recent call last)
Cell In[8], line 1
----> 1 basic_response = basic_summarize(text, max_tokens=1000)
3 print(basic_response)
Cell In[3], line 7, in basic_summarize(text, max_tokens)
1 def basic_summarize(text, max_tokens=1000):
2 # Prompt the model to summarize the text
3 prompt = f"""Summarize the following text in bullet points. Focus on the main ideas and key details:
4 {text}
5 """
----> 7 response = client.messages.create(
8 model="claude-sonnet-4-6",
9 max_tokens=max_tokens,
10 system="You are a legal analyst known for highly accurate and detailed summaries of legal documents.",
11 messages=[
12 {"role": "user", "content": prompt},
13 {
14 "role": "assistant",
15 "content": "Here is the summary of the legal document: ",
16 },
17 ],
18 stop_sequences=[""],
19 )
21 return response.content[0].text
File ~/src/claude-cookbooks/.venv/lib/python3.12/site-packages/anthropic/_utils/_utils.py:294, in required_args..inner..wrapper(*args, **kwargs)
292 msg = f"Missing required argument: {quote(missing[0])}"
293 raise TypeError(msg)
--> 294 return func(*args, **kwargs)
File ~/src/claude-cookbooks/.venv/lib/python3.12/site-packages/anthropic/resources/messages/messages.py:1003, in Messages.create(self, max_tokens, messages, model, cache_control, container, inference_geo, metadata, output_config, service_tier, stop_sequences, stream, system, temperature, thinking, tool_choice, tools, top_k, top_p, extra_headers, extra_query, extra_body, timeout)
996 if model in MODELS_TO_WARN_WITH_THINKING_ENABLED and thinking and thinking["type"] == "enabled":
997 warnings.warn(
998 f"Using Claude with {model} and 'thinking.type=enabled' is deprecated. Use 'thinking.type=adaptive' instead which results in better model performance in our testing: https://platform.claude.com/docs/en/build-with-claude/adaptive-thinking",
999 UserWarning,
1000 stacklevel=3,
1001 )
-> 1003 return self._post(
1004 "/v1/messages",
1005 body=maybe_transform(
1006 {
1007 "max_tokens": max_tokens,
1008 "messages": messages,
1009 "model": model,
1010 "cache_control": cache_control,
1011 "container": container,
1012 "inference_geo": inference_geo,
1013 "metadata": metadata,
1014 "output_config": output_config,
1015 "service_tier": service_tier,
1016 "stop_sequences": stop_sequences,
1017 "stream": stream,
1018 "system": system,
1019 "temperature": temperature,
1020 "thinking": thinking,
1021 "tool_choice": tool_choice,
1022 "tools": tools,
1023 "top_k": top_k,
1024 "top_p": top_p,
1025 },
1026 message_create_params.MessageCreateParamsStreaming
1027 if stream
1028 else message_create_params.MessageCreateParamsNonStreaming,
1029 ),
1030 options=make_request_options(
1031 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
1032 ),
1033 cast_to=Message,
1034 stream=stream or False,
1035 stream_cls=Stream[RawMessageStreamEvent],
1036 )
File ~/src/claude-cookbooks/.venv/lib/python3.12/site-packages/anthropic/_base_client.py:1374, in SyncAPIClient.post(self, path, cast_to, body, content, options, files, stream, stream_cls)
1365 warnings.warn(
1366 "Passing raw bytes as `body` is deprecated and will be removed in a future version. "
1367 "Please pass raw bytes via the `content` parameter instead.",
1368 DeprecationWarning,
1369 stacklevel=2,
1370 )
1371 opts = FinalRequestOptions.construct(
1372 method="post", url=path, json_data=body, content=content, files=to_httpx_files(files), **options
1373 )
-> 1374 return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
File ~/src/claude-cookbooks/.venv/lib/python3.12/site-packages/anthropic/_base_client.py:1147, in SyncAPIClient.request(self, cast_to, options, stream, stream_cls)
1144 err.response.read()
1146 log.debug("Re-raising status error")
-> 1147 raise self._make_status_error_from_response(err.response) from None
1149 break
1151 assert response is not None, "could not resolve response (should never happen)"
BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'This model does not support assistant message prefill. The conversation must end with a user message.'}, 'request_id': 'req_011CayDpya9P9poQn9ACDoGq'}
```
### Environment
_No response_
### Would you be willing to submit a PR to fix this?
None
コントリビューションガイド
評価
この issue はまだ評価されていません。