Skip to content

Latest commit

 

History

History
1349 lines (848 loc) · 36.2 KB

File metadata and controls

1349 lines (848 loc) · 36.2 KB
title Data Classes
id data-classes-api
description Core classes that carry data through the system.
slug /data-classes-api

answer

ExtractedAnswer

Holds an answer extracted by an extractive Reader (query, score, text, and optional document/context).

to_dict

to_dict() -> dict[str, Any]

Serialize the object to a dictionary.

Returns:

  • dict[str, Any] – Serialized dictionary representation of the object.

from_dict

from_dict(data: dict[str, Any]) -> ExtractedAnswer

Deserialize the object from a dictionary.

Parameters:

  • data (dict[str, Any]) – Dictionary representation of the object.

Returns:

  • ExtractedAnswer – Deserialized object.

GeneratedAnswer

Holds a generated answer from a Generator (answer text, query, referenced documents, and metadata).

to_dict

to_dict() -> dict[str, Any]

Serialize the object to a dictionary.

Returns:

  • dict[str, Any] – Serialized dictionary representation of the object.

from_dict

from_dict(data: dict[str, Any]) -> GeneratedAnswer

Deserialize the object from a dictionary.

Parameters:

  • data (dict[str, Any]) – Dictionary representation of the object.

Returns:

  • GeneratedAnswer – Deserialized object.

breakpoints

Breakpoint

A dataclass to hold a breakpoint for a component.

Parameters:

  • component_name (str) – The name of the component where the breakpoint is set.
  • visit_count (int) – The number of times the component must be visited before the breakpoint is triggered.
  • snapshot_file_path (str | None) – Optional path to store a snapshot of the pipeline when the breakpoint is hit. This is useful for debugging purposes, allowing you to inspect the state of the pipeline at the time of the breakpoint and to resume execution from that point.

to_dict

to_dict() -> dict[str, Any]

Convert the Breakpoint to a dictionary representation.

Returns:

  • dict[str, Any] – A dictionary containing the component name, visit count, and debug path.

from_dict

from_dict(data: dict) -> Breakpoint

Populate the Breakpoint from a dictionary representation.

Parameters:

  • data (dict) – A dictionary containing the component name, visit count, and debug path.

Returns:

  • Breakpoint – An instance of Breakpoint.

ToolBreakpoint

Bases: Breakpoint

A dataclass representing a breakpoint specific to tools used within an Agent component.

Inherits from Breakpoint and adds the ability to target individual tools. If tool_name is None, the breakpoint applies to all tools within the Agent component.

Parameters:

  • tool_name (str | None) – The name of the tool to target within the Agent component. If None, applies to all tools.

AgentBreakpoint

A dataclass representing a breakpoint tied to an Agent’s execution.

This allows for debugging either a specific component (e.g., the chat generator) or a tool used by the agent. It enforces constraints on which component names are valid for each breakpoint type.

Parameters:

  • agent_name (str) – The name of the agent component in a pipeline where the breakpoint is set.
  • break_point (Breakpoint | ToolBreakpoint) – An instance of Breakpoint or ToolBreakpoint indicating where to break execution.

Raises:

  • ValueError – If the component_name is invalid for the given breakpoint type:
  • Breakpoint must have component_name='chat_generator'.
  • ToolBreakpoint must have component_name='tool_invoker'.

to_dict

to_dict() -> dict[str, Any]

Convert the AgentBreakpoint to a dictionary representation.

Returns:

  • dict[str, Any] – A dictionary containing the agent name and the breakpoint details.

from_dict

from_dict(data: dict) -> AgentBreakpoint

Populate the AgentBreakpoint from a dictionary representation.

Parameters:

  • data (dict) – A dictionary containing the agent name and the breakpoint details.

Returns:

  • AgentBreakpoint – An instance of AgentBreakpoint.

AgentSnapshot

Snapshot of an Agent's state at a breakpoint (component inputs, visit counts, and breakpoint).

to_dict

to_dict() -> dict[str, Any]

Convert the AgentSnapshot to a dictionary representation.

Returns:

  • dict[str, Any] – A dictionary containing the agent state, timestamp, and breakpoint.

