```class ErrorResponse(BaseModel): status: str...
# ask-for-help
n
Copy code
class ErrorResponse(BaseModel):
    status: str = "error"
    message: str
    required_min_tokens: int
    provided_tokens: int

    class Config:
        json_schema_extra = {
            "example": {
                "status": "error",
                "message": "The provided input is too short.",
                "required_min_tokens": 64,
                "provided_tokens": 5
            }
        }


#Subject Generation
class SubjectResponse(BaseModel):
    status: str = "success"
    subjects: List[str]

    class Config:
        json_schema_extra = {
            "example": {
                "status": "success",
                "subjects": [
                    "First Generated Subject",
                    "Second Generated Subject",
                    "Third Generated Subject",
                ]
            }
        }


@bentoml.api(
        route="/v1/generate_subject",
    )
    async def generate_subject(self, input_data: str = text_sample) :
        response = self.predict_subject(input_data, self.MODEL_MAX_LENGTH)
        if isinstance(response, ErrorResponse):
            raise bentoml.exceptions.BadInput(response.message)
        return response
How can I add both ErrorResponse and SubjectResponse ? So I can See the example values in fastapi doc.
j
@Frost Ming do we support customizing Responses with pydantic? Maybe in
output_spec
?
f
Just try
Union[ErrorResponse, SubjectResponse]
j
in
output_spec
?
f
output_spec does the same as return type