I have a deeply nested schema for a pydantic model .I wanted to include an example for fastapi user .When I am trying to do so pydantic is ignoring the example .Below is my model code :
# generated by datamodel-codegen:
# filename: mllogitem.json
# timestamp: 2022-02-08T13:47:04+00:00
from __future__ import annotations
from typing import List, Optional
from pydantic import BaseModel
class Input(BaseModel):
columns: Optional[List[str]] = None
index: Optional[List[int]] = None
data: Optional[List[List]] = None
class Output(BaseModel):
question: Optional[str] = None
answer: Optional[str] = None
simScore: Optional[str] = None
class MlFlowData(BaseModel):
input: Optional[Input] = None
output: Optional[Output] = None
class MlFlow(BaseModel):
appId: str
sessionId: str
timestamp: str
type: str
payload: MlFlowData
class Config:
arbitrary_types_allowed = True
schema_extra = {
"example": {
"appId": "ConversationalAI",
"sessionId": "d290f1ee-6c54-4b01-90e6-d701748f0851",
"timestamp": "2018-09-27T12:40:00Z",
"type": "Input/output data",
"payload": {
"input": {
"columns": [
"question",
"scenario"
],
"index": [
0
],
"data": [
[
"what is your name?",
"gerontology"
]
]
},
"output": {
"answer": "Henry Williams: My name is Henry Williams.",
"question": "What is your name? ",
"simScore": "1.0"
}
}
}
}
I see the schema in api show as
{
"appId": "string",
"sessionId": "string",
"timestamp": "string",
"type": "string",
"payload": {
"input": {
"columns": [
"string"
],
"index": [
0
],
"data": [
[
"string"
]
]
},
"output": {
"question": "string",
"answer": "string",
"simScore": "string"
}
}
}
It ignore my example schema.Any clue on this will be useful.
The class Config
must be nested in your class model, in your example the class is sitting outside:
class MlFlow(BaseModel):
appId: str
sessionId: str
timestamp: str
type: str
payload: MlFlowData
class Config:
arbitrary_types_allowed = True
schema_extra = {}
Tip: Setting your properties to Optional
AND = None
is redundant. User either one, you don't need both.