ChatHistoryMessage 类

表示聊天历史记录中的单个消息。

此模型用于捕获用户与 AI 助手之间交换的单个消息,以便进行威胁防护分析和合规性监视。

构造函数

pydantic model ChatHistoryMessage

仅限关键字的参数

名称 说明
id
默认值: None
role
必需
content
必需
timestamp
默认值: None

示例


>>> message = ChatHistoryMessage(role="user", content="Hello, how can you help?")
>>> print(message.role)
'user'
>>> print(message.content)
'Hello, how can you help?'

方法

__init__

通过分析和验证关键字参数中的输入数据来创建新模型。

引发 [ValidationError][pydantic_core。如果无法验证输入数据以形成有效的模型,则 ValidationError]。

self 是显式位置仅允许 自己 作为字段名称。

__new__
construct
content_not_empty

验证内容是否不为空或仅空格。

copy

返回模型的副本。

!!! 警告“已弃用”此方法现已弃用;请改用 model_copy

如果需要 包括排除,请使用:

python {test="skip" lint="skip"} data = self.model_dump(include=include, exclude=exclude, round_trip=True) data = {**data, **(update or {})} copied = self.model_validate(data)

dict
from_orm
json
model_construct

使用已验证的数据创建 Model 类的新实例。

从受信任的或预先验证的数据创建一个新的模型设置 听写pydantic_fields_set 。 遵循默认值,但不会执行其他验证。

!!! 请注意 ,model_construct() 通常遵循提供的模型的 model_config.extra 设置。 也就是说,如果 model_config.extra == “allow”,则将所有额外的传递值添加到模型实例的 听写pydantic_extra 字段。 如果 model_config.extra == 'ignore' (默认值),则忽略所有额外的传递值。 由于对 model_construct() 的调用不执行验证,因此如果传递了额外的值,则 model_config.extra == 'forbid' 不会产生错误,但将被忽略。

model_copy

!!! 抽象的“使用情况文档” model_copy

返回模型的副本。

!!! 请注意基础实例的 [dict][object。复制 dict] 属性。 如果在模型字段中存储任何内容(例如[缓存属性][functools.cached_property]的值),则可能会产生意外的副作用。

model_dump

!!! 抽象的“使用情况文档” model_dump

生成模型的字典表示形式,可以选择指定要包含或排除的字段。

model_dump_json

!!! 抽象的“使用情况文档” model_dump_json

使用 Pydantic 的 to_json 方法生成模型的 JSON 表示形式。

model_json_schema

为模型类生成 JSON 架构。

model_parametrized_name

计算泛型类参数化的类名。

可以重写此方法以实现泛型 BaseModel 的自定义命名方案。

model_post_init

重写此方法以在 初始化model_construct后执行其他初始化。 如果要进行一些需要初始化整个模型的验证,这非常有用。

model_rebuild

尝试为模型重新生成 pydantic-core 架构。

如果其中一个批注是 ForwardRef,在最初尝试生成架构时无法解析,并且自动重新生成失败,则可能是必需的。

model_validate

验证 pydantic 模型实例。

model_validate_json

!!! 抽象的“使用情况文档” JSON 分析

根据 Pydantic 模型验证给定的 JSON 数据。

model_validate_strings

使用针对 Pydantic 模型的字符串数据验证给定对象。

parse_file
parse_obj
parse_raw
schema
schema_json
update_forward_refs
validate

__init__

通过分析和验证关键字参数中的输入数据来创建新模型。

引发 [ValidationError][pydantic_core。如果无法验证输入数据以形成有效的模型,则 ValidationError]。

self 是显式位置仅允许 自己 作为字段名称。

__init__(**data: Any) -> None

参数

名称 说明
data
必需
Any

返回

类型 说明

__new__

__new__(**kwargs)

construct

construct(_fields_set: set[str] | None = None, **values: Any) -> Self

参数

名称 说明
_fields_set
set[str] | None
默认值: None
values
必需
Any

返回

类型 说明

content_not_empty

验证内容是否不为空或仅空格。

validator content_not_empty  »  content

参数

名称 说明
v
必需
str

返回

类型 说明
str

copy

返回模型的副本。

