Spaces:
Running
Running
File size: 7,078 Bytes
82a1419 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
"""
Veo 3.1 Official Error Handler
Based on official Veo 3.1 API documentation
"""
from enum import Enum
from typing import Dict, Tuple, Optional
import re
class VeoErrorType(Enum):
"""Official Veo 3.1 error types"""
RESOURCE_EXHAUSTED = "429"
FORBIDDEN = "403"
SAFETY_FILTER = "safety"
PUBLIC_ERROR_MINOR = "public_error_minor"
CELEBRITY = "celebrity" # Prominent public figure
CHILD = "child"
SEXUAL = "sexual"
VIOLENCE = "violence"
DANGEROUS = "dangerous"
UNKNOWN = "unknown"
# Official Veo 3.1 Safety Filter Support Codes
# Source: Official API Documentation
SAFETY_CODES: Dict[str, str] = {
# Child Safety
"58061214": "Child",
"17301594": "Child",
# Celebrity / Public Figures
"29310472": "Celebrity",
"15236754": "Celebrity",
# Sexual Content
"90789179": "Sexual",
"43188360": "Sexual",
# Violence
"61493863": "Violence",
"56562880": "Violence",
# Dangerous Content
"62263041": "Dangerous",
}
def extract_support_code(error_message: str) -> Optional[str]:
"""
Extract 8-digit support code from error message
Args:
error_message: The error message from Veo API
Returns:
Support code if found, None otherwise
"""
# Look for 8-digit numbers (support codes)
match = re.search(r'\b\d{8}\b', error_message)
return match.group(0) if match else None
def get_safety_category(support_code: str) -> str:
"""
Get safety category from support code
Args:
support_code: 8-digit support code
Returns:
Safety category name
"""
return SAFETY_CODES.get(support_code, f"Unknown ({support_code})")
def is_safety_violation(error_message: str) -> bool:
"""
Check if error is a safety filter violation
Args:
error_message: Error message from API
Returns:
True if safety violation detected
"""
# Official keywords from documentation
safety_keywords = [
"safety filter",
"safety violation",
"content policy",
"inappropriate content",
"flagged for containing",
"prominent public figure",
"celebrity",
"public figure",
"politician",
"moderation",
]
error_lower = error_message.lower()
# Check for keywords
if any(keyword in error_lower for keyword in safety_keywords):
return True
# Check for support code
if extract_support_code(error_message):
return True
return False
def is_public_error_minor(error_message: str) -> bool:
"""
Check if error is PUBLIC_ERROR_MINOR (generic internal error)
Args:
error_message: Error message from API
Returns:
True if PUBLIC_ERROR_MINOR detected
"""
return "PUBLIC_ERROR_MINOR" in error_message.upper()
def handle_veo_error(error_response: Dict) -> Tuple[VeoErrorType, str, Optional[str]]:
"""
Handle Veo 3.1 errors according to official documentation
Args:
error_response: Error response from Veo API
Returns:
Tuple of (error_type, error_message, support_code)
"""
error_code = str(error_response.get("code", ""))
error_message = error_response.get("message", "")
# Extract support code if present
support_code = extract_support_code(error_message)
# 429: Too many requests
if "429" in error_code:
return (
VeoErrorType.RESOURCE_EXHAUSTED,
"Too many requests. Wait 5-10 minutes and retry.",
None
)
# 403: Insufficient permissions
if "403" in error_code:
return (
VeoErrorType.FORBIDDEN,
"Insufficient permissions. Check API configuration.",
None
)
# PUBLIC_ERROR_MINOR: Generic internal error
if is_public_error_minor(error_message):
return (
VeoErrorType.PUBLIC_ERROR_MINOR,
"Internal processing error. Wait 5-10 minutes or simplify request.",
None
)
# Safety filter with support code
if support_code:
category = get_safety_category(support_code)
return (
VeoErrorType.SAFETY_FILTER,
f"Safety filter triggered: {category}",
support_code
)
# Celebrity / Public Figure (no support code)
if any(keyword in error_message.lower() for keyword in [
"prominent public figure", "celebrity", "politician", "public figure"
]):
return (
VeoErrorType.CELEBRITY,
"Content contains prominent public figure or celebrity",
None
)
# Generic safety violation
if is_safety_violation(error_message):
return (
VeoErrorType.SAFETY_FILTER,
"Content policy violation detected",
None
)
# Unknown error
return (
VeoErrorType.UNKNOWN,
error_message or "Unknown error occurred",
None
)
def get_retry_strategy(error_type: VeoErrorType) -> Dict[str, any]:
"""
Get recommended retry strategy for error type
Args:
error_type: The type of error
Returns:
Dict with retry strategy
"""
strategies = {
VeoErrorType.RESOURCE_EXHAUSTED: {
"should_retry": True,
"wait_seconds": 600, # 10 minutes
"max_retries": 2,
"requires_fix": False,
},
VeoErrorType.PUBLIC_ERROR_MINOR: {
"should_retry": True,
"wait_seconds": 300, # 5 minutes
"max_retries": 2,
"requires_fix": False,
},
VeoErrorType.CELEBRITY: {
"should_retry": True,
"wait_seconds": 2,
"max_retries": 2,
"requires_fix": True, # Needs AI fix
},
VeoErrorType.SAFETY_FILTER: {
"should_retry": True,
"wait_seconds": 2,
"max_retries": 2,
"requires_fix": True, # Needs AI fix
},
VeoErrorType.FORBIDDEN: {
"should_retry": False,
"wait_seconds": 0,
"max_retries": 0,
"requires_fix": False,
},
VeoErrorType.UNKNOWN: {
"should_retry": True,
"wait_seconds": 5,
"max_retries": 1,
"requires_fix": False,
},
}
return strategies.get(error_type, strategies[VeoErrorType.UNKNOWN])
def should_auto_fix(error_type: VeoErrorType) -> bool:
"""
Determine if error should trigger AI auto-fix
Args:
error_type: The type of error
Returns:
True if auto-fix should be attempted
"""
fixable_types = [
VeoErrorType.CELEBRITY,
VeoErrorType.SAFETY_FILTER,
VeoErrorType.CHILD,
VeoErrorType.SEXUAL,
VeoErrorType.VIOLENCE,
VeoErrorType.DANGEROUS,
]
return error_type in fixable_types
|