Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(llm): read endpoint error messages without a doubled article
trohitg41b85f9c9266
diff --git a/server/services/llm/protocol.py b/server/services/llm/protocol.pyindex 692dcc5c..ccb1bc8c 100644--- a/server/services/llm/protocol.py+++ b/server/services/llm/protocol.py@@ -372,16 +372,22 @@ class LLMError(Exception):"lmstudio": "LM Studio",}# Common nouns take an article, so they read "The <noun>" as a- # subject and "the <noun>" as an object.+ # subject and "the <noun>" as an object. A name also works as an+ # adjective ("the configured OpenAI model"); a noun with its article+ # does not, so the two model-scoped messages are phrased around it.generic_nouns = {"openai_compatible": "OpenAI-compatible endpoint"}# A named endpoint's reference is "openai_compatible:<slug>".provider_key = str(self.provider or "").strip().lower().partition(":")[0]if provider_key in provider_names:provider = provider_object = provider_names[provider_key]+ not_found = f"The configured {provider} model or endpoint was not found."+ context_length = f"The request exceeds the {provider} model context window."else:noun = generic_nouns.get(provider_key, "language model provider")provider = f"The {noun}"provider_object = f"the {noun}"+ not_found = f"{provider} did not find the configured model."+ context_length = f"The request exceeds the context window of the model at {provider_object}."category = (self.category.valueif isinstance(self.category, LLMErrorCategory)@@ -403,14 +409,8 @@ class LLMError(Exception):LLMErrorCategory.INVALID_REQUEST.value: (f"{provider} rejected the model request configuration."),- LLMErrorCategory.NOT_FOUND.value: (- f"The configured {provider_object} model or endpoint "- "was not found."- ),- LLMErrorCategory.CONTEXT_LENGTH.value: (- f"The request exceeds the {provider_object} model "- "context window."- ),+ LLMErrorCategory.NOT_FOUND.value: not_found,+ LLMErrorCategory.CONTEXT_LENGTH.value: context_length,LLMErrorCategory.TIMEOUT.value: (f"The request to {provider_object} timed out."),diff --git a/server/tests/llm/test_protocol_codec.py b/server/tests/llm/test_protocol_codec.pyindex 2c0ff698..55bd902d 100644--- a/server/tests/llm/test_protocol_codec.py+++ b/server/tests/llm/test_protocol_codec.py@@ -179,6 +179,43 @@ def test_structured_error_exposes_only_category_based_user_message():assert "Bearer secret" not in error.user_message+ ("provider", "not_found", "context_length"),+ [+ (+ "openai",+ "The configured OpenAI model or endpoint was not found.",+ "The request exceeds the OpenAI model context window.",+ ),+ (+ "openai_compatible:home",+ "The OpenAI-compatible endpoint did not find the configured model.",+ "The request exceeds the context window of the model at the OpenAI-compatible endpoint.",+ ),+ (+ "some-unregistered-provider",+ "The language model provider did not find the configured model.",+ "The request exceeds the context window of the model at the language model provider.",+ ),+ ],+)+def test_model_scoped_messages_read_for_names_and_common_nouns(provider, not_found, context_length):+ def message(category: LLMErrorCategory) -> str:+ return LLMError(message="raw", provider=provider, category=category).user_message++ assert message(LLMErrorCategory.NOT_FOUND) == not_found+ assert message(LLMErrorCategory.CONTEXT_LENGTH) == context_length++[email protected]("provider", ["openai_compatible:home", "some-unregistered-provider"])[email protected]("category", list(LLMErrorCategory))+def test_no_message_doubles_the_article_of_a_common_noun(provider, category):+ text = LLMError(message="raw", provider=provider, category=category).user_message.lower()++ assert "the the " not in text+ assert "configured the " not in text++def test_structured_error_preserves_http_date_retry_after():class Response:status_code = 429