!!! 警告“已弃用”此方法现已弃用;请改用 model_copy

如果需要 包括排除,请使用:

python {test="skip" lint="skip"} data = self.model_dump(include=include, exclude=exclude, round_trip=True) data = {**data, **(update or {})} copied = self.model_validate(data)

copy(*, include: AbstractSetIntStr | MappingIntStrAny | None = None, exclude: AbstractSetIntStr | MappingIntStrAny | None = None, update: Dict[str, Any] | None = None, deep: bool = False) -> Self

参数

名称 说明
include
必需
<xref:AbstractSetIntStr> | <xref:MappingIntStrAny> | None

可选设置或映射,指定要包含在复制模型中的字段。

exclude
必需
<xref:AbstractSetIntStr> | <xref:MappingIntStrAny> | None

可选设置或映射,指定要在复制的模型中排除的字段。

update
必需

可选字段值对字典,用于替代复制模型中的字段值。

deep
必需

如果为 True,则为 Pydantic 模型的字段值将进行深层复制。

仅限关键字的参数

名称 说明
include
默认值: None
exclude
默认值: None
update
默认值: None
deep
默认值: False

返回

类型 说明

包含、排除和更新字段的模型副本,如指定。

dict

dict(*, include: set[int] | set[str] | Mapping[int, set[int] | set[str] | Mapping[int, IncEx | bool] | Mapping[str, IncEx | bool] | bool] | Mapping[str, set[int] | set[str] | Mapping[int, IncEx | bool] | Mapping[str, IncEx | bool] | bool] | None = None, exclude: set[int] | set[str] | Mapping[int, set[int] | set[str] | Mapping[int, IncEx | bool] | Mapping[str, IncEx | bool] | bool] | Mapping[str, set[int] | set[str] | Mapping[int, IncEx | bool] | Mapping[str, IncEx | bool] | bool] | None = None, by_alias: bool = False, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = False) -> Dict[str, Any]

参数

名称 说明
include
必需
set[int] | set[str] | Mapping[int, set[int] | set[str] | Mapping[int, <xref:IncEx> | bool] | Mapping[str, <xref:IncEx> | bool] | bool] | Mapping[str, set[int] | set[str] | Mapping[int, <xref:IncEx> | bool] | Mapping[str, <xref:IncEx> | bool] | bool] | None
exclude
必需
set[int] | set[str] | Mapping[int, set[int] | set[str] | Mapping[int, <xref:IncEx> | bool] | Mapping[str, <xref:IncEx> | bool] | bool] | Mapping[str, set[int] | set[str] | Mapping[int, <xref:IncEx> | bool] | Mapping[str, <xref:IncEx> | bool] | bool] | None
by_alias
必需
exclude_unset
必需
exclude_defaults
必需
exclude_none
必需

仅限关键字的参数

名称 说明
include
默认值: None
exclude
默认值: None
by_alias
默认值: False
exclude_unset
默认值: False
exclude_defaults
默认值: False
exclude_none
默认值: False

返回

类型 说明

from_orm

from_orm(obj: Any) -> Self

参数

名称 说明
obj
必需
Any

返回

类型 说明

json

json(*, include: set[int] | set[str] | Mapping[int, set[int] | set[str] | Mapping[int, IncEx | bool] | Mapping[str, IncEx | bool] | bool] | Mapping[str, set[int] | set[str] | Mapping[int, IncEx | bool] | Mapping[str, IncEx | bool] | bool] | None = None, exclude: set[int] | set[str] | Mapping[int, set[int] | set[str] | Mapping[int, IncEx | bool] | Mapping[str, IncEx | bool] | bool] | Mapping[str, set[int] | set[str] | Mapping[int, IncEx | bool] | Mapping[str, IncEx | bool] | bool] | None = None, by_alias: bool = False, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = False, encoder: Callable[[Any], Any] | None = PydanticUndefined, models_as_dict: bool = PydanticUndefined, **dumps_kwargs: Any) -> str

参数

