Issue with integrating fastapi with streamlit

I am creating an app which should take a pdf, text (to be pasted in text area) and the type of llm as an input and after taking all the inputs the app should call the fastapi backend. Below is the code for integrating streamlit with fastapi:
def generate(text,llm_type):
url = “http://localhost:8000/generate/
data = {“text”:text,“llm_type”:llm_type}
response = requests.post(url, data = data)

answer = generate(text,llm_type)

Backend:

@app.post(“/generate/”)
async def gen(text:str = Form(…), llm_type: str = None):
## further code

The issue I have is that in the backend I am able to get the text but not the llm_type. ( In the frontend I have a side bar in which radio buttons are there of different llm types and user needs to select the type of llm he needs to use). The backend is suppose to take the llm_type string, then with normal if else the llm code is selected, but llm_type is always coming as None. I have tested in the frontend, the selected llm_type string is being passed.

Hi @titiksha_Verma, and welcome to our community! :hugs:

FastAPI might be expecting a JSON payload, but you’re sending form data. So try modifying your requests.post call in Streamlit to use JSON instead:

response = requests.post(url, json=data)

Best,
Charly

with this I am getting 422 response “Unprocessable entity”

I see!

I think you’d need to ensure that the JSON structure and the data types you’re sending match exactly what your FastAPI endpoint expects according to its definition (e.g., the Pydantic model if you’ve updated to use one).

Also, you’ll need to check that all required fields are included and correctly formatted in your JSON payload.

Let me know hown it goes! :slight_smile:

Best,
Charly

Hi Charly,
In the fastapi function I changed the query parameters to the following and it worked for me. :
@app.post(“/generate/”)
async def gen(text:str = Form(…), llm_type : Optional[str] = Form(None):

Thanks for helping out.

Nice! I’m glad it helped!

Let me know if you need anything else, and happy Streamlit-ing! :balloon:

Charly

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.