from_dict

from_dict(data: dict) -> AgentSnapshot

Populate the AgentSnapshot from a dictionary representation.

Parameters:

  • data (dict) – A dictionary containing the agent state, timestamp, and breakpoint.

Returns:

  • AgentSnapshot – An instance of AgentSnapshot.

PipelineState

A dataclass to hold the state of the pipeline at a specific point in time.

Parameters:

  • component_visits (dict[str, int]) – A dictionary mapping component names to their visit counts.
  • inputs (dict[str, Any]) – The inputs processed by the pipeline at the time of the snapshot.
  • pipeline_outputs (dict[str, Any]) – Dictionary containing the final outputs of the pipeline up to the breakpoint.

to_dict

to_dict() -> dict[str, Any]

Convert the PipelineState to a dictionary representation.

Returns:

  • dict[str, Any] – A dictionary containing the inputs, component visits, and pipeline outputs.

from_dict

from_dict(data: dict) -> PipelineState

Populate the PipelineState from a dictionary representation.

Parameters:

  • data (dict) – A dictionary containing the inputs, component visits, and pipeline outputs.

Returns:

  • PipelineState – An instance of PipelineState.

PipelineSnapshot

A dataclass to hold a snapshot of the pipeline at a specific point in time.

Parameters:

  • original_input_data (dict[str, Any]) – The original input data provided to the pipeline.
  • ordered_component_names (list[str]) – A list of component names in the order they were visited.
  • pipeline_state (PipelineState) – The state of the pipeline at the time of the snapshot.
  • break_point (AgentBreakpoint | Breakpoint) – The breakpoint that triggered the snapshot.
  • agent_snapshot (AgentSnapshot | None) – Optional agent snapshot if the breakpoint is an agent breakpoint.
  • timestamp (datetime | None) – A timestamp indicating when the snapshot was taken.
  • include_outputs_from (set[str]) – Set of component names whose outputs should be included in the pipeline results.

to_dict

to_dict() -> dict[str, Any]

Convert the PipelineSnapshot to a dictionary representation.

Returns:

  • dict[str, Any] – A dictionary containing the pipeline state, timestamp, breakpoint, agent snapshot, original input data, ordered component names, include_outputs_from, and pipeline outputs.

from_dict

from_dict(data: dict) -> PipelineSnapshot

Populate the PipelineSnapshot from a dictionary representation.

Parameters:

  • data (dict) – A dictionary containing the pipeline state, timestamp, breakpoint, agent snapshot, original input data, ordered component names, include_outputs_from, and pipeline outputs.

byte_stream

ByteStream

Base data class representing a binary object in the Haystack API.

Parameters:

  • data (bytes) – The binary data stored in Bytestream.
  • meta (dict[str, Any]) – Additional metadata to be stored with the ByteStream.
  • mime_type (str | None) – The mime type of the binary data.

to_file

to_file(destination_path: Path) -> None

Write the ByteStream to a file. Note: the metadata will be lost.

Parameters:

  • destination_path (Path) – The path to write the ByteStream to.

from_file_path

from_file_path(
    filepath: Path,
    mime_type: str | None = None,
    meta: dict[str, Any] | None = None,
    guess_mime_type: bool = False,
) -> ByteStream

Create a ByteStream from the contents read from a file.

Parameters:

  • filepath (Path) – A valid path to a file.
  • mime_type (str | None) – The mime type of the file.
  • meta (dict[str, Any] | None) – Additional metadata to be stored with the ByteStream.
  • guess_mime_type (bool) – Whether to guess the mime type from the file.

from_string

from_string(
    text: str,
    encoding: str = "utf-8",
    mime_type: str | None = None,
    meta: dict[str, Any] | None = None,
) -> ByteStream

Create a ByteStream encoding a string.

Parameters:

  • text (str) – The string to encode
  • encoding (str) – The encoding used to convert the string into bytes
  • mime_type (str | None) – The mime type of the file.
  • meta (dict[str, Any] | None) – Additional metadata to be stored with the ByteStream.

to_string

to_string(encoding: str = 'utf-8') -> str

