JSON Schema → Pydantic
अपने browser में JSON Schema को Pydantic v2 model code में convert करें। Schema paste करें, ready-to-paste Python class पाएं। LLM structured-output workflows के लिए उपयोगी।
परिणाम देखने के लिए ऊपर इनपुट डालें।
यह किसके लिए है?
JSON Schema और Pydantic structured object describe करने के दो मुख्य तरीके हैं — JSON Schema OpenAPI specs, LLM function-calling, और structured outputs के लिए lingua-franca है; Pydantic Python की de-facto data-validation library है। काफ़ी अक्सर आपके पास एक होता है और आपको दूसरा चाहिए। यह tool एक direction में conversion करता है: एक JSON Schema paste करें, एक Pydantic class पाएं जिसे आप Python file में drop कर सकते हैं।
कब इस्तेमाल करें
- LLM structured outputs। OpenAI का
response_formatऔर Anthropic का tool-use दोनों JSON Schema लेते हैं। Schema design और test करने के बाद, आप आमतौर पर Python में fields को validate और access करने के लिए Pydantic model चाहते हैं। - OpenAPI client generation। एक OpenAPI spec आपको हर request/response body के लिए JSON Schema देती है; full code-generator pull किए बिना matching Pydantic models पाने का यह एक तेज़ तरीका है।
- Schema → Python migrations। आपको एक system inherit मिला है जो validation के लिए JSON Schema उपयोग करता है और आप इसे Pydantic में move करना चाहते हैं। हर schema paste करें, starting class पाएं, refine करें।
- Quick scaffolding। आपने notebook में JSON में shape sketch की; एक paste से इसे real model में बदलें।
यह tool क्या handle करता है
type: objectwithproperties+required— एक nested BaseModel class generate करता है।type: arraywithitems—List[X]generate करता है।- Primitives:
string→str,integer→int,number→float,boolean→bool,null→None। - String पर
format:date-time→datetime,email→EmailStr,uri/url→HttpUrl,uuid→UUID। enum→Literal[...];const→Literal[X]।oneOf/anyOf→Union[...]; एक null variantOptional[...]में folds हो जाता है।type: ["string", "null"]arrays →Optional[str]।description→Field(..., description=...)।default→ field default।minimum/maximum/minLength/maxLength→Field(ge=..., le=..., min_length=..., max_length=...)।- ऐसे field names जो valid Python identifiers नहीं हैं (hyphens, leading digits, keywords) sanitised होते हैं, और original wire name preserve करने के लिए एक
alias=added जाता है। Modelpopulate_by_name=Trueके साथ configured है। - Nested objects nested classes बनते हैं, यदि कोई
titleprovided नहीं तो field के नाम पर। $defs/definitionsseparate classes के रूप में emitted होते हैं।
यह tool क्या handle नहीं करता (अभी)
- External
$ref(URL refs)।$defsके लिए local refs काम करते हैं। patternProperties। एक TODO comment emit करता है।- Schema के साथ
additionalProperties। एक TODO comment emit करता है। - Multi-base
allOfcomposition। Single-elementallOfunwrapped होता है। patternपर आधारित custom validators — field emit करता है पर regex skip करता है। यदि आवश्यक हो तो paste करने के बाद एक Pydantic@field_validatoradd करें।
आम गलतियाँ
- Pydantic v2 vs v1। Default output v2 (current) है। यदि v1 codebase पर हैं तो dropdown से switch करें।
- Output एक starting point है, final word नहीं। इसे review करें। विशेषकर complex unions, recursive types, और custom validation rules के साथ कुछ भी के लिए।
- Names matter करते हैं। "Root class name" field top-level class name को control करता है। Sub-classes का नाम उनकी
titleproperty के बाद होता है यदि present, अन्यथा उस field के बाद जो उन्हें contain करता है। - v2 में Required vs Optional। बिना default वाला एक required field
...उपयोग करता है। Optional fields defaultNoneहोते हैं औरOptional[T]में wrapped होते हैं।