Complete worked example
python
from decimal import Decimal
from pydantic import BaseModel, Field, ValidationError, computed_field
class Quote(BaseModel):
amount: Decimal = Field(gt=0)
@computed_field
@property
def total(self) -> Decimal:
return self.amount * 2
def inspect_example(payload):
try:
quote = Quote.model_validate(payload)
inputs = Quote.model_json_schema(mode="validation")
outputs = Quote.model_json_schema(mode="serialization")
return {"value": quote.model_dump(mode="json"),
"input_has_total": "total" in inputs["properties"],
"total_read_only": outputs["properties"]["total"]["readOnly"],
"amount_output_type": outputs["properties"]["amount"]["type"]}
except ValidationError as error:
return {"error": sorted({e["type"] for e in error.errors()})}- Reasoning
- The inspection function validates the input, dumps a JSON-compatible value and obtains both schema modes. It reports whether total appears in the validation schema, whether output total is read-only and the output type of amount. Those observations connect documentation to actual object behavior. For amount="2.50", the result preserves the decimal scale in the shown JSON strings and includes the calculated total. A zero amount fails its Field bound before a successful quote is returned. Run both the value fixture and the schema observations; checking only an attractive OpenAPI page would miss a runtime validation regression.