Convert the ByteStream to a string, metadata will not be included.

Parameters:

  • encoding (str) – The encoding used to convert the bytes to a string. Defaults to "utf-8".

Returns:

  • str – The string representation of the ByteStream.

Raises:

  • UnicodeDecodeError – If the ByteStream data cannot be decoded with the specified encoding.

to_dict

to_dict() -> dict[str, Any]

Convert the ByteStream to a dictionary representation.

Returns:

  • dict[str, Any] – A dictionary with keys 'data', 'meta', and 'mime_type'.

from_dict

from_dict(data: dict[str, Any]) -> ByteStream

Create a ByteStream from a dictionary representation.

Parameters:

  • data (dict[str, Any]) – A dictionary with keys 'data', 'meta', and 'mime_type'.

Returns:

  • ByteStream – A ByteStream instance.

chat_message

ChatRole

Bases: str, Enum

Enumeration representing the roles within a chat.

from_str

from_str(string: str) -> ChatRole

Convert a string to a ChatRole enum.

TextContent

The textual content of a chat message.

Parameters:

  • text (str) – The text content of the message.

to_dict

to_dict() -> dict[str, Any]

Convert TextContent into a dictionary.

from_dict

from_dict(data: dict[str, Any]) -> TextContent

Create a TextContent from a dictionary.

ToolCall

Represents a Tool call prepared by the model, usually contained in an assistant message.

Parameters:

  • id (str | None) – The ID of the Tool call.
  • tool_name (str) – The name of the Tool to call.
  • arguments (dict[str, Any]) – The arguments to call the Tool with.
  • extra (dict[str, Any] | None) – Dictionary of extra information about the Tool call. Use to store provider-specific information. To avoid serialization issues, values should be JSON serializable.

to_dict

to_dict() -> dict[str, Any]

Convert ToolCall into a dictionary.

Returns:

  • dict[str, Any] – A dictionary with keys 'tool_name', 'arguments', 'id', and 'extra'.

from_dict

from_dict(data: dict[str, Any]) -> ToolCall

Creates a new ToolCall object from a dictionary.

Parameters:

  • data (dict[str, Any]) – The dictionary to build the ToolCall object.

Returns:

  • ToolCall – The created object.

ToolCallResult

Represents the result of a Tool invocation.

Parameters:

  • result (ToolCallResultContentT) – The result of the Tool invocation.
  • origin (ToolCall) – The Tool call that produced this result.
  • error (bool) – Whether the Tool invocation resulted in an error.

to_dict

to_dict() -> dict[str, Any]

Converts ToolCallResult into a dictionary.

Returns:

  • dict[str, Any] – A dictionary with keys 'result', 'origin', and 'error'.

from_dict

from_dict(data: dict[str, Any]) -> ToolCallResult

Creates a ToolCallResult from a dictionary.

Parameters:

  • data (dict[str, Any]) – The dictionary to build the ToolCallResult object.

Returns:

  • ToolCallResult – The created object.

ReasoningContent

Represents the optional reasoning content prepared by the model, usually contained in an assistant message.

Parameters:

  • reasoning_text (str) – The reasoning text produced by the model.
  • extra (dict[str, Any]) – Dictionary of extra information about the reasoning content. Use to store provider-specific information. To avoid serialization issues, values should be JSON serializable.

to_dict

to_dict() -> dict[str, Any]

Convert ReasoningContent into a dictionary.

Returns:

  • dict[str, Any] – A dictionary with keys 'reasoning_text', and 'extra'.

from_dict

from_dict(data: dict[str, Any]) -> ReasoningContent

Creates a new ReasoningContent object from a dictionary.

Parameters:

  • data (dict[str, Any]) – The dictionary to build the ReasoningContent object.

Returns:

  • ReasoningContent – The created object.

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.

role

role: ChatRole

Returns the role of the entity sending the message.

meta

meta: dict[str, Any]

Returns the metadata associated with the message.

name

name: str | None

Returns the name associated with the message.

texts

texts: list[str]

Returns the list of all texts contained in the message.

text

text: str | None

Returns the first text contained in the message.

tool_calls

tool_calls: list[ToolCall]

