Core classes that carry data through the system.
Module haystack_experimental.dataclasses.chat_message
ChatMessage
Represents a message in a LLM chat conversation.
Use the from_assistant
, from_user
, from_system
, and from_tool
class methods to create a ChatMessage.
ChatMessage.images
@property
def images() -> List[ImageContent]
Returns the list of all images contained in the message.
ChatMessage.image
@property
def image() -> Optional[ImageContent]
Returns the first image contained in the message.
ChatMessage.from_user
@classmethod
def from_user(
cls,
text: Optional[str] = None,
meta: Optional[Dict[str, Any]] = None,
name: Optional[str] = None,
*,
content_parts: Optional[Sequence[Union[TextContent, str,
ImageContent]]] = None
) -> "ChatMessage"
Create a message from the user.
Arguments:
text
: The text content of the message. Specify this or content_parts.meta
: Additional metadata associated with the message.name
: An optional name for the participant. This field is only supported by OpenAI.content_parts
: A list of content parts to include in the message. Specify this or text.
Returns:
A new ChatMessage instance.
ChatMessage.to_dict
def to_dict() -> Dict[str, Any]
Converts ChatMessage into a dictionary.
Returns:
Serialized version of the object.
ChatMessage.to_openai_dict_format
def to_openai_dict_format() -> Dict[str, Any]
Convert a ChatMessage to the dictionary format expected by OpenAI's Chat API.
Module haystack_experimental.dataclasses.image_content
ImageContent
The image content of a chat message.
Arguments:
base64_image
: A base64 string representing the image.mime_type
: The MIME type of the image (e.g. "image/png", "image/jpeg"). Providing this value is recommended, as most LLM providers require it. If not provided, the MIME type is guessed from the base64 string, which can be slow and not always reliable.detail
: Optional detail level of the image (only supported by OpenAI). One of "auto", "high", or "low".meta
: Optional metadata for the image.validate
: If True (default), a validation process is performed:- Check whether the base64 string is valid;
- Guess the MIME type if not provided;
- Check if the MIME type is a valid image MIME type. Set to False to skip validation and speed up initialization.
ImageContent.__repr__
def __repr__() -> str
Return a string representation of the ImageContent, truncating the base64_image to 100 bytes.
ImageContent.show
def show() -> None
Shows the image.
ImageContent.from_file_path
@classmethod
def from_file_path(cls,
file_path: Union[str, Path],
*,
size: Optional[Tuple[int, int]] = None,
detail: Optional[Literal["auto", "high", "low"]] = None,
meta: Optional[Dict[str, Any]] = None) -> "ImageContent"
Create an ImageContent object from a file path.
It exposes similar functionality as the ImageFileToImageContent
component. For PDF to ImageContent conversion,
use the PDFToImageContent
component.
Arguments:
file_path
: The path to the image file. PDF files are not supported. For PDF to ImageContent conversion, use thePDFToImageContent
component.size
: If provided, resizes the image to fit within the specified dimensions (width, height) while maintaining aspect ratio. This reduces file size, memory usage, and processing time, which is beneficial when working with models that have resolution constraints or when transmitting images to remote services.detail
: Optional detail level of the image (only supported by OpenAI). One of "auto", "high", or "low".meta
: Additional metadata for the image.
Returns:
An ImageContent object.
ImageContent.from_url
@classmethod
def from_url(cls,
url: str,
*,
retry_attempts: int = 2,
timeout: int = 10,
size: Optional[Tuple[int, int]] = None,
detail: Optional[Literal["auto", "high", "low"]] = None,
meta: Optional[Dict[str, Any]] = None) -> "ImageContent"
Create an ImageContent object from a URL. The image is downloaded and converted to a base64 string.
For PDF to ImageContent conversion, use the PDFToImageContent
component.
Arguments:
url
: The URL of the image. PDF files are not supported. For PDF to ImageContent conversion, use thePDFToImageContent
component.retry_attempts
: The number of times to retry to fetch the URL's content.timeout
: Timeout in seconds for the request.size
: If provided, resizes the image to fit within the specified dimensions (width, height) while maintaining aspect ratio. This reduces file size, memory usage, and processing time, which is beneficial when working with models that have resolution constraints or when transmitting images to remote services.detail
: Optional detail level of the image (only supported by OpenAI). One of "auto", "high", or "low".meta
: Additional metadata for the image.
Raises:
ValueError
: If the URL does not point to an image or if it points to a PDF file.
Returns:
An ImageContent object.