Skip to main content

Export: Pydantic Model

Generates Pydantic model classes from the data contract.

datacontract export pydantic-model orders.odcs.yaml --output orders.py

Running this against the example orders contract produces:

import datetime, typing, pydantic, decimal
'Tracks customer orders and their line items for analytics and reporting.'

class Orders(pydantic.BaseModel):
"""One row per customer order."""
order_id: str
'Unique identifier of the order.'
order_timestamp: datetime.datetime
'Timestamp when the order was placed.'
customer_id: str
'Reference to the customer who placed the order.'
order_total: int
'Total amount of the order in cents.'
status: str
'Current fulfilment status of the order.'

class Line_items(pydantic.BaseModel):
"""One row per line item within an order."""
line_item_id: str
'Unique identifier of the line item.'
order_id: str
'Reference to the parent order.'
sku: str
'Stock keeping unit of the purchased product.'
quantity: int
'Number of units purchased.'

Defaults and constraints​

Fields that are not required are typed typing.Optional[...] and default to None.

logicalTypeOptions become pydantic.Field constraints, so the generated model rejects what datacontract test would flag:

logicalTypelogicalTypeOptionspydantic.Field keyword
stringpattern, minLength, maxLengthpattern, min_length, max_length
integer, number, date, timestamp, timeminimum, maximum, exclusiveMinimum, exclusiveMaximumge, le, gt, lt
label: typing.Optional[str] = pydantic.Field(default=None, pattern='^[^,]+(,\\s*[^,]+)*$')
order_total: int = pydantic.Field(ge=0)

All options: datacontract export pydantic-model.