1

I am trying to pass a multi-line text to fastApi Code. My code looks like

from fastapi import FastAPI
from pydantic import BaseModel

from model import predictT5

testApp = FastAPI() # create an app

@testApp.get("/") # set root directory

# pydantic models
class TextIn(BaseModel):
    text: str
class TextOut(BaseModel):
    Summary: dict 

    # routes
def root():
    return {"message":"jmj"}

@testApp.post("/predict", response_model=TextOut, status_code=200)
def get_prediction(payload: TextIn):
    """
    Gets a text and runs through T5 & outputs summary
    """
    text = payload.text
    t5Summary = predictT5(text)

This works with a request like

curl -X 'POST' \
  'http://127.0.0.1:8000/predict' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "text": "A dictionary that maps attention modules to devices. For reference, the t5 models have the following number of attention modules:"
}'

However if I split the text with a \n and have a request body like

curl -X 'POST' \
  'http://127.0.0.1:8000/predict' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "text": "A dictionary that maps attention modules to devices.  
For reference, the t5 models have the following number of attention modules:"
}'

I see

Error: Unprocessable Entity "msg": "Invalid control character at: line 2 column 285 (char 286)", "type": "value_error.jsondecode",

Would anyone know how to handle this ? thank you!

LGG
  • 528
  • 9
  • 21

1 Answers1

0

According to this answer, multi-line strings are not allowed in JSON.

From the same source: "You need to replace all the line breaks with \n"

Artur Shiriev
  • 131
  • 1
  • 8