Skip to content

PromptMessage

Base classes

Name Children Inherits
BaseModel
pydantic.main
!!! abstract "Usage Documentation"

⋔ Inheritance diagram

graph TD
  279755353["models.PromptMessage"]
  3879467795["main.BaseModel"]
  2751680898["builtins.object"]
  3879467795 --> 279755353
  2751680898 --> 3879467795

🛈 DocStrings

Bases: BaseModel

A message in a prompt template.

Source code in src/llmling/prompts/models.py
class PromptMessage(BaseModel):
    """A message in a prompt template."""

    role: MessageRole
    content: str | MessageContent | list[MessageContent] = ""

    model_config = ConfigDict(frozen=True)

    def get_text_content(self) -> str:
        """Get text content of message."""
        match self.content:
            case str():
                return self.content
            case MessageContent() if self.content.type == "text":
                return self.content.content
            case list() if self.content:
                # Join text content items with space
                text_items = [
                    item.content
                    for item in self.content
                    if isinstance(item, MessageContent) and item.type == "text"
                ]
                return " ".join(text_items) if text_items else ""
            case _:
                return ""

get_text_content

get_text_content() -> str

Get text content of message.

Source code in src/llmling/prompts/models.py
def get_text_content(self) -> str:
    """Get text content of message."""
    match self.content:
        case str():
            return self.content
        case MessageContent() if self.content.type == "text":
            return self.content.content
        case list() if self.content:
            # Join text content items with space
            text_items = [
                item.content
                for item in self.content
                if isinstance(item, MessageContent) and item.type == "text"
            ]
            return " ".join(text_items) if text_items else ""
        case _:
            return ""

:fa-brands-github: Show source on GitHub