Repository navigation
fix(agent): propagate fatal LLMError on invalid llm across start/run/chat (#5956) #5966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,11 +9,62 @@ | |
| - External integrations | ||
| """ | ||
|
|
||
| from typing import Literal, Protocol, runtime_checkable, Optional, Dict, Any, get_args | ||
| from typing import Literal, Protocol, runtime_checkable, Optional, Dict, Any, Sequence, get_args | ||
| from dataclasses import dataclass, field | ||
| import os | ||
| import re | ||
| import uuid | ||
| import warnings | ||
|
|
||
| _API_KEY_TOKEN_PATTERN = re.compile(r"\bsk-[A-Za-z0-9_\-]{12,}\b") | ||
| _SECRET_ENV_VARS = ( | ||
| "OPENAI_API_KEY", | ||
| "ANTHROPIC_API_KEY", | ||
| "GOOGLE_API_KEY", | ||
| "GEMINI_API_KEY", | ||
| "GROQ_API_KEY", | ||
| "MISTRAL_API_KEY", | ||
| "DEEPSEEK_API_KEY", | ||
| "OPENROUTER_API_KEY", | ||
| "XAI_API_KEY", | ||
| "COHERE_API_KEY", | ||
| "AZURE_OPENAI_API_KEY", | ||
| ) | ||
|
|
||
|
|
||
| def sanitize_llm_error_message( | ||
| message: str, | ||
| *, | ||
| extra_secrets: Optional[Sequence[Optional[str]]] = None, | ||
| preserve_tokens: Optional[Sequence[Optional[str]]] = None, | ||
| ) -> str: | ||
| """Redact API keys and credential literals from an LLM error message.""" | ||
| if not message: | ||
| return message | ||
| text = str(message) | ||
| try: | ||
| from .trace.redact import redact_string | ||
| text = redact_string(text, enabled=True) | ||
| except Exception: | ||
| pass | ||
| text = _API_KEY_TOKEN_PATTERN.sub("[REDACTED]", text) | ||
|
|
||
| preserved = {t for t in (preserve_tokens or ()) if t} | ||
| candidates = [] | ||
| if extra_secrets: | ||
| candidates.extend(extra_secrets) | ||
| for env_name in _SECRET_ENV_VARS: | ||
| candidates.append(os.environ.get(env_name)) | ||
| for secret in candidates: | ||
| if ( | ||
| isinstance(secret, str) | ||
| and len(secret) >= 6 | ||
| and secret not in preserved | ||
| and secret in text | ||
| ): | ||
| text = text.replace(secret, "[REDACTED]") | ||
| return text | ||
|
|
||
|
|
||
| # Closed error taxonomy for typed failure classification | ||
| AgentErrorKind = Literal[ | ||
|
|
@@ -167,19 +218,34 @@ def __init__( | |
| run_id: Optional[str] = None, | ||
| error_category: AgentErrorKind = "unknown", | ||
| is_retryable: bool = False, # Default to non-retryable unless specified | ||
| context: Optional[Dict[str, Any]] = None | ||
| context: Optional[Dict[str, Any]] = None, | ||
| extra_secrets: Optional[Sequence[Optional[str]]] = None, | ||
| ): | ||
| model_str = str(model_name) if model_name else "unknown" | ||
| sanitized_message = sanitize_llm_error_message( | ||
| str(message), | ||
| extra_secrets=extra_secrets, | ||
| preserve_tokens=(model_str,), | ||
| ) | ||
| context = context or {} | ||
| context["model_name"] = model_name | ||
| context["model_name"] = model_str | ||
| super().__init__( | ||
| message, | ||
| agent_id=agent_id, | ||
| run_id=run_id, | ||
| sanitized_message, | ||
| agent_id=agent_id or "unknown", | ||
| run_id=run_id, | ||
| error_category=error_category, | ||
| is_retryable=is_retryable, | ||
| context=context | ||
| context=context, | ||
| ) | ||
| self.model_name = model_name | ||
| self.model_name = model_str | ||
|
|
||
| def __str__(self) -> str: | ||
| if self.model_name and self.model_name != "unknown": | ||
| return ( | ||
| f"[{self.error_category}] {self.message} " | ||
| f"(model: {self.model_name}, agent: {self.agent_id}, run: {self.run_id})" | ||
|
Comment on lines
+242
to
+246
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sanitize the displayed model field, and avoid using whole configuration objects as model names. How this was verified: An invalid configuration dictionary reaches |
||
| ) | ||
| return super().__str__() | ||
|
|
||
|
|
||
| class BudgetExceededError(PraisonAIError): | ||
|
|
@@ -471,5 +537,6 @@ def __init__(self, message: str, validation_errors: Optional[list] = None, **kwa | |
| "HandoffDepthError", | ||
| "HandoffTimeoutError", | ||
| "HandoffValidationError", | ||
| "PraisonAIConfigError" | ||
| "PraisonAIConfigError", | ||
| "sanitize_llm_error_message", | ||
| ] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: MervinPraison/PraisonAI
Length of output: 719
🏁 Script executed:
Repository: MervinPraison/PraisonAI
Length of output: 45565
🏁 Script executed:
Repository: MervinPraison/PraisonAI
Length of output: 45669
🏁 Script executed:
Repository: MervinPraison/PraisonAI
Length of output: 19430
🏁 Script executed:
Repository: MervinPraison/PraisonAI
Length of output: 9195
🏁 Script executed:
Repository: MervinPraison/PraisonAI
Length of output: 5989
🏁 Script executed:
Repository: MervinPraison/PraisonAI
Length of output: 4304
Normalize an empty
PRAISONAI_MODELvalue in the wrapper.When
PRAISONAI_MODELis set to an empty string, the wrapper passesllm=""toAgent. The changed branch retains it, and the firstchat()raises a non-retryableLLMErrorinstead of usinggpt-4o-mini. Normalize the wrapper value while preserving explicit empty-value validation inAgent.Suggested fix
🤖 Prompt for AI Agents
Source: Coding guidelines