名称 说明
include
必需
set[int] | set[str] | Mapping[int, set[int] | set[str] | Mapping[int, <xref:IncEx> | bool] | Mapping[str, <xref:IncEx> | bool] | bool] | Mapping[str, set[int] | set[str] | Mapping[int, <xref:IncEx> | bool] | Mapping[str, <xref:IncEx> | bool] | bool] | None
exclude
必需
set[int] | set[str] | Mapping[int, set[int] | set[str] | Mapping[int, <xref:IncEx> | bool] | Mapping[str, <xref:IncEx> | bool] | bool] | Mapping[str, set[int] | set[str] | Mapping[int, <xref:IncEx> | bool] | Mapping[str, <xref:IncEx> | bool] | bool] | None
by_alias
必需
exclude_unset
必需
exclude_defaults
必需
exclude_none
必需
encoder
必需
models_as_dict
必需
dumps_kwargs
必需
Any

仅限关键字的参数

名称 说明
include
默认值: None
exclude
默认值: None
by_alias
默认值: False
exclude_unset
默认值: False
exclude_defaults
默认值: False
exclude_none
默认值: False
encoder
默认值: PydanticUndefined
models_as_dict
默认值: PydanticUndefined

返回

类型 说明
str

model_construct

使用已验证的数据创建 Model 类的新实例。

从受信任的或预先验证的数据创建一个新的模型设置 听写pydantic_fields_set 。 遵循默认值,但不会执行其他验证。

!!! 请注意 ,model_construct() 通常遵循提供的模型的 model_config.extra 设置。 也就是说,如果 model_config.extra == “allow”,则将所有额外的传递值添加到模型实例的 听写pydantic_extra 字段。 如果 model_config.extra == 'ignore' (默认值),则忽略所有额外的传递值。 由于对 model_construct() 的调用不执行验证,因此如果传递了额外的值,则 model_config.extra == 'forbid' 不会产生错误,但将被忽略。

model_construct(_fields_set: set[str] | None = None, **values: Any) -> Self

参数

名称 说明
_fields_set
set[str] | None

在实例化期间最初显式设置的一组字段名称。 如果提供,则直接用于 [model_fields_set][pydantic。BaseModel.model_fields_set] 属性。 否则,将使用 参数中的字段名称。

默认值: None
values
必需
Any

受信任的或预先验证的数据字典。

返回

类型 说明

具有已验证数据的 Model 类的新实例。

model_copy

!!! 抽象的“使用情况文档” model_copy

返回模型的副本。

!!! 请注意基础实例的 [dict][object。复制 dict] 属性。 如果在模型字段中存储任何内容(例如[缓存属性][functools.cached_property]的值),则可能会产生意外的副作用。

model_copy(*, update: Mapping[str, Any] | None = None, deep: bool = False) -> Self

参数

名称 说明
update
必需

要更改/添加新模型中的值。 注意:创建新模型之前不会验证数据。 应信任此数据。

deep
必需

设置为 True 以创建模型的深层副本。

仅限关键字的参数

名称 说明
update
默认值: None
deep
默认值: False

返回

类型 说明

新的模型实例。

model_dump

!!! 抽象的“使用情况文档” model_dump

生成模型的字典表示形式,可以选择指定要包含或排除的字段。

model_dump(*, mode: Literal['json', 'python'] | str = 'python', include: set[int] | set[str] | Mapping[int, set[int] | set[str] | Mapping[int, IncEx | bool] | Mapping[str, IncEx | bool] | bool] | Mapping[str, set[int] | set[str] | Mapping[int, IncEx | bool] | Mapping[str, IncEx | bool] | bool] | None = None, exclude: set[int] | set[str] | Mapping[int, set[int] | set[str] | Mapping[int, IncEx | bool] | Mapping[str, IncEx | bool] | bool] | Mapping[str, set[int] | set[str] | Mapping[int, IncEx | bool] | Mapping[str, IncEx | bool] | bool] | None = None, context: Any | None = None, by_alias: bool | None = None, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = False, exclude_computed_fields: bool = False, round_trip: bool = False, warnings: bool | Literal['none', 'warn', 'error'] = True, fallback: Callable[[Any], Any] | None = None, serialize_as_any: bool = False, polymorphic_serialization: bool | None = None) -> dict[str, Any]

参数

名称 说明
mode
必需
Literal['json', 'python'] | str

