Message
Base classes
| Name |
Children |
Inherits |
Schema schemez.schema Base class for configuration models. |
|
|
⋔ Inheritance diagram
graph TD
1367365344["typedefs.Message"]
330168112["schema.Schema"]
3879467795["main.BaseModel"]
2751680898["builtins.object"]
330168112 --> 1367365344
3879467795 --> 330168112
2751680898 --> 3879467795
🛈 DocStrings
Bases: Schema
A chat message.
Source code in src/llmling/core/typedefs.py
| class Message(Schema):
"""A chat message."""
role: MessageRole
content: str = "" # Keep for backward compatibility
content_items: list[MessageContent] = Field(default_factory=list)
name: str | None = None
tool_calls: list[ToolCall] | None = None
tool_call_id: str | None = None
model_config = ConfigDict(frozen=True)
@model_validator(mode="before")
@classmethod
def ensure_content_items(cls, data: dict[str, Any]) -> dict[str, Any]:
"""Ensure content_items is populated from content if empty."""
if isinstance(data, dict):
content = data.get("content", "")
content_items = data.get("content_items", [])
# Only create content_items from content if we have content and no items
if content and not content_items:
data["content_items"] = [MessageContent(type="text", content=content).model_dump()]
# Always keep content in sync with first text content item
elif content_items:
text_items = [
item
for item in content_items
if isinstance(item, dict) and item.get("type") == "text"
]
if text_items:
data["content"] = text_items[0]["content"]
return data
@classmethod
def from_dict(cls, data: dict[str, Any]) -> Message:
"""Create message from dict representation."""
# Handle content_items if present
if "content_items" in data and isinstance(data["content_items"], list):
data["content_items"] = [
MessageContent(**item) if isinstance(item, dict) else item
for item in data["content_items"]
]
# Handle tool_calls if present
if "tool_calls" in data and isinstance(data["tool_calls"], list):
data["tool_calls"] = [
ToolCall(**call) if isinstance(call, dict) else call for call in data["tool_calls"]
]
return cls(**data)
def to_dict(self) -> dict[str, Any]:
"""Convert to dict for serialization."""
return self.model_dump(exclude_none=True)
|
ensure_content_items
classmethod
ensure_content_items(data: dict[str, Any]) -> dict[str, Any]
Ensure content_items is populated from content if empty.
Source code in src/llmling/core/typedefs.py
| @model_validator(mode="before")
@classmethod
def ensure_content_items(cls, data: dict[str, Any]) -> dict[str, Any]:
"""Ensure content_items is populated from content if empty."""
if isinstance(data, dict):
content = data.get("content", "")
content_items = data.get("content_items", [])
# Only create content_items from content if we have content and no items
if content and not content_items:
data["content_items"] = [MessageContent(type="text", content=content).model_dump()]
# Always keep content in sync with first text content item
elif content_items:
text_items = [
item
for item in content_items
if isinstance(item, dict) and item.get("type") == "text"
]
if text_items:
data["content"] = text_items[0]["content"]
return data
|
from_dict
classmethod
from_dict(data: dict[str, Any]) -> Message
Create message from dict representation.
Source code in src/llmling/core/typedefs.py
| @classmethod
def from_dict(cls, data: dict[str, Any]) -> Message:
"""Create message from dict representation."""
# Handle content_items if present
if "content_items" in data and isinstance(data["content_items"], list):
data["content_items"] = [
MessageContent(**item) if isinstance(item, dict) else item
for item in data["content_items"]
]
# Handle tool_calls if present
if "tool_calls" in data and isinstance(data["tool_calls"], list):
data["tool_calls"] = [
ToolCall(**call) if isinstance(call, dict) else call for call in data["tool_calls"]
]
return cls(**data)
|
to_dict
to_dict() -> dict[str, Any]
Convert to dict for serialization.
Source code in src/llmling/core/typedefs.py
| def to_dict(self) -> dict[str, Any]:
"""Convert to dict for serialization."""
return self.model_dump(exclude_none=True)
|
:fa-brands-github: Show source on GitHub