admin管理员组

文章数量:1122846

I am trying to create the following JSON Schema

from pydantic import BaseModel, Field

class VendorInfo(BaseModel):
    vendor_name: str = Field("", description= "Vendor Name")
    vendor_vat_no: str = Field("", description= "Vendor VAT Number")

class InvoiceHeader(BaseModel):
    invoice_number: str = Field("", description= "The unique invoice number")
    invoice_date: str = Field("", description= "The date invoice was created")
    vendor_info: VendorInfo = Field("", description= "Description of the vendor")

print(json.dumps(InvoiceHeader.model_json_schema(), indent=2))

This gives following output with $defs and $ref. How to remove them from schema?

{"$defs": {"VendorInfo": {"properties": {"vendor_name": {"default": "","description": "Vendor Name","title": "Vendor Name","type": "string"},"vendor_vat_no": {"default": "","description": "Vendor VAT Number","title": "Vendor Vat No","type": "string"}},"title": "VendorInfo","type": "object"}},"properties": {"invoice_number": {"default": "","description": "The unique invoice number","title": "Invoice Number","type": "string"},"invoice_date": {"default": "","description": "The date invoice was created","title": "Invoice Date","type": "string"},"vendor_info": {"$ref": "#/$defs/VendorInfo","default": "","description": "Description of the vendor"}},"title": "InvoiceHeader","type": "object"}

本文标签: jsonschemaHow to remove defs and ref when creating a nested JSON Schema using PydanticStack Overflow