FieldSpec
Base classes
| Name |
Children |
Inherits |
BaseModel pydantic.main !!! abstract "Usage Documentation" |
|
|
⋔ Inheritance diagram
graph TD
1643032872["model_spec.FieldSpec"]
3879467795["main.BaseModel"]
2751680898["builtins.object"]
3879467795 --> 1643032872
2751680898 --> 3879467795
🛈 DocStrings
Bases: BaseModel
One field of a model being designed.
Source code in src/promptantic/model_spec.py
| class FieldSpec(BaseModel):
"""One field of a model being designed."""
# pydantic doesn't allow field names with a leading underscore
name: str = Field(min_length=1, pattern=r"^[A-Za-z][A-Za-z0-9_]*$")
type: FieldType = "string"
required: bool = True
default: str | None = Field(
default=None, description="As you'd type it: 42, true, a text, or JSON for lists"
)
description: str | None = None
items: ItemType = Field(default="string", description="Item type (for lists)")
fields: list[FieldSpec] = Field(default=[], description="Its fields (for objects)")
@field_validator("default")
@classmethod
def _check_default(cls, value: str | None, info: ValidationInfo) -> str | None:
"""Reject a default that isn't a value of the field's type (here, in the form)."""
if value is not None and (kind := info.data.get("type")) is not None:
_parse_default(kind, value) # raises ValueError, shown at the field
return value
@field_validator("fields")
@classmethod
def _check_fields(cls, value: list[FieldSpec]) -> list[FieldSpec]:
return _unique_names(value)
def json_schema(self) -> dict[str, Any]:
"""The field as a JSON Schema property."""
match self.type:
case "list":
schema: dict[str, Any] = {"type": "array", "items": dict(_SCHEMAS[self.items])}
case "object":
schema = _object_schema(self.fields)
case kind:
schema = dict(_SCHEMAS[kind])
if self.description:
schema["description"] = self.description
if self.default is not None:
schema["default"] = _parse_default(self.type, self.default)
return schema
|
json_schema
json_schema() -> dict[str, Any]
The field as a JSON Schema property.
Source code in src/promptantic/model_spec.py
| def json_schema(self) -> dict[str, Any]:
"""The field as a JSON Schema property."""
match self.type:
case "list":
schema: dict[str, Any] = {"type": "array", "items": dict(_SCHEMAS[self.items])}
case "object":
schema = _object_schema(self.fields)
case kind:
schema = dict(_SCHEMAS[kind])
if self.description:
schema["description"] = self.description
if self.default is not None:
schema["default"] = _parse_default(self.type, self.default)
return schema
|
:fa-brands-github: Show source on GitHub