Returns the list of all Tool calls contained in the message.

tool_call

tool_call: ToolCall | None

Returns the first Tool call contained in the message.

tool_call_results

tool_call_results: list[ToolCallResult]

Returns the list of all Tool call results contained in the message.

tool_call_result

tool_call_result: ToolCallResult | None

Returns the first Tool call result contained in the message.

images

images: list[ImageContent]

Returns the list of all images contained in the message.

image

image: ImageContent | None

Returns the first image contained in the message.

files

files: list[FileContent]

Returns the list of all files contained in the message.

file

file: FileContent | None

Returns the first file contained in the message.

reasonings

reasonings: list[ReasoningContent]

Returns the list of all reasoning contents contained in the message.

reasoning

reasoning: ReasoningContent | None

Returns the first reasoning content contained in the message.

is_from

is_from(role: ChatRole | str) -> bool

Check if the message is from a specific role.

Parameters:

  • role (ChatRole | str) – The role to check against.

Returns:

  • bool – True if the message is from the specified role, False otherwise.

from_user

from_user(
    text: str | None = None,
    meta: dict[str, Any] | None = None,
    name: str | None = None,
    *,
    content_parts: (
        Sequence[TextContent | str | ImageContent | FileContent] | None
    ) = None
) -> ChatMessage

Create a message from the user.

Parameters:

  • text (str | None) – The text content of the message. Specify this or content_parts.
  • meta (dict[str, Any] | None) – Additional metadata associated with the message.
  • name (str | None) – An optional name for the participant. This field is only supported by OpenAI.
  • content_parts (Sequence[TextContent | str | ImageContent | FileContent] | None) – A list of content parts to include in the message. Specify this or text.

Returns:

  • ChatMessage – A new ChatMessage instance.

Raises:

  • ValueError – If neither or both of text and content_parts are provided, or if content_parts is empty.
  • TypeError – If a content part is not a str, TextContent, ImageContent, or FileContent.

from_system

from_system(
    text: str, meta: dict[str, Any] | None = None, name: str | None = None
) -> ChatMessage

Create a message from the system.

Parameters:

  • text (str) – The text content of the message.
  • meta (dict[str, Any] | None) – Additional metadata associated with the message.
  • name (str | None) – An optional name for the participant. This field is only supported by OpenAI.

Returns:

  • ChatMessage – A new ChatMessage instance.

from_assistant

from_assistant(
    text: str | None = None,
    meta: dict[str, Any] | None = None,
    name: str | None = None,
    tool_calls: list[ToolCall] | None = None,
    *,
    reasoning: str | ReasoningContent | None = None
) -> ChatMessage

Create a message from the assistant.

Parameters:

  • text (str | None) – The text content of the message.
  • meta (dict[str, Any] | None) – Additional metadata associated with the message.
  • name (str | None) – An optional name for the participant. This field is only supported by OpenAI.
  • tool_calls (list[ToolCall] | None) – The Tool calls to include in the message.
  • reasoning (str | ReasoningContent | None) – The reasoning content to include in the message.

Returns:

  • ChatMessage – A new ChatMessage instance.

Raises:

  • TypeError – If reasoning is not a string or ReasoningContent object.

from_tool

from_tool(
    tool_result: ToolCallResultContentT,
    origin: ToolCall,
    error: bool = False,
    meta: dict[str, Any] | None = None,
) -> ChatMessage

Create a message from a Tool.

Parameters:

  • tool_result (ToolCallResultContentT) – The result of the Tool invocation.
  • origin (ToolCall) – The Tool call that produced this result.
  • error (bool) – Whether the Tool invocation resulted in an error.
  • meta (dict[str, Any] | None) – Additional metadata associated with the message.

Returns:

  • ChatMessage – A new ChatMessage instance.

to_dict

to_dict() -> dict[str, Any]

Converts ChatMessage into a dictionary.

Returns:

  • dict[str, Any] – Serialized version of the object.

from_dict

from_dict(data: dict[str, Any]) -> ChatMessage

Creates a new ChatMessage object from a dictionary.

Parameters:

  • data (dict[str, Any]) – The dictionary to build the ChatMessage object.

