This message was deleted.
# ask-for-help
s
This message was deleted.
🦾 1
s
You can raise a BentoMLException, that has both a status code and a message. Alternatively, you can also set status code with
context
.
s
@sauyon To change the status code of a response in case of an exception, you can use the
JSONResponse
function from the
fastapi.responses
module. Here's an example of how to modify your code to return a custom status code and detail message:
Copy code
python
from fastapi import HTTPException
from fastapi.responses import JSONResponse

try:
    results = await pub_classifier_runner.async_run(input_text)

    label_id = results[0]['label'].split('_')[1]
    score = round(results[0]['score'], 2)

    return [{
        'label': ids_mapping.get(label_id),
        'score': score,
        'model_version': pub_classifier_model.info.metadata.get('version')
    }]

except Exception as e:
    bentoml_logger.error(f'Error running pub_classifier_runner: {e}')
    raise HTTPException(status_code=503, detail=str(e))
This content can't be displayed.
👀 1
s
Ah, that's where
JSONResponse
comes from. But yeah, fastapi APIs won't work in BentoML
p
Thanks everyone for the help!