| import uuid | |
| from datetime import datetime | |
| from pydantic import BaseModel, Field, computed_field | |
| from typing import Optional | |
| # ------------------- | |
| # SCHEMAS FOR INPUT | |
| # ------------------- | |
| class MessageCreate(BaseModel): | |
| content: str = Field(..., description="The text content of the user's message.") | |
| # ------------------- | |
| # SCHEMAS FOR OUTPUT | |
| # ------------------- | |
| class MessageRead(BaseModel): | |
| id: uuid.UUID | |
| role: str | |
| created_at: datetime | |
| links: Optional[list[str]] = None | |
| raw_content: Optional[str] = Field(None, alias='content', exclude=True) | |
| answer: Optional[str] = Field(None, alias='answer', exclude=True) | |
| # Step 2: The computed field now accesses the private, guaranteed-to-exist attributes | |
| # on the MessageRead instance itself. | |
| def content(self) -> str: | |
| return self.answer if self.answer is not None else self.raw_content | |
| class Config: | |
| from_attributes = True |