ihtesham0345 commited on
Commit
c4fea6f
·
1 Parent(s): ff08af5

fix: Handle missing correction fields + ResponseValidationError

Browse files
Files changed (2) hide show
  1. main.py +13 -0
  2. services/utils.py +9 -0
main.py CHANGED
@@ -1,5 +1,7 @@
1
  from fastapi import FastAPI, HTTPException
2
  from fastapi.responses import JSONResponse
 
 
3
  from models.schemas import (
4
  SEORequest, SEOResponse,
5
  YouTubeResponse, InstagramResponse, LinkedInResponse,
@@ -25,6 +27,17 @@ app = FastAPI(
25
  )
26
 
27
  ERROR_MASK = "An internal error occurred. Please try again later."
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  def safe_analyze(analyze_fn, content: str):
30
  try:
 
1
  from fastapi import FastAPI, HTTPException
2
  from fastapi.responses import JSONResponse
3
+ from fastapi.exceptions import ResponseValidationError
4
+ from starlette.requests import Request
5
  from models.schemas import (
6
  SEORequest, SEOResponse,
7
  YouTubeResponse, InstagramResponse, LinkedInResponse,
 
27
  )
28
 
29
  ERROR_MASK = "An internal error occurred. Please try again later."
30
+ ERROR_MASK_VALIDATION = "The model returned an incomplete response. Please try again."
31
+
32
+
33
+ @app.exception_handler(ResponseValidationError)
34
+ async def validation_exception_handler(request: Request, exc: ResponseValidationError):
35
+ print(f"[ValidationError] {exc}")
36
+ return JSONResponse(
37
+ status_code=200,
38
+ content={"error": ERROR_MASK_VALIDATION},
39
+ )
40
+
41
 
42
  def safe_analyze(analyze_fn, content: str):
43
  try:
services/utils.py CHANGED
@@ -207,10 +207,19 @@ def normalize_response(data: dict) -> dict:
207
  if isinstance(data["corrections"], list) and data["corrections"]:
208
  if isinstance(data["corrections"][0], str):
209
  data["corrections"] = [{"original": s, "corrected": s, "error_type": "grammar", "explanation": "Automatically corrected."} for s in data["corrections"]]
 
 
 
 
210
  if "issues" in data:
211
  if isinstance(data["issues"], list) and data["issues"]:
212
  if isinstance(data["issues"][0], str):
213
  data["issues"] = [{"issue_type": s, "location": "text", "suggestion": "Review this section."} for s in data["issues"]]
 
 
 
 
 
214
  if "grammar_score" in data:
215
  if not isinstance(data["grammar_score"], (int, float)):
216
  try:
 
207
  if isinstance(data["corrections"], list) and data["corrections"]:
208
  if isinstance(data["corrections"][0], str):
209
  data["corrections"] = [{"original": s, "corrected": s, "error_type": "grammar", "explanation": "Automatically corrected."} for s in data["corrections"]]
210
+ elif isinstance(data["corrections"][0], dict):
211
+ for c in data["corrections"]:
212
+ c.setdefault("error_type", "grammar")
213
+ c.setdefault("explanation", "Review and correct this.")
214
  if "issues" in data:
215
  if isinstance(data["issues"], list) and data["issues"]:
216
  if isinstance(data["issues"][0], str):
217
  data["issues"] = [{"issue_type": s, "location": "text", "suggestion": "Review this section."} for s in data["issues"]]
218
+ elif isinstance(data["issues"][0], dict):
219
+ for iss in data["issues"]:
220
+ iss.setdefault("issue_type", "style")
221
+ iss.setdefault("location", "text")
222
+ iss.setdefault("suggestion", "Review this section.")
223
  if "grammar_score" in data:
224
  if not isinstance(data["grammar_score"], (int, float)):
225
  try: