OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

example in schema extra is ignored by pydantic in fastapi

  • Thread starter Thread starter Rajesh Kumar Dash
  • Start date Start date
R

Rajesh Kumar Dash

Guest
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 :

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

Code:
{
  "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.
<p>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 :</p>
<pre><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
  • ] = 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"
    }
    }
    }
    }
    </code></pre>
    <p>I see the schema in api show as</p>
    <pre><code>{
    "appId": "string",
    "sessionId": "string",
    "timestamp": "string",
    "type": "string",
    "payload": {
    "input": {
    "columns": [
    "string"
    ],
    "index": [
    0
    ],
    "data": [
    [
    "string"
    ]
    ]
    },
    "output": {
    "question": "string",
    "answer": "string",
    "simScore": "string"
    }
    }
    }
    </code></pre>
    <p>It ignore my example schema.Any clue on this will be useful.</p>
 
Top