Spaces:
Runtime error
Runtime error
Commit ·
fb41f3c
1
Parent(s): 9512f60
Fixed json pasring
Browse files118 unmodified lines119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157131 unmodified lines289290291292293294295296118 unmodified linesmessage: strrequest_id: str = Field(default_factory=lambda: str(uuid.uuid4()))def clean_json_response(response_text: str) -> str:"""Clean and extract JSON from API response"""logger.info("Cleaning JSON response")if not response_text:logger.error("Empty response text provided")raise ValueError("Empty response text")cleaned = response_text.strip()patterns = [r'```json\s*',r'```',]for pattern in patterns:cleaned = re.sub(pattern, '', cleaned, flags=re.MULTILINE | re.DOTALL)cleaned = cleaned.strip()# Use regex to extract the first valid JSON objectmatch = re.search(r'\{[\s\S]*\}', cleaned)if not match:logger.error(f"No JSON object found. Response preview: {cleaned[:300]}...")raise ValueError("No JSON object found in response")json_str = match.group(0)logger.info(f"Extracted JSON length: {len(json_str)}")logger.info(f"JSON preview: {json_str[:150]}...")logger.info(f"JSON ending: ...{json_str[-50:]}")try:json.loads(json_str)logger.info("JSON syntax validation passed")except json.JSONDecodeError as e:logger.error(f"JSON syntax validation failed: {str(e)}")logger.error(f"Problematic JSON: {json_str}")raise ValueError(f"Invalid JSON syntax: {str(e)}")return json_strdef validate_strategies_data(data: dict) -> dict:"""Validate and clean strategies data"""logger.info("Validating strategies data")131 unmodified lineslogger.info(f"Request {request_id}: Raw response first 200 chars: {api_response[:200]}")logger.info(f"Request {request_id}: Raw response last 200 chars: {api_response[-200:]}")json_str = clean_json_response(api_response)strategies_data = json.loads(json_str)logger.info(f"Request {request_id}: JSON parsing successful")except (json.JSONDecodeError, ValueError) as e:118 unmodified lines119120121122123124131 unmodified lines256257258259260261262118 unmodified linesmessage: strrequest_id: str = Field(default_factory=lambda: str(uuid.uuid4()))def validate_strategies_data(data: dict) -> dict:"""Validate and clean strategies data"""logger.info("Validating strategies data")131 unmodified lineslogger.info(f"Request {request_id}: Raw response first 200 chars: {api_response[:200]}")logger.info(f"Request {request_id}: Raw response last 200 chars: {api_response[-200:]}")strategies_data = json.loads(api_response)logger.info(f"Request {request_id}: JSON parsing successful")except (json.JSONDecodeError, ValueError) as e:
business_continuity.py+1-35