运行to_python的模式。 如果模式为“json”,则输出将仅包含 JSON 可序列化类型。 如果模式为“python”,则输出可能包含不可 JSON 序列化的 Python 对象。

include
必需
set[int] | set[str] | Mapping[int, set[int] | set[str] | Mapping[int, <xref:IncEx> | bool] | Mapping[str, <xref:IncEx> | bool] | bool] | Mapping[str, set[int] | set[str] | Mapping[int, <xref:IncEx> | bool] | Mapping[str, <xref:IncEx> | bool] | bool] | None

要包含在输出中的一组字段。

exclude
必需
set[int] | set[str] | Mapping[int, set[int] | set[str] | Mapping[int, <xref:IncEx> | bool] | Mapping[str, <xref:IncEx> | bool] | bool] | Mapping[str, set[int] | set[str] | Mapping[int, <xref:IncEx> | bool] | Mapping[str, <xref:IncEx> | bool] | bool] | None

要从输出中排除的一组字段。

context
必需
Any | None

要传递给序列化程序的其他上下文。

by_alias
必需

如果已定义,是否在字典键中使用字段的别名。

exclude_unset
必需

是否排除尚未显式设置的字段。

exclude_defaults
必需

是否排除设置为其默认值的字段。

exclude_none
必需

是否排除值为 None 的字段。

exclude_computed_fields
必需

是否排除计算字段。 虽然这可用于往返,但通常建议改用专用 round_trip 参数。

round_trip
必需

如果为 True,则转储的值应作为非幂等类型的输入有效,例如 Json[T]。

warnings
必需
bool | Literal['none', 'warn', 'error']

如何处理序列化错误。 False/“none”忽略它们,True/“warn”日志错误,“error”将引发 [PydanticSerializationError][pydantic_core。PydanticSerializationError]。

fallback
必需

遇到未知值时要调用的函数。 如果未提供,则为 [PydanticSerializationError][pydantic_core。引发 PydanticSerializationError] 错误。

serialize_as_any
必需

是否使用鸭子键入序列化行为序列化字段。

polymorphic_serialization
必需

是否为此调用使用模型和数据类多态序列化。

仅限关键字的参数

名称 说明
mode
默认值: 'python'
include
默认值: None
exclude
默认值: None
context
默认值: None
by_alias
默认值: None
exclude_unset
默认值: False
exclude_defaults
默认值: False
exclude_none
默认值: False
exclude_computed_fields
默认值: False
round_trip
默认值: False
warnings
默认值: True
fallback
默认值: None
serialize_as_any
默认值: False
polymorphic_serialization
默认值: None

返回

类型 说明

模型的字典表示形式。

model_dump_json

!!! 抽象的“使用情况文档” model_dump_json

使用 Pydantic 的 to_json 方法生成模型的 JSON 表示形式。

model_dump_json(*, indent: int | None = None, ensure_ascii: bool = False, include: set[int] | set[str] | Mapping[int, set[int] | set[str] | Mapping[int, IncEx | bool] | Mapping[str, IncEx | bool] | bool] | Mapping[str, set[int] | set[str] | Mapping[int, IncEx | bool] | Mapping[str, IncEx | bool] | bool] | None = None, exclude: set[int] | set[str] | Mapping[int, set[int] | set[str] | Mapping[int, IncEx | bool] | Mapping[str, IncEx | bool] | bool] | Mapping[str, set[int] | set[str] | Mapping[int, IncEx | bool] | Mapping[str, IncEx | bool] | bool] | None = None, context: Any | None = None, by_alias: bool | None = None, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = False, exclude_computed_fields: bool = False, round_trip: bool = False, warnings: bool | Literal['none', 'warn', 'error'] = True, fallback: Callable[[Any], Any] | None = None, serialize_as_any: bool = False, polymorphic_serialization: bool | None = None) -> str

参数

名称 说明
indent
必需
int | None

要在 JSON 输出中使用的缩进。 如果传递 None,输出将压缩。

ensure_ascii
必需

如果 为 True,则保证输出将转义所有传入的非 ASCII 字符。 如果 为 False (默认值),这些字符将输出 as-is。

