Replace hardwired Anthropic API calls with a pluggable provider system. Each deployment configures its AI provider in instance.yaml — switching between Anthropic, LiteLLM, OpenRouter, or any OpenAI-compatible proxy is a config change, not a code change. New connectors/llm/ module: - StructuredExtractor Protocol with extract_json() interface - AnthropicExtractor: direct Anthropic SDK with retry + backoff - OpenAICompatExtractor: any OpenAI-compatible proxy with three-layer structured output fallback (json_schema -> json_object -> prompt) - Configurable structured_output policy (strict/json/auto) - Custom exception hierarchy (auth/rate_limit/timeout/format/refusal) - Zero secrets in logs: no API keys, prompts, or responses logged Reviewed by: Google Gemini, Claude Sonnet, OpenAI GPT-5.4. Security audit passed with all critical findings resolved.
55 lines
1.3 KiB
Python
55 lines
1.3 KiB
Python
"""Exception hierarchy for LLM connector errors.
|
|
|
|
All exceptions inherit from LLMError so callers can catch the base
|
|
class for broad error handling or specific subclasses for targeted
|
|
recovery strategies.
|
|
"""
|
|
|
|
|
|
class LLMError(Exception):
|
|
"""Base exception for all LLM-related errors."""
|
|
|
|
|
|
class LLMAuthError(LLMError):
|
|
"""Invalid API key or authentication failure.
|
|
|
|
This is a permanent error - do not retry.
|
|
"""
|
|
|
|
|
|
class LLMRateLimitError(LLMError):
|
|
"""Rate limited by the provider.
|
|
|
|
This is a transient error - retry with exponential backoff.
|
|
"""
|
|
|
|
|
|
class LLMTimeoutError(LLMError):
|
|
"""Timeout or connection error.
|
|
|
|
This is a transient error - retry with exponential backoff.
|
|
"""
|
|
|
|
|
|
class LLMFormatError(LLMError):
|
|
"""Invalid JSON or unexpected response structure.
|
|
|
|
The model returned content that could not be parsed as valid JSON
|
|
or did not match the expected schema.
|
|
"""
|
|
|
|
|
|
class LLMUnsupportedError(LLMError):
|
|
"""Provider does not support a required feature.
|
|
|
|
For example, the provider does not support structured output
|
|
(json_schema response format) and the configuration does not
|
|
allow fallback strategies.
|
|
"""
|
|
|
|
|
|
class LLMRefusalError(LLMError):
|
|
"""Model refused to generate a response.
|
|
|
|
Typically triggered by safety filters or content policy violations.
|
|
"""
|