惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Cisco Talos Blog
Cisco Talos Blog
O
OpenAI News
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 最新话题
T
Tenable Blog
SecWiki News
SecWiki News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
S
Security @ Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Latest news
Latest news
N
News and Events Feed by Topic
N
News and Events Feed by Topic
A
Arctic Wolf
AWS News Blog
AWS News Blog
I
Intezer
Scott Helme
Scott Helme
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
W
WeLiveSecurity
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Threatpost
Project Zero
Project Zero
N
News | PayPal Newsroom
Hacker News: Ask HN
Hacker News: Ask HN
Cyberwarzone
Cyberwarzone
P
Privacy & Cybersecurity Law Blog
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
Security Archives - TechRepublic
Security Archives - TechRepublic
Cloudbric
Cloudbric
T
Tor Project blog
Know Your Adversary
Know Your Adversary
H
Heimdal Security Blog
K
Kaspersky official blog
The Hacker News
The Hacker News
NISL@THU
NISL@THU
P
Palo Alto Networks Blog
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
Spread Privacy
Spread Privacy
Help Net Security
Help Net Security
G
GRAHAM CLULEY
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Exploit Database - CXSecurity.com

博客园 - 漫思

python使用.env构建开发和生产环境 python项目的构建 nodejs构建CICD时的思考  成为 AI 智能体工程师的 10 个步骤 es6的 yield python 中的 yield 笔记本的A壳 thinkpad 更换 reduce() in Python python的字段赋值和取值的操作 python的语法类似于lodash分组展开,合并分组操作 SelectMany C# lodash 数组的常用做法 lodash里面的常用方法 技术的边界 Reduce 和 Transduce 的含义 尤雨溪创办的 VoidZero 官宣加入 Cloudflare,前端 Vite 等保持开源 Ramda 函数库参考教程 ramda es 数组的方法 flatmap map object.entity的教程 Map与FlatMap:在数据处理中的区别与联系 flat、flatmap与map的用法区别1 flat、flatmap与map的用法区别 AI不是从天而降,它经历了七十年三起三落:读懂AI的第三课 Agent 17 种架构模式 分析 & 思考 只有踩过坑才懂:前端生成唯一 ID,别用 Date.now ()了!试试它crypto.randomUUID() FastAPI python并发 代码是 AI 写的,生产事故谁背锅? AI Agent 走出 Demo 幻觉的唯一解药:Harness Engineering 高管的 AI 精神病 小米开源可控视频音效生成模型 ControlFoley ​IBM 联合红帽投资 50 亿美元:帮助企业确保开源软件安全 国家大基金领投 DeepSeek,首轮融资投前估值 450 亿美元 SpaceX 自研 AI 训练栈 V1.0 接近完工:用 C 重写、适配 22 万块 GB300 GPU 高管的 AI 精神病 - 漫思 做一款企业真正敢用的AI测试应用,到底有多难?究竟难在哪? - 漫思 工良吐槽篇:万字长文细说 AI 落地之笑谈 koarouter的路由基本能力 7. 简单语句 class-validator class- @koa/router 分路由 这是我见过的最好的koa2 classvalidate typeorm的能力 HLS.js destroy时会取消请求吗 从入门到精通:2025年本体管理工具选型指南,8款工具助你驾驭语义网络 一文入门智能体:dify 超快速构建AI agent C# 15 类型系统改进:Union Types skill网站 应无所住,而生其心 process.stdin.isTTY 谷歌学术走过风雨十年 听创始人畅谈苦辣酸甜 - 漫思 process.stdin.isTTY 是false的具体情况 asyncio 较为复杂的demo asyncio 简单demo python Annotated 独家解读:淘宝使用 Node.js 的 TypeScript 多场景开发和实践 mac uv 使用 Python 包管理工具 uv 使用教程 Python依赖管理新标杆:UV工具安装与实战指南 python的uv lobehubui python的enum通过int进行初始化 python的枚举类型 深度拆解:AI 智能体 Harness 的构造【译】 LlamaIndex是什么?LlamaIndex综述!看这一篇就够了! python和langchain的简单教程 nodejs的顶尖开源项目 基础Pydantic+TypedDict+Annotated +Dataclass 深度解析Python结构化数据工具:dataclass、Pydantic Model与TypedDict 被 LangChain 全家桶搞晕了?LangGraph、LangSmith、LangFlow 一文读懂 - 漫思 谁才是企业级开源平台的优选?OpenCSG与Dify、Coze、Langflow、Ollama 的差异化之路 四大框架全景解析:LangChain、LangGraph、DeepAgent、LangFlow 测试数据管理方式对比:dict vs dataclass vs TypedDict 革命性的写作:MDX 让你的 Markdown 全面动起来 Python - pydantic 入门介绍与 Models 的简单使用 python模块导入 Python 里通常说“数组切片”,大多数时候指的是列表切片。基本格式是: 自定义hook的写法 Python 遍历字典的8种方法 python基础教程 or的简单用法 C# 15 引入了 联合类型 asyncio的基础理论 flask配置热更新 Form.Item 无法直接绑定两个 name Form.Item 的双向翻译官:getValueFromEvent 与 getValueProps Knex.js入门指南:Node.js最强大的SQL查询构建器 knex.js1 Knex.js RFC 9535:JSONPath 的标准化之路 C#多线程Thread.Join()的详解 Pretext:值得关注的文本排版引擎 Flask入门(四):Flask静态文件及配置 【高并发】消息队列思路 为何高并发系统中都要使用消息队列?这次彻底懂了! Node.js 消息队列应用:RabbitMQ、Kafka 与处理高并发任务 大文件上传下载处理方案-断点续传,秒传,分片,合并 Node.js 缓存策略优化:Redis、Memcached 与本地缓存实现 .NET开发者的救星:8个让你告别996的高效库 别再吹牛了,100% Vibe Coding 存在无法自洽的逻辑漏洞!
欢迎使用 Pydantic¶
漫思 · 2026-04-28 · via 博客园 - 漫思