include
必需
set[int] | set[str] | Mapping[int, set[int] | set[str] | Mapping[int, <xref:IncEx> | bool] | Mapping[str, <xref:IncEx> | bool] | bool] | Mapping[str, set[int] | set[str] | Mapping[int, <xref:IncEx> | bool] | Mapping[str, <xref:IncEx> | bool] | bool] | None

要包含在 JSON 输出中的 Field(s)。

exclude
必需
set[int] | set[str] | Mapping[int, set[int] | set[str] | Mapping[int, <xref:IncEx> | bool] | Mapping[str, <xref:IncEx> | bool] | bool] | Mapping[str, set[int] | set[str] | Mapping[int, <xref:IncEx> | bool] | Mapping[str, <xref:IncEx> | bool] | bool] | None

要从 JSON 输出中排除的字段(s)。

context
必需
Any | None

要传递给序列化程序的其他上下文。

by_alias
必需

是否使用字段别名进行序列化。

exclude_unset
必需

是否排除尚未显式设置的字段。

exclude_defaults
必需

是否排除设置为其默认值的字段。

exclude_none
必需

是否排除值为 None 的字段。

exclude_computed_fields
必需

是否排除计算字段。 虽然这可用于往返,但通常建议改用专用 round_trip 参数。

round_trip
必需

如果为 True,则转储的值应作为非幂等类型的输入有效,例如 Json[T]。

warnings
必需
bool | Literal['none', 'warn', 'error']

如何处理序列化错误。 False/“none”忽略它们,True/“warn”日志错误,“error”将引发 [PydanticSerializationError][pydantic_core。PydanticSerializationError]。

fallback
必需

遇到未知值时要调用的函数。 如果未提供,则为 [PydanticSerializationError][pydantic_core。引发 PydanticSerializationError] 错误。

serialize_as_any
必需

是否使用鸭子键入序列化行为序列化字段。

polymorphic_serialization
必需

是否为此调用使用模型和数据类多态序列化。

仅限关键字的参数

名称 说明
indent
默认值: None
ensure_ascii
默认值: False
include
默认值: None
exclude
默认值: None
context
默认值: None
by_alias
默认值: None
exclude_unset
默认值: False
exclude_defaults
默认值: False
exclude_none
默认值: False
exclude_computed_fields
默认值: False
round_trip
默认值: False
warnings
默认值: True
fallback
默认值: None
serialize_as_any
默认值: False
polymorphic_serialization
默认值: None

返回

类型 说明
str

模型的 JSON 字符串表示形式。

model_json_schema

为模型类生成 JSON 架构。

model_json_schema(by_alias: bool = True, ref_template: str = DEFAULT_REF_TEMPLATE, schema_generator: type[GenerateJsonSchema] = GenerateJsonSchema, mode: Literal['validation', 'serialization'] = 'validation', *, union_format: Literal['any_of', 'primitive_type_array'] = 'any_of') -> dict[str, Any]

参数

名称 说明
by_alias

是否使用属性别名。

默认值: True
ref_template
str

引用模板。

默认值: DEFAULT_REF_TEMPLATE
union_format
必需
Literal['any_of', 'primitive_type_array']

将架构从联合组合在一起时要使用的格式。 可以是以下选项之一:

  • “any_of”:使用 anyOf

用于组合架构的关键字(默认值)。

  • “primitive_type_array”:使用 type 关键字作为字符串数组,其中包含每种类型的组合。 如果任一架构不是基元类型(字符串布尔值null整数数字)或包含约束/元数据,则回退到 any_of
schema_generator
type[<xref:pydantic.json_schema.GenerateJsonSchema>]

若要重写用于生成 JSON 架构的逻辑,请使用所需的修改作为 GenerateJsonSchema 的子类

默认值: GenerateJsonSchema
mode
Literal['validation', 'serialization']

要在其中生成架构的模式。

默认值: 'validation'

仅限关键字的参数

名称 说明
union_format
默认值: 'any_of'

返回

类型 说明

给定模型类的 JSON 架构。

model_parametrized_name

计算泛型类参数化的类名。

可以重写此方法以实现泛型 BaseModel 的自定义命名方案。

