R
RunPod5d ago
Tomi

Error Handling Issue: Updating Response Status in Python’s Runpod

Hello everyone! I encountered an issue where I need to raise an error in my handler, but I’ve found that in the Python’s runpod library, errors are added to a job_output list. There’s a condition where it searches for the error field to update the response status to FAILED. However, since the error is within an output list, it doesn’t recognize that field, and the status remains COMPLETED. This is my handler and here is where I raise the error
except Exception as e:
yield {"error": str(e)}
except Exception as e:
yield {"error": str(e)}
and this is the Runpod code
async def _sim_runsync(self, job_request: DefaultRequest) -> JobOutput:
""" Development endpoint to simulate runsync behavior. """
assigned_job_id = f"test-{uuid.uuid4()}"
job = TestJob(id=assigned_job_id, input=job_request.input)

if is_generator(self.config["handler"]):
generator_output = run_job_generator(self.config["handler"], job.__dict__)
job_output = {"output": []}
async for stream_output in generator_output:
job_output['output'].append(stream_output["output"])
else:
job_output = await run_job(self.config["handler"], job.__dict__)

if job_output.get('error', None):
return jsonable_encoder({
"id": job.id,
"status": "FAILED",
"error": job_output['error']
})

if job_request.webhook:
thread = threading.Thread(
target=_send_webhook,
args=(job_request.webhook, job_output), daemon=True)
thread.start()

return jsonable_encoder({
"id": job.id,
"status": "COMPLETED",
"output": job_output['output']
})
async def _sim_runsync(self, job_request: DefaultRequest) -> JobOutput:
""" Development endpoint to simulate runsync behavior. """
assigned_job_id = f"test-{uuid.uuid4()}"
job = TestJob(id=assigned_job_id, input=job_request.input)

if is_generator(self.config["handler"]):
generator_output = run_job_generator(self.config["handler"], job.__dict__)
job_output = {"output": []}
async for stream_output in generator_output:
job_output['output'].append(stream_output["output"])
else:
job_output = await run_job(self.config["handler"], job.__dict__)

if job_output.get('error', None):
return jsonable_encoder({
"id": job.id,
"status": "FAILED",
"error": job_output['error']
})

if job_request.webhook:
thread = threading.Thread(
target=_send_webhook,
args=(job_request.webhook, job_output), daemon=True)
thread.start()

return jsonable_encoder({
"id": job.id,
"status": "COMPLETED",
"output": job_output['output']
})
Any insights would be greatly appreciated!
4 Replies
nerdylive
nerdylive5d ago
is it only for the testing or it happens in endpointz Try to throw the exception instead of yielding it
digigoblin
digigoblin5d ago
Yeah if you throw the exception, the SDK will handle it automatically, probably has something to do with yielding the error, I haven't had experience with that.
Tomi
Tomi5d ago
Thanks for the answers, I was guiding me by the runpod doc https://docs.runpod.io/serverless/workers/handlers/handler-error-handling to implement a custom error response. I going to keep trying.
Handling Errors | RunPod Documentation
Learn how to handle exceptions and implement custom error responses in your RunPod SDK handler function, including how to validate input and return customized error messages.
nerdylive
nerdylive5d ago
Its between yield / return keyword, they both work differently in the handler but if you're using generator try to just throw the exception