Returns:

  • ChatMessage – The created object.

Raises:

  • ValueError – If the role field is missing from the dictionary.
  • TypeError – If the content field is not a list or string.

to_openai_dict_format

to_openai_dict_format(require_tool_call_ids: bool = True) -> dict[str, Any]

Convert a ChatMessage to the dictionary format expected by OpenAI's Chat Completions API.

Parameters:

  • require_tool_call_ids (bool) – If True (default), enforces that each Tool Call includes a non-null id attribute. Set to False to allow Tool Calls without id, which may be suitable for shallow OpenAI-compatible APIs.

Returns:

  • dict[str, Any] – The ChatMessage in the format expected by OpenAI's Chat Completions API.

Raises:

  • ValueError – If the message format is invalid, or if require_tool_call_ids is True and any Tool Call is missing an id attribute.

from_openai_dict_format

from_openai_dict_format(message: dict[str, Any]) -> ChatMessage

Create a ChatMessage from a dictionary in the format expected by OpenAI's Chat API.

NOTE: While OpenAI's API requires tool_call_id in both tool calls and tool messages, this method accepts messages without it to support shallow OpenAI-compatible APIs. If you plan to use the resulting ChatMessage with OpenAI, you must include tool_call_id or you'll encounter validation errors.

Parameters:

  • message (dict[str, Any]) – The OpenAI dictionary to build the ChatMessage object.

Returns:

  • ChatMessage – The created ChatMessage object.

Raises:

  • ValueError – If the message dictionary is missing required fields.

document

Document

Base data class containing some data to be queried.

Can contain text snippets and file paths to images or audios. Documents can be sorted by score and saved to/from dictionary and JSON.

Parameters:

  • id (str) – Unique identifier for the document. When not set, it's generated based on the Document fields' values.
  • content (str | None) – Text of the document, if the document contains text.
  • blob (ByteStream | None) – Binary data associated with the document, if the document has any binary data associated with it.
  • meta (dict[str, Any]) – Additional custom metadata for the document. Must be JSON-serializable.
  • score (float | None) – Score of the document. Used for ranking, usually assigned by retrievers.
  • embedding (list[float] | None) – dense vector representation of the document.
  • sparse_embedding (SparseEmbedding | None) – sparse vector representation of the document.

to_dict

to_dict(flatten: bool = True) -> dict[str, Any]

Converts Document into a dictionary.

blob field is converted to a JSON-serializable type.

Parameters:

  • flatten (bool) – Whether to flatten meta field or not. Defaults to True to be backward-compatible with Haystack 1.x.

from_dict

from_dict(data: dict[str, Any]) -> Document

Creates a new Document object from a dictionary.

The blob field is converted to its original type.

content_type

content_type: str

Returns the type of the content for the document.

This is necessary to keep backward compatibility with 1.x.

file_content

FileContent

The file content of a chat message.

Parameters:

  • base64_data (str) – A base64 string representing the file.
  • mime_type (str | None) – The MIME type of the file (e.g. "application/pdf"). 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.
  • filename (str | None) – Optional filename of the file. Some LLM providers use this information.
  • extra (dict[str, Any]) – Dictionary of extra information about the file. Can be used to store provider-specific information. To avoid serialization issues, values should be JSON serializable.
  • validation (bool) – If True (default), a validation process is performed:
  • Check whether the base64 string is valid;
  • Guess the MIME type if not provided. Set to False to skip validation and speed up initialization.

to_dict

to_dict() -> dict[str, Any]

Convert FileContent into a dictionary.

from_dict

from_dict(data: dict[str, Any]) -> FileContent

Create an FileContent from a dictionary.

from_file_path

from_file_path(
    file_path: str | Path,
    *,
    filename: str | None = None,
    extra: dict[str, Any] | None = None
) -> FileContent

Create an FileContent object from a file path.

Parameters:

  • file_path (str | Path) – The path to the file.
  • filename (str | None) – Optional file name. Some LLM providers use this information. If not provided, the filename is extracted from the file path.
  • extra (dict[str, Any] | None) – Dictionary of extra information about the file. Can be used to store provider-specific information. To avoid serialization issues, values should be JSON serializable.