欢迎使用 Pydantic

CI Coverage
pypi CondaForge downloads
license

.

Pydantic 是 Python 中使用最广泛的数据验证库。

Pydantic 教程

快速且可扩展,Pydantic 与你的代码检查器/集成开发环境/大脑配合良好。以纯的、规范的 Python 3.8+ 定义数据应该如何;使用 Pydantic 对其进行验证。

成功

“迁移到 Pydantic V2”

使用 Pydantic V1 吗? 在应用程序中查看迁移指南以获取有关升级到 Pydantic V2 的注意事项!!

Pydantic Example

from datetime import datetime
from typing import Tuple

from pydantic import BaseModel


class Delivery(BaseModel):
    timestamp: datetime
    dimensions: Tuple[int, int]


m = Delivery(timestamp='2020-01-02T03:04:05Z', dimensions=['10', '20'])
print(repr(m.timestamp))
#> datetime.datetime(2020, 1, 2, 3, 4, 5, tzinfo=TzInfo(UTC))
print(m.dimensions)
#> (10, 20)

问题

“为什么 Pydantic 是这样命名的?”

Pydantic 咨询

“Pydantic”这个名字是“Py”和“pedantic”的混合词。“Py”部分表示该库与 Python 相关,而“pedantic”指的是该库在数据验证和类型强制方面的细致方法。

综合这些元素,“Pydantic”描述了我们的 Python 库,它提供了注重细节、严格的数据验证。

我们意识到具有讽刺意味的是,Pydantic V1 在其验证中并不严格,所以如果我们很“吹毛求疵”的话,在 V2 版本之前,“Pydantic”是一个用词不当的名称😉。

为什么使用 Pydantic?

  • 由类型提示驱动——借助 Pydantic,模式验证和序列化由类型注释控制;学习的更少,编写的代码更少,并且与您的 IDE 和静态分析工具集成。了解更多……

  • 速度——Pydantic 的核心验证逻辑是用 Rust 编写的。因此,Pydantic 是 Python 中最快的数据验证库之一。了解更多……

  • JSON 模式——Pydantic 模型可以生成 JSON 模式,从而便于与其他工具进行集成。了解更多……

  • 严格模式和宽松模式——Pydantic 可以在 strict=True 模式(数据不进行转换)或 strict=False 模式下运行(在适当的情况下,Pydantic 尝试将数据强制转换为正确类型)。了解更多……

    Python 函数库

  • 数据类、类型字典等——Pydantic 支持对许多标准库类型的验证,包括 dataclass 和 TypedDict 。了解更多……

  • 自定义——Pydantic 允许自定义验证器和序列化器以多种强大方式改变数据的处理方式。了解更多……

  • 生态系统——PyPI 上约有 8000 个包使用 Pydantic,包括像 FastAPI、 huggingface、Django Ninja、SQLModel 和 LangChain 这样极受欢迎的库。了解更多……

  • 经过实战检验——Pydantic 每月被下载超过 7000 万次,被所有 FAANG 公司以及纳斯达克 25 家最大公司中的 20 家所使用。如果你正试图用 Pydantic 做某事,那么可能其他人已经做过了。了解更多……

安装 Pydantic 就像这样简单: pip install pydantic

Pydantic 使用例子

Validation Successful

from datetime import datetime

from pydantic import BaseModel, PositiveInt


class User(BaseModel):
    id: int  
    name: str = 'John Doe'  
    signup_ts: datetime | None  
    tastes: dict[str, PositiveInt]  


external_data = {
    'id': 123,
    'signup_ts': '2019-06-01 12:22',  
    'tastes': {
        'wine': 9,
        b'cheese': 7,  
        'cabbage': '1',  
    },
}

user = User(**external_data)  

print(user.id)  
#> 123
print(user.model_dump())  
"""
{
    'id': 123,
    'name': 'John Doe',
    'signup_ts': datetime.datetime(2019, 6, 1, 12, 22),
    'tastes': {'wine': 9, 'cheese': 7, 'cabbage': 1},
}
"""

如果验证失败,Pydantic 会引发一个错误并详细说明哪里出错了:

Pydantic 教程

Validation Error

# continuing the above example...

from pydantic import ValidationError


class User(BaseModel):
    id: int
    name: str = 'John Doe'
    signup_ts: datetime | None
    tastes: dict[str, PositiveInt]


external_data = {'id': 'not an int', 'tastes': {}}  

try:
    User(**external_data)  
except ValidationError as e:
    print(e.errors())
    """
    [
        {
            'type': 'int_parsing',
            'loc': ('id',),
            'msg': 'Input should be a valid integer, unable to parse string as an integer',
            'input': 'not an int',
            'url': 'https://pydantic.com.cn/errors/validation_errors#int_parsing',
        },
        {
            'type': 'missing',
            'loc': ('signup_ts',),
            'msg': 'Field required',
            'input': {'id': 'not an int', 'tastes': {}},
            'url': 'https://pydantic.com.cn/errors/validation_errors#missing',
        },
    ]
    """

谁在使用 Pydantic?

数百个组织和包正在使用 Pydantic。全球一些使用 Pydantic 的著名公司和组织包括:

数据模型

对于使用 Pydantic 的更全面的开源项目列表,请参阅 github 上的依赖项列表,或者您可以在 awesome-pydantic 中找到一些使用 Pydantic 的很棒的项目。