model_parametrized_name(params: tuple[type[Any], ...]) -> str

参数

名称 说明
params
必需
tuple[type[Any], ...]

类类型的元组。 给定具有 2 个类型变量的泛型类 模型 和具体模型 Model[str, int],值 (str, int) 将传递给 参数

返回

类型 说明
str

表示参数作为类型变量传递给 cls 的新类的字符串。

例外

类型 说明

尝试为非泛型模型生成具体名称时引发。

model_post_init

重写此方法以在 初始化model_construct后执行其他初始化。 如果要进行一些需要初始化整个模型的验证,这非常有用。

model_post_init(context: Any, /) -> None

仅限位置的参数

名称 说明
context
必需

参数

名称 说明
context
必需
Any

返回

类型 说明

model_rebuild

尝试为模型重新生成 pydantic-core 架构。

如果其中一个批注是 ForwardRef,在最初尝试生成架构时无法解析,并且自动重新生成失败,则可能是必需的。

model_rebuild(*, force: bool = False, raise_errors: bool = True, _parent_namespace_depth: int = 2, _types_namespace: MappingNamespace | None = None) -> bool | None

参数

名称 说明
force
必需

是否强制重新生成模型架构,默认为 False

raise_errors
必需

是否引发错误,默认值为 True

_parent_namespace_depth
必需
int

父命名空间的深度级别默认为 2。

_types_namespace
必需
<xref:MappingNamespace> | None

类型命名空间默认为 None

仅限关键字的参数

名称 说明
force
默认值: False
raise_errors
默认值: True
_parent_namespace_depth
默认值: 2
_types_namespace
默认值: None

返回

类型 说明

如果架构已“完成”且不需要重新生成,则返回 None 。 如果需要 重新生成 ,则如果重新生成成功,则返回 True ,否则返回 False

model_validate

验证 pydantic 模型实例。

model_validate(obj: Any, *, strict: bool | None = None, extra: Literal['allow', 'ignore', 'forbid'] | None = None, from_attributes: bool | None = None, context: Any | None = None, by_alias: bool | None = None, by_name: bool | None = None) -> Self

参数

名称 说明
obj
必需
Any

要验证的对象。

strict
必需

是否严格强制实施类型。

extra
必需
Literal['allow', 'ignore', 'forbid'] | None

无论是在模型验证期间忽略、允许还是禁止额外数据。 请参阅 [额外 配置值][pydantic。ConfigDict.extra] 了解详细信息。

from_attributes
必需

是否从对象属性中提取数据。

context
必需
Any | None

要传递给验证程序的其他上下文。

by_alias
必需

是否在针对提供的输入数据进行验证时使用字段的别名。

by_name
必需

在针对提供的输入数据进行验证时,是否使用字段的名称。

仅限关键字的参数

名称 说明
strict
默认值: None
extra
默认值: None
from_attributes
默认值: None
context
默认值: None
by_alias
默认值: None
by_name
默认值: None

返回

类型 说明

已验证的模型实例。

例外

类型 说明
ValidationError

如果无法验证对象。

model_validate_json

!!! 抽象的“使用情况文档” JSON 分析

根据 Pydantic 模型验证给定的 JSON 数据。

model_validate_json(json_data: str | bytes | bytearray, *, strict: bool | None = None, extra: Literal['allow', 'ignore', 'forbid'] | None = None, context: Any | None = None, by_alias: bool | None = None, by_name: bool | None = None) -> Self

参数

名称 说明
json_data
必需

要验证的 JSON 数据。

strict
必需

是否严格强制实施类型。

extra
必需
Literal['allow', 'ignore', 'forbid'] | None

无论是在模型验证期间忽略、允许还是禁止额外数据。 请参阅 [额外 配置值][pydantic。ConfigDict.extra] 了解详细信息。

context
必需
Any | None

要传递给验证程序的额外变量。

by_alias
必需

是否在针对提供的输入数据进行验证时使用字段的别名。

by_name
必需

在针对提供的输入数据进行验证时,是否使用字段的名称。

仅限关键字的参数

名称 说明
strict
默认值: None
extra
默认值: None
context
默认值: None
by_alias
默认值: None
by_name
默认值: None

