From 19541d9d07ec29cb25851ac32f711c2767f6fde4 Mon Sep 17 00:00:00 2001 From: Soulter <37870767+Soulter@users.noreply.github.com> Date: Fri, 26 Dec 2025 21:01:05 +0800 Subject: [PATCH] fix: ensure max_tokens is set and validate tool_calls type in ProviderAnthropic (#4212) --- astrbot/core/provider/sources/anthropic_source.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/astrbot/core/provider/sources/anthropic_source.py b/astrbot/core/provider/sources/anthropic_source.py index 0ff61e393..6c9f62e29 100644 --- a/astrbot/core/provider/sources/anthropic_source.py +++ b/astrbot/core/provider/sources/anthropic_source.py @@ -68,7 +68,7 @@ class ProviderAnthropic(Provider): blocks = [] if isinstance(message["content"], str): blocks.append({"type": "text", "text": message["content"]}) - if "tool_calls" in message: + if "tool_calls" in message and isinstance(message["tool_calls"], list): for tool_call in message["tool_calls"]: blocks.append( # noqa: PERF401 { @@ -132,6 +132,9 @@ class ProviderAnthropic(Provider): extra_body = self.provider_config.get("custom_extra_body", {}) + if "max_tokens" not in payloads: + payloads["max_tokens"] = 1024 + completion = await self.client.messages.create( **payloads, stream=False, extra_body=extra_body ) @@ -181,6 +184,9 @@ class ProviderAnthropic(Provider): usage = TokenUsage() extra_body = self.provider_config.get("custom_extra_body", {}) + if "max_tokens" not in payloads: + payloads["max_tokens"] = 1024 + async with self.client.messages.stream( **payloads, extra_body=extra_body ) as stream: @@ -342,11 +348,11 @@ class ProviderAnthropic(Provider): async def text_chat_stream( self, - prompt, + prompt=None, session_id=None, - image_urls=..., + image_urls=None, func_tool=None, - contexts=..., + contexts=None, system_prompt=None, tool_calls_result=None, model=None,