DocumentationAPI Reference📓 Tutorials🧑‍🍳 Cookbook🤝 Integrations💜 Discord🎨 Studio (Waitlist)
API Reference

Core classes that carry data through the system.

Module haystack_experimental.dataclasses.chat_message

ChatRole

Enumeration representing the roles within a chat.

USER

The user role. A message from the user contains only text.

SYSTEM

The system role. A message from the system contains only text.

ASSISTANT

The assistant role. A message from the assistant can contain text and Tool calls. It can also store metadata.

TOOL

The tool role. A message from a tool contains the result of a Tool invocation.

ToolCall

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

Arguments:

  • id: The ID of the Tool call.
  • tool_name: The name of the Tool to call.
  • arguments: The arguments to call the Tool with.

id

noqa: A003

ToolCallResult

Represents the result of a Tool invocation.

Arguments:

  • result: The result of the Tool invocation.
  • origin: The Tool call that produced this result.
  • error: Whether the Tool invocation resulted in an error.

TextContent

The textual content of a chat message.

Arguments:

  • text: The text content of the message.

ChatMessage

Represents a message in a LLM chat conversation.

Arguments:

  • content: The content of the message.
  • role: The role of the entity sending the message.
  • meta: Additional metadata associated with the message.

ChatMessage.role

@property
def role() -> ChatRole

Returns the role of the entity sending the message.

ChatMessage.meta

@property
def meta() -> Dict[str, Any]

Returns the metadata associated with the message.

ChatMessage.texts

@property
def texts() -> List[str]

Returns the list of all texts contained in the message.

ChatMessage.text

@property
def text() -> Optional[str]

Returns the first text contained in the message.

ChatMessage.tool_calls

@property
def tool_calls() -> List[ToolCall]

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

ChatMessage.tool_call

@property
def tool_call() -> Optional[ToolCall]

Returns the first Tool call contained in the message.

ChatMessage.tool_call_results

@property
def tool_call_results() -> List[ToolCallResult]

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

ChatMessage.tool_call_result

@property
def tool_call_result() -> Optional[ToolCallResult]

Returns the first Tool call result contained in the message.

ChatMessage.is_from

def is_from(role: ChatRole) -> bool

Check if the message is from a specific role.

Arguments:

  • role: The role to check against.

Returns:

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

ChatMessage.from_user

@classmethod
def from_user(cls, text: str) -> "ChatMessage"

Create a message from the user.

Arguments:

  • text: The text content of the message.

Returns:

A new ChatMessage instance.

ChatMessage.from_system

@classmethod
def from_system(cls, text: str) -> "ChatMessage"

Create a message from the system.

Arguments:

  • text: The text content of the message.

Returns:

A new ChatMessage instance.

ChatMessage.from_assistant

@classmethod
def from_assistant(cls,
                   text: Optional[str] = None,
                   tool_calls: Optional[List[ToolCall]] = None,
                   meta: Optional[Dict[str, Any]] = None) -> "ChatMessage"

Create a message from the assistant.

Arguments:

  • text: The text content of the message.
  • tool_calls: The Tool calls to include in the message.
  • meta: Additional metadata associated with the message.

Returns:

A new ChatMessage instance.

ChatMessage.from_tool

@classmethod
def from_tool(cls,
              tool_result: str,
              origin: ToolCall,
              error: bool = False) -> "ChatMessage"

Create a message from a Tool.

Arguments:

  • tool_result: The result of the Tool invocation.
  • origin: The Tool call that produced this result.
  • error: Whether the Tool invocation resulted in an error.

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.from_dict

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "ChatMessage"

Creates a new ChatMessage object from a dictionary.

Arguments:

  • data: The dictionary to build the ChatMessage object.

Returns:

The created object.

Module haystack_experimental.dataclasses.tool

ToolInvocationError

Exception raised when a Tool invocation fails.

Tool

Data class representing a tool for which Language Models can prepare a call.

Accurate definitions of the textual attributes such as name and description are important for the Language Model to correctly prepare the call.

Arguments:

  • name: Name of the tool.
  • description: Description of the tool.
  • parameters: A JSON schema defining the parameters expected by the tool.
  • function: The callable that will be invoked when the tool is called.

Tool.tool_spec

@property
def tool_spec() -> Dict[str, Any]

Return the tool specification to be used by the Language Model.

Tool.invoke

def invoke(**kwargs) -> Any

Invoke the tool with the provided keyword arguments.

Tool.to_dict

def to_dict() -> Dict[str, Any]

Convert the Tool to a dictionary.

Returns:

Serialized version of the Tool.

Tool.from_dict

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "Tool"

Create a Tool from a dictionary.

Arguments:

  • data: The serialized version of the Tool.

Returns:

The deserialized Tool.

deserialize_tools_inplace

def deserialize_tools_inplace(data: Dict[str, Any], key: str = "tools")

Deserialize tools in a dictionary inplace.

Arguments:

  • data: The dictionary with the serialized data.
  • key: The key in the dictionary where the tools are stored.