FastAPI and Pydantic - ignore but warn when extra elements are provided to a router input Model

73 views Asked by At

This is the code I wrote

from typing import Any
from fastapi import APIRouter
from pydantic import BaseModel, ConfigDict, ValidationError



class State(BaseModel):
    mode: str | None = None
    alarm: int = 0


class StateLoose(State):
    model_config = ConfigDict(extra='allow')


class StateExact(State):
    model_config = ConfigDict(extra='forbid')


def validated_state(state: dict) -> State:
    try:
        return StateExact(**state)
    except ValidationError as e:
        logger.warning("Sanitized input state that caused a validation error. Error: %s", e)
        return State(**state)


@router.put("/{client_id}", response_model=State)
def update_state(client_id: str, state: StateLoose) -> Any:
    v_state = validated_state(state.dict()).dict()
    return update_resource(client_id=client_id, state=v_state)


# Example State inputs
a = {"mode": "MANUAL", "alarm": 1}
b = {"mode": "MANUAL", "alarm": 1, "dog": "bau"}

normal = State(**a)
loose = StateLoose(**a)
exact = StateExact(**a)

From my understanding/tests with/of pydantic

  • State "adapts" the input dict to fit the model and trows an exception only when something is very wrong (non-convertable type, missing required field). However, extra fields are lost.
  • StateLoose, accepts extra fields and shows them by default (or with pydantic_extra)
  • StateExact trows a ValidationError whenever something extra is provided

What I wanted to achieve is:

  • Show the "State scheme as input" in the FastAPI generated Docs (this means having a State-like input in the "put function".
  • Accept States that have extra elements but ignoring the extra elements (this means using State to remove extra args)
  • Log a warning when extra elements are detected so that I can trace this since probably it means something went not as planned

To achieve this I was forced to create 3 different State classes and play with those. Since I plan to have lots of Models, I don't like the idea of having 3 versions of each and it feels like I am doing something quite wrong if it's so weird to accomplish.

Is there a less redundant way to:

  • accept extra elements in a Model;
  • use the Model as an input to FastAPI router.put;
  • generate a warning;
  • ignore extra elements and continue with the right ones?
1

There are 1 answers

1
MatsLindh On BEST ANSWER

You can use a model_validator with mode="before" together with the field names from model - i.e. in your validator you see which fields got submitted and log those that you didn't expect.

Setting extra='ignore' as the model_config will also make sure that the extra fields gets removed automagically.

from fastapi import FastAPI
from pydantic import BaseModel, ConfigDict, model_validator


class WarnUnknownBase(BaseModel):
    model_config = ConfigDict(extra='ignore')

    @model_validator(mode='before')
    @classmethod
    def validate(cls, values):
        expected_fields = set(cls.model_fields.keys())
        submitted_fields = set(values.keys())
        unknown_fields = submitted_fields - expected_fields
        
        if unknown_fields:
            print(f"Log these fields: {unknown_fields}")

        return values
        
        
class Foo(WarnUnknownBase):
    foo: int
    
    
app = FastAPI()

@app.put('/')
def put_it(foo: Foo):
    return foo

Example requests:

λ curl -H "Content-Type: application/json" -X PUT -d "{\"foo\": 42}" http://localhost:8008
{"foo":42}
λ curl -H "Content-Type: application/json" -X PUT -d "{\"foo\": 42, \"bar\": 13}" http://localhost:8008
{"foo":42}

As you can see, bar gets ignored when the Foo object is created.

The second request will output Log these fields: {'bar'} on the server side, while the first will keep quiet.

INFO:     127.0.0.1:49458 - "PUT / HTTP/1.1" 200 OK
Log these fields: {'bar'}
INFO:     127.0.0.1:49462 - "PUT / HTTP/1.1" 200 OK