Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

How to exclude Optional unset values from a Pydantic model using FastAPI?

$
0
0

I have this model:

class Text(BaseModel):    id: str    text: str = Noneclass TextsRequest(BaseModel):    data: list[Text]    n_processes: Union[int, None]

So I want to be able to take requests like:

{"data": ["id": "1", "text": "The text 1"], "n_processes": 8} 

and

{"data": ["id": "1", "text": "The text 1"]}.

Right now in the second case I get

{'data': [{'id': '1', 'text': 'The text 1'}], 'n_processes': None}

using this code:

app = FastAPI()@app.post("/make_post/", response_model_exclude_none=True)async def create_graph(request: TextsRequest):    input_data = jsonable_encoder(request)

So how can I exclude n_processes here?


Viewing all articles
Browse latest Browse all 23160

Trending Articles