Returns:

  • FileContent – An FileContent object.

from_url

from_url(
    url: str,
    *,
    retry_attempts: int = 2,
    timeout: int = 10,
    filename: str | None = None,
    extra: dict[str, Any] | None = None
) -> FileContent

Create an FileContent object from a URL. The file is downloaded and converted to a base64 string.

Parameters:

  • url (str) – The URL of the file.
  • retry_attempts (int) – The number of times to retry to fetch the URL's content.
  • timeout (int) – Timeout in seconds for the request.
  • filename (str | None) – Optional filename of the file. Some LLM providers use this information. If not provided, the filename is extracted from the URL.
  • extra (dict[str, Any] | None) – Dictionary of extra information about the file. Can be used to store provider-specific information. To avoid serialization issues, values should be JSON serializable.

Returns:

  • FileContent – An FileContent object.

image_content

ImageContent

The image content of a chat message.

Parameters:

  • base64_image (str) – A base64 string representing the image.
  • mime_type (str | None) – 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 (Literal['auto', 'high', 'low'] | None) – Optional detail level of the image (only supported by OpenAI). One of "auto", "high", or "low".
  • meta (dict[str, Any]) – Optional metadata for the image.
  • validation (bool) – 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.

show

show() -> None

Shows the image.

to_dict

to_dict() -> dict[str, Any]

Convert ImageContent into a dictionary.

from_dict

from_dict(data: dict[str, Any]) -> ImageContent

Create an ImageContent from a dictionary.

from_file_path

