DC

datamodel-code-generator/datamodel-code-generator

开发工具

本页由 AI 翻译,技术细节请以原文为准。 查看原文

4047 stars 质量 77 趋势 48MIT

生成 Pydantic v2 模型、数据类、TypedDict 和 msgspec。从 OpenAPI 构建结构体,JSON Schema、GraphQL、Avro、Protobuf 以及原始的 JSON/YAML/CSV。

概览

🚀 从模式定义中几秒钟内生成 Python 数据模型。 📚 文档 ·🧪游乐场 ·💼首席维护员可工作 Playground 隐私:生成程序在你的浏览器中本地运行,使用 Pyodide。你的模式和选项不会发送到后端。共享复制 URL 编码在 URL 片段(#state=...),浏览器不会将这些片段发送到服务器;完整 URL 仍可能存储在你的浏览器历史或你分享的任何地方。 选择支持的任意输入,并选择你想要的 Python 模型样式作为输出。--input-model path/to/file.py:ClassName 甚至可以将另一个 Python 文件中定义的 Pydantic、dataclass 或 TypedDict 类重新定位到不同的输出类型。

概述(AI 翻译)

🚀 从模式定义中几秒钟内生成 Python 数据模型。

📚 文献资料· 🧪 游乐场· 💼 首席维护员可工作

[!注] Playground 隐私:生成程序在你的浏览器中本地运行,使用 Pyodide。你的模式和选项不会发送到 后端。共享复制URL会将其编码在URL片段(#state=...),浏览器不会将这些片段发送到服务器; 完整的网址仍然可以存储在你的浏览器历史或你分享的任何地方。

✨ 它的作用

选择任何支持的输入,并选择你想要的 Python 模型样式作为输出。 --input-model path/to/file.py:ClassName 甚至可以重新定位已定义的 Pydantic、dataclass 或 TypedDict 类 在另一个Python文件中转换为不同的输出类型。

  • 📄 将 OpenAPI 3、AsyncAPI、**JSON 模式、Apache Avro、XML 模式、协议缓冲区/gRPC、GraphQL、MCP 工具模式以及原始数据(JSON/YAML/CSV)转换为 Python 模型
  • 🐍 通过[[T2]从现有的Python类型(Pydantic、dataclass、TypedDict)生成
  • 🎯 生成Pydantic v2、Pydantic v2数据类、dataclasses、TypedDict或msgspec输出

本页由 AI 翻译,技术细节请以原文为准。

README

以下为原文 README 节选,完整内容请查看原文。

datamodel-code-generator

🚀 Generate Python data models from schema definitions in seconds.

📚 Documentation · 🧪 Playground · 💼 Lead maintainer available for work

[!NOTE] Playground privacy: Generation runs locally in your browser with Pyodide. Your schema and options are not sent to a backend. Shared repro URLs encode them in the URL fragment (#state=...), which browsers do not send to the server; the full URL can still be stored in your browser history or wherever you share it.

✨ What it does

Pick any one of the supported inputs and pick the Python model style you want as output. --input-model path/to/file.py:ClassName can even retarget an existing Pydantic, dataclass, or TypedDict class defined in another Python file to a different output type.

  • 📄 Converts OpenAPI 3, AsyncAPI, JSON Schema, Apache Avro, XML Schema, Protocol Buffers/gRPC, GraphQL, MCP tool schemas, and raw data (JSON/YAML/CSV) into Python models
  • 🐍 Generates from existing Python types (Pydantic, dataclass, TypedDict) via --input-model
  • 🎯 Generates Pydantic v2, Pydantic v2 dataclass, dataclasses, TypedDict, or msgspec output
  • 🔗 Handles complex schemas: $ref, allOf, oneOf, anyOf, enums, and nested types
  • ✅ Produces type-safe, validated code ready for your IDE and type checker
在 GitHub 查看完整 README

推荐工具

换一个关键词,或者移除筛选条件。

安装

npx skillfish add datamodel-code-generator/datamodel-code-generator
新手?先看 mcpskill.org 教程 →