FastAPI expects the body of a POST request to follow a structure.
If you send just a plain string like:
"hello world"
FastAPI cannot parse it unless you tell it exactly how to treat that string.
So it returns something like:
{"detail":"There was an error parsing the body"}
or:
{"detail":[{"type":"string_type","msg":"str type expected"}]}
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class TextIn(BaseModel):
text: str
@app.post("/predict")
def predict(data: TextIn):
input_text = data.text
# your ML model prediction here
result = my_model.predict([input_text])
return {"prediction": result}
{
"text": "This is my input sentence"
}