返回

类型 说明

已验证的 Pydantic 模型。

例外

类型 说明
ValidationError

如果 json_data 不是 JSON 字符串,或者无法验证对象。

model_validate_strings

使用针对 Pydantic 模型的字符串数据验证给定对象。

model_validate_strings(obj: Any, *, strict: bool | None = None, extra: Literal['allow', 'ignore', 'forbid'] | None = None, context: Any | None = None, by_alias: bool | None = None, by_name: bool | None = None) -> Self

参数

名称 说明
obj
必需
Any

包含要验证的字符串数据的对象。

strict
必需

是否严格强制实施类型。

extra
必需
Literal['allow', 'ignore', 'forbid'] | None

无论是在模型验证期间忽略、允许还是禁止额外数据。 请参阅 [额外 配置值][pydantic。ConfigDict.extra] 了解详细信息。

context
必需
Any | None

要传递给验证程序的额外变量。

by_alias
必需

是否在针对提供的输入数据进行验证时使用字段的别名。

by_name
必需

在针对提供的输入数据进行验证时,是否使用字段的名称。

仅限关键字的参数

名称 说明
strict
默认值: None
extra
默认值: None
context
默认值: None
by_alias
默认值: None
by_name
默认值: None

返回

类型 说明

已验证的 Pydantic 模型。

parse_file

parse_file(path: str | Path, *, content_type: str | None = None, encoding: str = 'utf8', proto: DeprecatedParseProtocol | None = None, allow_pickle: bool = False) -> Self

参数

名称 说明
path
必需
str | <xref:Path>
content_type
必需
str | None
encoding
必需
str
proto
必需
<xref:DeprecatedParseProtocol> | None
allow_pickle
必需

仅限关键字的参数

名称 说明
content_type
默认值: None
encoding
默认值: 'utf8'
proto
默认值: None
allow_pickle
默认值: False

返回

类型 说明

parse_obj

parse_obj(obj: Any) -> Self

参数

名称 说明
obj
必需
Any

返回

类型 说明

parse_raw

parse_raw(b: str | bytes, *, content_type: str | None = None, encoding: str = 'utf8', proto: DeprecatedParseProtocol | None = None, allow_pickle: bool = False) -> Self

参数

名称 说明
b
必需
content_type
必需
str | None
encoding
必需
str
proto
必需
<xref:DeprecatedParseProtocol> | None
allow_pickle
必需

仅限关键字的参数

名称 说明
content_type
默认值: None
encoding
默认值: 'utf8'
proto
默认值: None
allow_pickle
默认值: False

返回

类型 说明

schema

schema(by_alias: bool = True, ref_template: str = DEFAULT_REF_TEMPLATE) -> Dict[str, Any]

参数

名称 说明
by_alias
默认值: True
ref_template
str
默认值: DEFAULT_REF_TEMPLATE

返回

类型 说明

schema_json

schema_json(*, by_alias: bool = True, ref_template: str = DEFAULT_REF_TEMPLATE, **dumps_kwargs: Any) -> str

参数

名称 说明
by_alias
必需
ref_template
必需
str
dumps_kwargs
必需
Any

仅限关键字的参数

名称 说明
by_alias
默认值: True
ref_template
默认值: DEFAULT_REF_TEMPLATE

返回

类型 说明
str

update_forward_refs

update_forward_refs(**localns: Any) -> None

参数

名称 说明
localns
必需
Any

返回

类型 说明

validate

validate(value: Any) -> Self

参数

名称 说明
value
必需
Any

返回

类型 说明

属性

model_extra

获取在验证期间设置的额外字段。

返回

类型 说明

额外字段的字典;如果未将 config.extra 设置为“allow”,则为 None

model_fields_set

返回在此模型实例上显式设置的字段集。

返回

类型 说明

一组字符串,表示已设置的字段,即未从默认值填充的字段。

id

消息的可选唯一标识符。

field id: str | None = None

role

消息发送者(用户、助理或系统)的角色。

field role: Literal['user', 'assistant', 'system'] [Required]

content

消息的文本内容。

field content: str [Required]

timestamp

创建消息时的可选时间戳。

field timestamp: datetime | None = None