1

Input is just a single string in this case id and it will return other attributes linked to that id.

Output will be a nested dictionary which belongs to that id:

{"t1":"Hello there! I am getting stuck at Response.", "details":[{"Start":0,"End":5,"Name":"ABCD"}, {"Start":6,"End":10,"Name":"XYZ"}]}

I am getting a Pydantic validation error, and not sure how to resolve.

Response() output-

from fastapi import FastAPI, Query, HTTPException
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
from predict import predict

app = FastAPI()

class Request(BaseModel):
    id: str

class Result(BaseModel):
    text: str
    entities: dict

class Response(BaseModel):
    results: Result

@app.post("/predict", status_code=200)
async def predict_api(request: Request):
    result = predict(request.id)
    json_compatible_data = jsonable_encoder(result)
    output_result = Response(json_compatible_data)
    return output_result

I am stuck at resolving the nested dictionary as an output.

Full Error message

 File "/Users/shringa/miniconda/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 429, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/Users/shringa/miniconda/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
  File "/Users/shringa/miniconda/lib/python3.10/site-packages/fastapi/applications.py", line 276, in __call__
    await super().__call__(scope, receive, send)
  File "/Users/shringa/miniconda/lib/python3.10/site-packages/starlette/applications.py", line 122, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/Users/shringa/miniconda/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/Users/shringa/miniconda/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/Users/shringa/miniconda/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/Users/shringa/miniconda/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/Users/shringa/miniconda/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/Users/shringa/miniconda/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/Users/shringa/miniconda/lib/python3.10/site-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/Users/shringa/miniconda/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/Users/shringa/miniconda/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/Users/shringa/miniconda/lib/python3.10/site-packages/fastapi/routing.py", line 237, in app
    raw_response = await run_endpoint_function(
  File "/Users/shringa/miniconda/lib/python3.10/site-packages/fastapi/routing.py", line 163, in run_endpoint_function
    return await dependant.call(**values)
  File "/Users/shringa/FourthBrain/ML project/nlp_project/src/main.py", line 33, in predict_api
    output_result = Response(json_compatible_data)
  File "pydantic/main.py", line 332, in pydantic.main.BaseModel.__init__
Shringa Bais
  • 191
  • 2
  • 11
  • Please [edit] to post the exact error message. – Gino Mempin Apr 29 '23 at 09:19
  • 2
    Please also [edit] to show how exactly you are calling this endpoint. You say "*Input is just a single string.*" but then your `/predict` endpoint is expecting a `Request` object as a parameter and you are trying to access a `request.id` which doesn't exist in your model. – Gino Mempin Apr 29 '23 at 09:24
  • 1
    You might find [this answer](https://stackoverflow.com/a/73974946/17865804) helpful – Chris Apr 29 '23 at 19:38
  • Thank you Chris and Gino. Both suggestions helped me with error. I don't need Response class in my case and also output variable is just one single dict. – Shringa Bais May 01 '23 at 09:56

0 Answers0