Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

fix: anthropic system message parse (#1467)

cthomasdf899ee05b5c
diff --git a/letta/errors.py b/letta/errors.py
index d5d832b08..a79d5bdf1 100644
--- a/letta/errors.py
+++ b/letta/errors.py
@@ -70,6 +70,14 @@ class LLMRateLimitError(LLMError):
"""Error when rate limited by LLM service"""
+class LLMBadRequestError(LLMError):
+ """Error when LLM service cannot process request"""
+
+
+class LLMAuthenticationError(LLMError):
+ """Error when authentication fails with LLM service"""
+
+
class LLMPermissionDeniedError(LLMError):
"""Error when permission is denied by LLM service"""
@@ -82,6 +90,10 @@ class LLMUnprocessableEntityError(LLMError):
"""Error when request is well-formed but semantically invalid"""
+class LLMServerError(LLMError):
+ """Error when LLM service encounters an internal error"""
+
+
class BedrockPermissionError(LettaError):
"""Exception raised for errors in the Bedrock permission process."""
diff --git a/letta/llm_api/anthropic_client.py b/letta/llm_api/anthropic_client.py
index c0f7a39b6..a96a3d294 100644
--- a/letta/llm_api/anthropic_client.py
+++ b/letta/llm_api/anthropic_client.py
@@ -5,6 +5,17 @@ from typing import List, Optional, Union
import anthropic
from anthropic.types import Message as AnthropicMessage
+from letta.errors import (
+ ErrorCode,
+ LLMAuthenticationError,
+ LLMBadRequestError,
+ LLMConnectionError,
+ LLMNotFoundError,
+ LLMPermissionDeniedError,
+ LLMRateLimitError,
+ LLMServerError,
+ LLMUnprocessableEntityError,
+)
from letta.helpers.datetime_helpers import get_utc_time
from letta.llm_api.helpers import add_inner_thoughts_to_functions, unpack_all_inner_thoughts_from_kwargs
from letta.llm_api.llm_client_base import LLMClientBase
@@ -104,7 +115,7 @@ class AnthropicClient(LLMClientBase):
data["messages"] = [
m.to_anthropic_dict(
inner_thoughts_xml_tag=inner_thoughts_xml_tag,
- put_inner_thoughts_in_kwargs=self.llm_config.put_inner_thoughts_in_kwargs,
+ put_inner_thoughts_in_kwargs=bool(self.llm_config.put_inner_thoughts_in_kwargs),
)
for m in messages
]
@@ -241,7 +252,7 @@ class AnthropicClient(LLMClientBase):
response = AnthropicMessage(**response_data)
prompt_tokens = response.usage.input_tokens
completion_tokens = response.usage.output_tokens
- finish_reason = remap_finish_reason(response.stop_reason)
+ finish_reason = remap_finish_reason(str(response.stop_reason))
content = None
reasoning_content = None
diff --git a/letta/llm_api/llm_client_base.py b/letta/llm_api/llm_client_base.py
index 66cbf244d..31f68ef87 100644
--- a/letta/llm_api/llm_client_base.py
+++ b/letta/llm_api/llm_client_base.py
@@ -4,6 +4,7 @@ from typing import List, Optional, Union
from openai import AsyncStream, Stream
from openai.types.chat.chat_completion_chunk import ChatCompletionChunk
+from letta.errors import LLMError
from letta.schemas.llm_config import LLMConfig
from letta.schemas.message import Message
from letta.schemas.openai.chat_completion_response import ChatCompletionResponse
diff --git a/letta/schemas/message.py b/letta/schemas/message.py
index 65e5d84be..503a40720 100644
--- a/letta/schemas/message.py
+++ b/letta/schemas/message.py
@@ -681,7 +681,7 @@ class Message(BaseMessage):
user_system_event = add_xml_tag(string=f"SYSTEM ALERT: {text_content}", xml_tag="event")
anthropic_message = {
"content": user_system_event,
- "role": "user",
+ "role": "system",
}
elif self.role == "user":