Skip to content
0:00 / 0:00

#pydantic#jsonschema#datamodel-code-generator

Pydantic - Nested Models, JSON Schema and Auto-Generating Models with datamodel-code-generator

BugBytes

58K subscribers • 1 video on Hallway

YouTube
  • Recommended by 1 person: 1 on Stack Overflow and its sister sites.
  • Linked in the descriptionbugbytes-io/datasets

Intermediate. Assumes working knowledge.

22K views3 years ago1 recommendation

Programming languagesSoftware designPythonDesign patterns

A tutorial on defining nested Pydantic models, constraining fields, generating JSON schemas, and auto-generating models from schemas.

In this video, we dive further into the Pydantic library in Python. This is based on the following blog post: bugbytes.io/posts/pydantic-nested-models-and-json-schemas/ We'll learn a number of things, including: * How to define nested Pydantic models, forming parent-child relationships that allow us to model complex data that contains relationships. * Using typing.Literal to constrain values on a field * Defining default values for fields * Generating JSON Schema from Pydantic models * Auto-generating Pydantic models from JSON Schema definitions Github data: github.com/bugbytes-io/datasets 📌 𝗖𝗵𝗮𝗽𝘁𝗲𝗿𝘀: Intro Adding new Pydantic model for nested data Adding a link to nested model in parent Pydantic class The effect of the Union type on a Pydantic field Constraining field values with Python Literal type Adding custom validator function to check list length JSON Schema outputs with Pydantic models Auto-generating Pydantic models from JSON Schema with datamodel-code-generator ☕️ 𝗕𝘂𝘆 𝗺𝗲 𝗮 𝗰𝗼𝗳𝗳𝗲𝗲: To support the channel and encourage new videos, please consider buying me a coffee here: ko-fi.com/bugbytes ▶️ Full Playlist: youtube.com/playlist?list=PL-2EBeDYMIbT1M9S9PEFlqJ9SgFYYbIKp 𝗦𝗼𝗰𝗶𝗮𝗹 𝗠𝗲𝗱𝗶𝗮: 📖 Blog: bugbytes.io/posts/pydantic-nested-models-and-json-schemas/ 👾 Github: github.com/bugbytes-io/datasets 📚 𝗙𝘂𝗿𝘁𝗵𝗲𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 𝗮𝗻𝗱 𝗶𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻: Pydantic Blog post: bugbytes.io/posts/pydantic-nested-models-and-json-schemas/ Github Dataset: github.com/bugbytes-io/datasets Pydantic Models: docs.pydantic.dev/usage/models/ Pydantic Validators: docs.pydantic.dev/usage/validators/ Pydantic Schema: docs.pydantic.dev/usage/schema/ JSON Schema: json-schema.org/ #python #pydantic #datascience

Why it's here

1 person recommends this

What people say. Recommended as a guide for handling nested JSON schemas in LangChain output parsers.

chemFouron Stack Overflow • Recommends

On “define an output schema for a nested json in langchain”

From what I can see the recommended approach is to use the pydantic output parser as opposed to the structured output parser... python.langchain.com/docs/modules/model_io/output_parsers/… (and dealing with nesting explained here... link to this video). e.g. from langchain.output_parsers import PydanticOutputParser from pydantic import BaseModel, Field, validator from typing import List, Optional ... class PlanItem(BaseModel): step: str tools: Optionalstr = [] data_sources: Optionalstr = [] sub_steps_needed: str class Plan(BaseModel): plan: ListPlanItem parser = PydanticOutputParser(pydantic_object=Plan) parser.get_format_instructions()