from_file_path(
    file_path: str | Path,
    *,
    size: tuple[int, int] | None = None,
    detail: Literal["auto", "high", "low"] | None = None,
    meta: dict[str, Any] | None = 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.

Parameters:

  • file_path (str | Path) – The path to the image file. PDF files are not supported. For PDF to ImageContent conversion, use the PDFToImageContent component.
  • size (tuple[int, int] | None) – 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 (Literal['auto', 'high', 'low'] | None) – Optional detail level of the image (only supported by OpenAI). One of "auto", "high", or "low".
  • meta (dict[str, Any] | None) – Additional metadata for the image.

Returns:

  • ImageContent – An ImageContent object.

from_url

from_url(
    url: str,
    *,
    retry_attempts: int = 2,
    timeout: int = 10,
    size: tuple[int, int] | None = None,
    detail: Literal["auto", "high", "low"] | None = None,
    meta: dict[str, Any] | None = 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.

Parameters:

  • url (str) – The URL of the image. PDF files are not supported. For PDF to ImageContent conversion, use the PDFToImageContent component.
  • retry_attempts (int) – The number of times to retry to fetch the URL's content.
  • timeout (int) – Timeout in seconds for the request.
  • size (tuple[int, int] | None) – 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 (Literal['auto', 'high', 'low'] | None) – Optional detail level of the image (only supported by OpenAI). One of "auto", "high", or "low".
  • meta (dict[str, Any] | None) – Additional metadata for the image.

Returns:

  • ImageContent – An ImageContent object.

Raises:

  • ValueError – If the URL does not point to an image or if it points to a PDF file.

sparse_embedding

SparseEmbedding

Class representing a sparse embedding.

Parameters:

  • indices (list[int]) – List of indices of non-zero elements in the embedding.
  • values (list[float]) – List of values of non-zero elements in the embedding.

to_dict

to_dict() -> dict[str, Any]

Convert the SparseEmbedding object to a dictionary.

Returns:

  • dict[str, Any] – Serialized sparse embedding.

from_dict

from_dict(sparse_embedding_dict: dict[str, Any]) -> SparseEmbedding

Deserializes the sparse embedding from a dictionary.

Parameters:

  • sparse_embedding_dict (dict[str, Any]) – Dictionary to deserialize from.

Returns:

  • SparseEmbedding – Deserialized sparse embedding.

streaming_chunk

ToolCallDelta

Represents a Tool call prepared by the model, usually contained in an assistant message.

Parameters:

  • index (int) – The index of the Tool call in the list of Tool calls.
  • tool_name (str | None) – The name of the Tool to call.
  • arguments (str | None) – Either the full arguments in JSON format or a delta of the arguments.
  • id (str | None) – The ID of the Tool call.
  • extra (dict[str, Any] | None) – Dictionary of extra information about the Tool call. Use to store provider-specific information. To avoid serialization issues, values should be JSON serializable.

to_dict

to_dict() -> dict[str, Any]

Returns a dictionary representation of the ToolCallDelta.

Returns:

  • dict[str, Any] – A dictionary with keys 'index', 'tool_name', 'arguments', 'id', and 'extra'.

from_dict

from_dict(data: dict[str, Any]) -> ToolCallDelta

Creates a ToolCallDelta from a serialized representation.

Parameters:

  • data (dict[str, Any]) – Dictionary containing ToolCallDelta's attributes.

Returns:

  • ToolCallDelta – A ToolCallDelta instance.

ComponentInfo

The ComponentInfo class encapsulates information about a component.

Parameters:

  • type (str) – The type of the component.
  • name (str | None) – The name of the component assigned when adding it to a pipeline.

from_component

from_component(component: Component) -> ComponentInfo

Create a ComponentInfo object from a Component instance.

Parameters:

  • component (Component) – The Component instance.

Returns:

  • ComponentInfo – The ComponentInfo object with the type and name of the given component.

to_dict

to_dict() -> dict[str, Any]

Returns a dictionary representation of ComponentInfo.

Returns:

  • dict[str, Any] – A dictionary with keys 'type' and 'name'.

from_dict

from_dict(data: dict[str, Any]) -> ComponentInfo

Creates a ComponentInfo from a serialized representation.

Parameters:

  • data (dict[str, Any]) – Dictionary containing ComponentInfo's attributes.

Returns:

  • ComponentInfo – A ComponentInfo instance.

StreamingChunk

The StreamingChunk class encapsulates a segment of streamed content along with associated metadata.

This structure facilitates the handling and processing of streamed data in a systematic manner.

Parameters:

  • content (str) – The content of the message chunk as a string.
  • meta (dict[str, Any]) – A dictionary containing metadata related to the message chunk.
  • component_info (ComponentInfo | None) – A ComponentInfo object containing information about the component that generated the chunk, such as the component name and type.
  • index (int | None) – An optional integer index representing which content block this chunk belongs to.
  • tool_calls (list[ToolCallDelta] | None) – An optional list of ToolCallDelta object representing a tool call associated with the message chunk.
  • tool_call_result (ToolCallResult | None) – An optional ToolCallResult object representing the result of a tool call.
  • start (bool) – A boolean indicating whether this chunk marks the start of a content block.
  • finish_reason (FinishReason | None) – An optional value indicating the reason the generation finished. Standard values follow OpenAI's convention: "stop", "length", "tool_calls", "content_filter", plus Haystack-specific value "tool_call_results".
  • reasoning (ReasoningContent | None) – An optional ReasoningContent object representing the reasoning content associated with the message chunk.

to_dict

to_dict() -> dict[str, Any]

Returns a dictionary representation of the StreamingChunk.

Returns:

  • dict[str, Any] – Serialized dictionary representation of the calling object.

from_dict

from_dict(data: dict[str, Any]) -> StreamingChunk

Creates a deserialized StreamingChunk instance from a serialized representation.

Parameters:

  • data (dict[str, Any]) – Dictionary containing the StreamingChunk's attributes.

Returns:

  • StreamingChunk – A StreamingChunk instance.

select_streaming_callback

select_streaming_callback(
    init_callback: StreamingCallbackT | None,
    runtime_callback: StreamingCallbackT | None,
    requires_async: bool,
) -> StreamingCallbackT | None

Picks the correct streaming callback given an optional initial and runtime callback.

The runtime callback takes precedence over the initial callback.

Parameters:

  • init_callback (StreamingCallbackT | None) – The initial callback.
  • runtime_callback (StreamingCallbackT | None) – The runtime callback.
  • requires_async (bool) – Whether the selected callback must be async compatible.

Returns:

  • StreamingCallbackT | None – The selected callback.