AFAIK it's not documented, but schema_extra parameter of SQLModel's Field is for passing parameters that are not present in the Field, but supported by Pydantic's Field.
For example, there is no validation_alias parameter, but you can pass it the following way:
id_: in = Field(schema_extra={"validation_alias": "item_id"})
python - example in schema extra is ignored by pydantic in fastapi - Stack Overflow
Advice for setting schema_extra on primitive types
Pydantic v2: how to post-process the generated schema now?
Defining additional properties with a given schema
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.
If abyone else stumbles on thisone and can't get it to work, in Python 3.10+ and Pydantic 2, you seem to have to use model_config, so the about would look like
model_config = {
"json_schema_extra": {
"examples": [
{
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
}
]
}
}
Instead of using class config
After digging a bit deeper into the pydantic code I found a nice little way to prevent this. There is a method called field_title_should_be_set(...) in GenerateJsonSchema which can be subclassed and provided to model_json_schema(...).
I'm not sure if the way I've overwritten the method is sufficient for each edge case but at least for this little test class it works as intended.
from pydantic import BaseModel
from pydantic._internal._core_utils import is_core_schema, CoreSchemaOrField
from pydantic.json_schema import GenerateJsonSchema
class Test(BaseModel):
a: int
class GenerateJsonSchemaWithoutDefaultTitles(GenerateJsonSchema):
def field_title_should_be_set(self, schema: CoreSchemaOrField) -> bool:
return_value = super().field_title_should_be_set(schema)
if return_value and is_core_schema(schema):
return False
return return_value
json_schema = Test.model_json_schema(schema_generator=GenerateJsonSchemaWithoutDefaultTitles)
assert "title" not in json_schema["properties"]["a"]
You can do it in following way with Pydantic v2:
from pydantic import BaseModel, ConfigDict
def my_schema_extra(schema: dict[str, Any]) -> None:
for prop in schema.get('properties', {}).values():
prop.pop('title', None)
class Model(BaseModel):
a: int
model_config = ConfigDict(
json_schema_extra=my_schema_extra,
)
print(Model.schema_json())