I’d like for a field in my Pydantic model, used as a schema, to be populated based on what has been parsed in the preceding fields. For example
class Category(str, Enum):
violence = "violence"
sexual = "sexual"
self_harm = "self_harm"
swearing = "swearing"
class CensoredExplanation(BaseModel):
censored_explanation: str = Field(description="same content as
explanation_if_violating but censored (use *** for censored words)")
class ContentCompliance(BaseModel):
is_violating: bool
original_user_message: str = Field(description="the original user message")
category: Optional[Category]
explanation_if_violating: Optional[str]
explanation_censored: Optional[CensoredExplanation] = Field(description="same content as explanation_if_violating but censored (use *** for censored words)")
@field_validator("explanation_if_violating", mode="before")
@classmethod
def validate_explanation_if_violating(cls, v, info):
if "fuck" in info.data.get("original_user_message"):
v = "This is a violation of the guidelines because it contains the word 'fuck'"
return v
In this example, the response “explanation censored” should be “This is a violation of the guidelines because it contains the word ‘****’”
I’d rather not hard code any particular string, I’d like the llm to fill in the field based off what it parsed in the previous field.