Skip to content

Commit 0317819

Browse files
committed
Fix status code: split pipeline load from input parsing
pipeline loading -> 500 input parsing -> 400 Signed-off-by: Raphael Glon <[email protected]>
1 parent 45907cd commit 0317819

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

api_inference_community/routes.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,20 @@ async def pipeline_route(request: Request) -> Response:
9595
] and already_left(request):
9696
logger.info("Discarding request as the caller already left")
9797
return Response(status_code=204)
98-
9998
payload = await request.body()
99+
100100
task = os.environ["TASK"]
101+
102+
try:
103+
inputs, params = normalize_payload(payload, task, sampling_rate=sampling_rate)
104+
except EnvironmentError as e:
105+
logger.error("Error while parsing input %s", e)
106+
return JSONResponse({"error": str(e)}, status_code=500)
107+
except Exception as e:
108+
# We assume the payload is bad -> 400
109+
logger.warning("Error while parsing input %s", e)
110+
return JSONResponse({"error": str(e)}, status_code=400)
111+
101112
if os.getenv("DEBUG", "0") in {"1", "true"}:
102113
pipe = request.app.get_pipeline()
103114
try:
@@ -106,7 +117,6 @@ async def pipeline_route(request: Request) -> Response:
106117
sampling_rate = pipe.sampling_rate
107118
except Exception:
108119
sampling_rate = None
109-
inputs, params = normalize_payload(payload, task, sampling_rate=sampling_rate)
110120
except ValidationError as e:
111121
errors = []
112122
for error in e.errors():

0 commit comments

Comments
 (0)