Skip to content

Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.

Refactor/improve api key error messages (#279)

Louis Choquelcfca926d3fc9
diff --git a/pipelex/cogt/exceptions.py b/pipelex/cogt/exceptions.py
index c56100840..e70b17602 100644
--- a/pipelex/cogt/exceptions.py
+++ b/pipelex/cogt/exceptions.py
@@ -1,6 +1,7 @@
from typing import Optional
from pipelex.tools.exceptions import FatalError, RootException
+from pipelex.types import StrEnum
class CogtError(RootException):
@@ -148,8 +149,24 @@ class InferenceBackendError(CogtError):
pass
+class InferenceBackendCredentialsErrorType(StrEnum):
+ VAR_NOT_FOUND = "var_not_found"
+ UNKNOWN_VAR_PREFIX = "unknown_var_prefix"
+ VAR_FALLBACK_PATTERN = "var_fallback_pattern"
+
+
class InferenceBackendCredentialsError(CogtError):
- pass
+ def __init__(
+ self,
+ error_type: InferenceBackendCredentialsErrorType,
+ backend_name: str,
+ message: str,
+ key_name: str,
+ ):
+ self.error_type = error_type
+ self.backend_name = backend_name
+ self.key_name = key_name
+ super().__init__(message)
class InferenceBackendLibraryError(CogtError):
diff --git a/pipelex/cogt/model_backends/backend_library.py b/pipelex/cogt/model_backends/backend_library.py
index a4829a9e2..33cebeac3 100644
--- a/pipelex/cogt/model_backends/backend_library.py
+++ b/pipelex/cogt/model_backends/backend_library.py
@@ -4,7 +4,12 @@ from pydantic import Field, RootModel, ValidationError
from typing_extensions import Self
from pipelex import log
-from pipelex.cogt.exceptions import InferenceBackendCredentialsError, InferenceBackendLibraryError, InferenceModelSpecError
+from pipelex.cogt.exceptions import (
+ InferenceBackendCredentialsError,
+ InferenceBackendCredentialsErrorType,
+ InferenceBackendLibraryError,
+ InferenceModelSpecError,
+)
from pipelex.cogt.model_backends.backend import InferenceBackend
from pipelex.cogt.model_backends.backend_factory import InferenceBackendBlueprint, InferenceBackendFactory
from pipelex.cogt.model_backends.model_spec import InferenceModelSpec
@@ -13,7 +18,7 @@ from pipelex.cogt.model_backends.prompting_target import PromptingTarget
from pipelex.config import get_config
from pipelex.tools.misc.dict_utils import apply_to_strings_recursive
from pipelex.tools.misc.toml_utils import load_toml_from_path
-from pipelex.tools.secrets.secrets_utils import UnknownVarPrefixError, VarNotFoundError, substitute_vars
+from pipelex.tools.secrets.secrets_utils import UnknownVarPrefixError, VarFallbackPatternError, VarNotFoundError, substitute_vars
InferenceBackendLibraryRoot = Dict[str, InferenceBackend]
@@ -43,15 +48,35 @@ class InferenceBackendLibrary(RootModel[InferenceBackendLibraryRoot]):
continue
try:
inference_backend_blueprint_dict = apply_to_strings_recursive(inference_backend_blueprint_dict_raw, substitute_vars)
- except (VarNotFoundError, UnknownVarPrefixError) as exc:
- raise InferenceBackendCredentialsError(f"Variable substitution failed in file '{backends_library_path}': {exc}") from exc
+ except VarFallbackPatternError as var_fallback_pattern_exc:
+ raise InferenceBackendCredentialsError(
+ error_type=InferenceBackendCredentialsErrorType.VAR_FALLBACK_PATTERN,
+ backend_name=backend_name,
+ message=f"Variable substitution failed due to a pattern error in file '{backends_library_path}':\n{var_fallback_pattern_exc}",
+ key_name="unknown",
+ ) from var_fallback_pattern_exc
+ except VarNotFoundError as var_not_found_exc:
+ raise InferenceBackendCredentialsError(
+ error_type=InferenceBackendCredentialsErrorType.VAR_NOT_FOUND,
+ backend_name=backend_name,
+ message=f"Variable substitution failed due to a variable not found error in file '{backends_library_path}':\n{var_not_found_exc}",
+ key_name=var_not_found_exc.var_name,
+ ) from var_not_found_exc
+ except UnknownVarPrefixError as unknown_var_prefix_exc:
+ raise InferenceBackendCredentialsError(
+ error_type=InferenceBackendCredentialsErrorType.UNKNOWN_VAR_PREFIX,
+ backend_name=backend_name,
+ message=(
+ f"Variable substitution failed due to an unknown variable prefix error "
+ f"in file '{backends_library_path}':\n{unknown_var_prefix_exc}"
+ ),
+ key_name=unknown_var_prefix_exc.var_name,
+ ) from unknown_var_prefix_exc
for key in backend_dict.keys():
if key not in standard_fields:
extra_config[key] = inference_backend_blueprint_dict.pop(key)
backend_blueprint = InferenceBackendBlueprint.model_validate(inference_backend_blueprint_dict)
- # if not backend_blueprint.enabled:
- # continue
path_to_model_specs_toml = get_config().cogt.inference_config.model_specs_path(backend_name=backend_name)
try:
diff --git a/pipelex/pipelex.py b/pipelex/pipelex.py
index f17bc55e4..2c511b3f5 100644
--- a/pipelex/pipelex.py
+++ b/pipelex/pipelex.py
@@ -178,14 +178,27 @@ class Pipelex(metaclass=MetaSingleton):
"The routing library could not be found, please call `pipelex init config` to create it"
) from routing_profile_library_exc
except InferenceBackendCredentialsError as credentials_exc:
+ backend_name = credentials_exc.backend_name
+ var_name = credentials_exc.key_name
+ error_msg: str
if secrets_provider:
- raise PipelexSetupError(
- f"Missing credentials for inference backend: {credentials_exc}, check that it's available from your secrets provider."
- ) from credentials_exc
+ error_msg = (
+ f"Could not get credentials for inference backend {backend_name}:\n{credentials_exc},"
+ f"\ncheck that secret '{var_name}' is available from your secrets provider."
+ )
else:
- raise PipelexSetupError(
- f"Missing credentials for inference backend: {credentials_exc}, add it to your environment variables or to your .env file."
- ) from credentials_exc
+ error_msg = (
+ f"Could not get credentials for inference backend {backend_name}:\n{credentials_exc},\n"
+ f"you need to add '{var_name}' to your environment variables or to your .env file."
+ )
+ if credentials_exc.backend_name == "pipelex_inference":
+ error_msg += (
+ "\nYou can check the project's README about getting a Pipelex Inference API key,\n\n"
+ "or you can bring your own 'OPENAI_API_KEY', "
+ "'AZURE_OPENAI_API_KEY', 'ANTHROPIC_API_KEY', 'MISTRAL_API_KEY' etc.\n"
+ "--> choose which inference backends to enable in .pipelex/inference/backends.toml\n"
+ )
+ raise PipelexSetupError(error_msg) from credentials_exc
self.pipelex_hub.set_content_generator(content_generator or ContentGenerator())
self.reporting_delegate.setup()
self.class_registry.register_classes(PipelexRegistryModels.get_all_models())
diff --git a/pipelex/tools/secrets/secrets_utils.py b/pipelex/tools/secrets/secrets_utils.py
index 5807c77de..50d2c400c 100644
--- a/pipelex/tools/secrets/secrets_utils.py
+++ b/pipelex/tools/secrets/secrets_utils.py
@@ -8,13 +8,21 @@ from pipelex.types import StrEnum
class VarNotFoundError(ToolException):
+ def __init__(self, var_name: str, message: str):
+ self.var_name = var_name
+ super().__init__(message)
+
+
+class VarFallbackPatternError(ToolException):
pass
class UnknownVarPrefixError(ToolException):
"""Raised when an unknown variable prefix is used in variable substitution."""
- pass
+ def __init__(self, var_name: str, message: str):
+ self.var_name = var_name
+ super().__init__(message)
class VarPrefix(StrEnum):
@@ -58,7 +66,10 @@ def substitute_vars(content: str) -> str:
try:
prefix = VarPrefix(prefix_str)
except ValueError:
- raise UnknownVarPrefixError(f"Unknown variable prefix: '{prefix_str}'")
+ raise UnknownVarPrefixError(
+ var_name=var_name,
+ message=f"Unknown variable prefix: '{prefix_str}'",
+ )
match prefix:
case VarPrefix.ENV:
@@ -87,7 +98,10 @@ def _handle_fallback_pattern(var_spec: str) -> str:
try:
prefix = VarPrefix(prefix_str)
except ValueError:
- raise UnknownVarPrefixError(f"Unknown variable prefix: '{prefix_str}'")
+ raise UnknownVarPrefixError(
+ var_name=var_name,
+ message=f"Unknown variable prefix: '{prefix_str}'",
+ )
match prefix:
case VarPrefix.ENV:
@@ -106,7 +120,9 @@ def _handle_fallback_pattern(var_spec: str) -> str:
except SecretNotFoundError:
continue # Try next option
- raise VarNotFoundError(f"Variable not found in any source: {var_spec}")
+ raise VarFallbackPatternError(
+ message=f"Could not get variable from fallback pattern: {var_spec}",
+ )
def _get_env_var(var_name: str) -> str:
@@ -114,7 +130,7 @@ def _get_env_var(var_name: str) -> str:
try:
return get_required_env(var_name)
except EnvVarNotFoundError as exc:
- raise VarNotFoundError(f"Could not get variable '{var_name}': {str(exc)}") from exc
+ raise VarNotFoundError(message=f"Could not get variable '{var_name}': {str(exc)}", var_name=var_name) from exc
def _get_secret(secret_name: str) -> str:
@@ -122,4 +138,4 @@ def _get_secret(secret_name: str) -> str:
try:
return get_secrets_provider().get_secret(secret_id=secret_name)
except SecretNotFoundError as exc:
- raise VarNotFoundError(f"Could not get variable '{secret_name}': {str(exc)}") from exc
+ raise VarNotFoundError(message=f"Could not get variable '{secret_name}': {str(exc)}", var_name=secret_name) from exc
diff --git a/tests/unit/pipelex/tools/secrets/test_secrets_utils.py b/tests/unit/pipelex/tools/secrets/test_secrets_utils.py
index 0dd3b05ec..d344f1337 100644
--- a/tests/unit/pipelex/tools/secrets/test_secrets_utils.py
+++ b/tests/unit/pipelex/tools/secrets/test_secrets_utils.py
@@ -5,7 +5,7 @@ import pytest
from pytest_mock import MockerFixture
from pipelex.tools.secrets.secrets_provider_abstract import SecretsProviderAbstract
-from pipelex.tools.secrets.secrets_utils import UnknownVarPrefixError, VarNotFoundError, substitute_vars
+from pipelex.tools.secrets.secrets_utils import UnknownVarPrefixError, VarFallbackPatternError, VarNotFoundError, substitute_vars
@pytest.fixture
@@ -139,7 +139,7 @@ class TestSubstituteVars:
mock_secrets_provider.get_secret.side_effect = SecretNotFoundError("Secret not found")
mocker.patch("pipelex.tools.secrets.secrets_utils.get_secrets_provider", return_value=mock_secrets_provider)
- with pytest.raises(VarNotFoundError, match="Variable not found in any source"):
+ with pytest.raises(VarFallbackPatternError, match="Could not get variable from fallback pattern: env:MISSING_ENV\\|secret:MISSING_SECRET"):
substitute_vars("Value: ${env:MISSING_ENV|secret:MISSING_SECRET}")
def test_reverse_fallback_both_missing_raises_error(self, mocker: MockerFixture, mock_secrets_provider: Any) -> None:
@@ -149,7 +149,7 @@ class TestSubstituteVars:
mock_secrets_provider.get_secret.side_effect = SecretNotFoundError("Secret not found")
mocker.patch("pipelex.tools.secrets.secrets_utils.get_secrets_provider", return_value=mock_secrets_provider)
- with pytest.raises(VarNotFoundError, match="Variable not found in any source"):
+ with pytest.raises(VarFallbackPatternError, match="Could not get variable from fallback pattern: secret:MISSING_SECRET\\|env:MISSING_ENV"):
substitute_vars("Value: ${secret:MISSING_SECRET|env:MISSING_ENV}")
def test_complex_variable_names(self, mocker: MockerFixture, mock_secrets_provider: Any) -> None: