mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
fix(anthropic): strip trailing whitespace from final assistant message (#35072)
This commit is contained in:
@@ -643,6 +643,17 @@ def _format_messages(
|
||||
_lc_tool_calls_to_anthropic_tool_use_blocks(missing_tool_calls),
|
||||
)
|
||||
|
||||
if role == "assistant" and _i == len(merged_messages) - 1:
|
||||
if isinstance(content, str):
|
||||
content = content.rstrip()
|
||||
elif (
|
||||
isinstance(content, list)
|
||||
and content
|
||||
and isinstance(content[-1], dict)
|
||||
and content[-1].get("type") == "text"
|
||||
):
|
||||
content[-1]["text"] = content[-1]["text"].rstrip()
|
||||
|
||||
if not content and role == "assistant" and _i < len(merged_messages) - 1:
|
||||
# anthropic.BadRequestError: Error code: 400: all messages must have
|
||||
# non-empty content except for the optional final assistant message
|
||||
|
||||
Reference in New Issue
Block a user