chore: import upstream snapshot with attribution
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
Docker image release / Build base image (push) Has been cancelled
Sync docs with Docusaurus / sync (push) Has been cancelled
Tests / Check if changed (push) Has been cancelled
Tests / format (push) Has been cancelled
Tests / check-imports (push) Has been cancelled
Tests / Unit / macos-latest (push) Has been cancelled
Tests / Unit / ubuntu-latest (push) Has been cancelled
Tests / Unit / windows-latest (push) Has been cancelled
Tests / mypy (push) Has been cancelled
Tests / Integration / ubuntu-latest (push) Has been cancelled
Tests / Integration / macos-latest (push) Has been cancelled
Tests / Integration / windows-latest (push) Has been cancelled
Tests / notify-slack-on-failure (push) Has been cancelled
Tests / Mark tests as completed (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 13:22:28 +08:00
commit c56bef871b
9296 changed files with 1854228 additions and 0 deletions
@@ -0,0 +1,475 @@
---
title: "Agents"
id: experimental-agents-api
description: "Tool-using agents with provider-agnostic chat model support."
slug: "/experimental-agents-api"
---
<a id="haystack_experimental.components.agents.agent"></a>
## Module haystack\_experimental.components.agents.agent
<a id="haystack_experimental.components.agents.agent.Agent"></a>
### Agent
A Haystack component that implements a tool-using agent with provider-agnostic chat model support.
NOTE: This class extends Haystack's Agent component to add support for human-in-the-loop confirmation strategies.
The component processes messages and executes tools until an exit condition is met.
The exit condition can be triggered either by a direct text response or by invoking a specific designated tool.
Multiple exit conditions can be specified.
When you call an Agent without tools, it acts as a ChatGenerator, produces one response, then exits.
### Usage example
```python
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.tools.tool import Tool
from haystack_experimental.components.agents import Agent
from haystack_experimental.components.agents.human_in_the_loop import (
HumanInTheLoopStrategy,
AlwaysAskPolicy,
NeverAskPolicy,
SimpleConsoleUI,
)
calculator_tool = Tool(name="calculator", description="A tool for performing mathematical calculations.", ...)
search_tool = Tool(name="search", description="A tool for searching the web.", ...)
agent = Agent(
chat_generator=OpenAIChatGenerator(),
tools=[calculator_tool, search_tool],
confirmation_strategies={
calculator_tool.name: HumanInTheLoopStrategy(
confirmation_policy=NeverAskPolicy(), confirmation_ui=SimpleConsoleUI()
),
search_tool.name: HumanInTheLoopStrategy(
confirmation_policy=AlwaysAskPolicy(), confirmation_ui=SimpleConsoleUI()
),
},
)
# Run the agent
result = agent.run(
messages=[ChatMessage.from_user("Find information about Haystack")]
)
assert "messages" in result # Contains conversation history
```
<a id="haystack_experimental.components.agents.agent.Agent.__init__"></a>
#### Agent.\_\_init\_\_
```python
def __init__(*,
chat_generator: ChatGenerator,
tools: ToolsType | None = None,
system_prompt: str | None = None,
exit_conditions: list[str] | None = None,
state_schema: dict[str, Any] | None = None,
max_agent_steps: int = 100,
streaming_callback: StreamingCallbackT | None = None,
raise_on_tool_invocation_failure: bool = False,
confirmation_strategies: dict[str, ConfirmationStrategy]
| None = None,
tool_invoker_kwargs: dict[str, Any] | None = None,
chat_message_store: ChatMessageStore | None = None,
memory_store: MemoryStore | None = None) -> None
```
Initialize the agent component.
**Arguments**:
- `chat_generator`: An instance of the chat generator that your agent should use. It must support tools.
- `tools`: List of Tool objects or a Toolset that the agent can use.
- `system_prompt`: System prompt for the agent.
- `exit_conditions`: List of conditions that will cause the agent to return.
Can include "text" if the agent should return when it generates a message without tool calls,
or tool names that will cause the agent to return once the tool was executed. Defaults to ["text"].
- `state_schema`: The schema for the runtime state used by the tools.
- `max_agent_steps`: Maximum number of steps the agent will run before stopping. Defaults to 100.
If the agent exceeds this number of steps, it will stop and return the current state.
- `streaming_callback`: A callback that will be invoked when a response is streamed from the LLM.
The same callback can be configured to emit tool results when a tool is called.
- `raise_on_tool_invocation_failure`: Should the agent raise an exception when a tool invocation fails?
If set to False, the exception will be turned into a chat message and passed to the LLM.
- `tool_invoker_kwargs`: Additional keyword arguments to pass to the ToolInvoker.
- `chat_message_store`: The ChatMessageStore that the agent can use to store
and retrieve chat messages history.
- `memory_store`: The memory store that the agent can use to store and retrieve memories.
**Raises**:
- `TypeError`: If the chat_generator does not support tools parameter in its run method.
- `ValueError`: If the exit_conditions are not valid.
<a id="haystack_experimental.components.agents.agent.Agent.run"></a>
#### Agent.run
```python
def run(messages: list[ChatMessage],
streaming_callback: StreamingCallbackT | None = None,
*,
generation_kwargs: dict[str, Any] | None = None,
break_point: AgentBreakpoint | None = None,
snapshot: AgentSnapshot | None = None,
system_prompt: str | None = None,
tools: ToolsType | list[str] | None = None,
confirmation_strategy_context: dict[str, Any] | None = None,
chat_message_store_kwargs: dict[str, Any] | None = None,
memory_store_kwargs: dict[str, Any] | None = None,
**kwargs: Any) -> dict[str, Any]
```
Process messages and execute tools until an exit condition is met.
**Arguments**:
- `messages`: List of Haystack ChatMessage objects to process.
- `streaming_callback`: A callback that will be invoked when a response is streamed from the LLM.
The same callback can be configured to emit tool results when a tool is called.
- `generation_kwargs`: Additional keyword arguments for LLM. These parameters will
override the parameters passed during component initialization.
- `break_point`: An AgentBreakpoint, can be a Breakpoint for the "chat_generator" or a ToolBreakpoint
for "tool_invoker".
- `snapshot`: A dictionary containing a snapshot of a previously saved agent execution. The snapshot contains
the relevant information to restart the Agent execution from where it left off.
- `system_prompt`: System prompt for the agent. If provided, it overrides the default system prompt.
- `tools`: Optional list of Tool objects, a Toolset, or list of tool names to use for this run.
When passing tool names, tools are selected from the Agent's originally configured tools.
- `confirmation_strategy_context`: Optional dictionary for passing request-scoped resources
to confirmation strategies. Useful in web/server environments to provide per-request
objects (e.g., WebSocket connections, async queues, Redis pub/sub clients) that strategies
can use for non-blocking user interaction.
- `chat_message_store_kwargs`: Optional dictionary of keyword arguments to pass to the ChatMessageStore.
For example, it can include the `chat_history_id` and `last_k` parameters for retrieving chat history.
- `memory_store_kwargs`: Optional dictionary of keyword arguments to pass to the MemoryStore.
It can include:
- `user_id`: The user ID to search and add memories from.
- `run_id`: The run ID to search and add memories from.
- `agent_id`: The agent ID to search and add memories from.
- `search_criteria`: A dictionary of containing kwargs for the `search_memories` method.
This can include:
- `filters`: A dictionary of filters to search for memories.
- `query`: The query to search for memories.
Note: If you pass this, the user query passed to the agent will be
ignored for memory retrieval.
- `top_k`: The number of memories to return.
- `include_memory_metadata`: Whether to include the memory metadata in the ChatMessage.
- `kwargs`: Additional data to pass to the State schema used by the Agent.
The keys must match the schema defined in the Agent's `state_schema`.
**Raises**:
- `RuntimeError`: If the Agent component wasn't warmed up before calling `run()`.
- `BreakpointException`: If an agent breakpoint is triggered.
**Returns**:
A dictionary with the following keys:
- "messages": List of all messages exchanged during the agent's run.
- "last_message": The last message exchanged during the agent's run.
- Any additional keys defined in the `state_schema`.
<a id="haystack_experimental.components.agents.agent.Agent.run_async"></a>
#### Agent.run\_async
```python
async def run_async(messages: list[ChatMessage],
streaming_callback: StreamingCallbackT | None = None,
*,
generation_kwargs: dict[str, Any] | None = None,
break_point: AgentBreakpoint | None = None,
snapshot: AgentSnapshot | None = None,
system_prompt: str | None = None,
tools: ToolsType | list[str] | None = None,
confirmation_strategy_context: dict[str, Any]
| None = None,
chat_message_store_kwargs: dict[str, Any] | None = None,
memory_store_kwargs: dict[str, Any] | None = None,
**kwargs: Any) -> dict[str, Any]
```
Asynchronously process messages and execute tools until the exit condition is met.
This is the asynchronous version of the `run` method. It follows the same logic but uses
asynchronous operations where possible, such as calling the `run_async` method of the ChatGenerator
if available.
**Arguments**:
- `messages`: List of Haystack ChatMessage objects to process.
- `streaming_callback`: An asynchronous callback that will be invoked when a response is streamed from the
LLM. The same callback can be configured to emit tool results when a tool is called.
- `generation_kwargs`: Additional keyword arguments for LLM. These parameters will
override the parameters passed during component initialization.
- `break_point`: An AgentBreakpoint, can be a Breakpoint for the "chat_generator" or a ToolBreakpoint
for "tool_invoker".
- `snapshot`: A dictionary containing a snapshot of a previously saved agent execution. The snapshot contains
the relevant information to restart the Agent execution from where it left off.
- `system_prompt`: System prompt for the agent. If provided, it overrides the default system prompt.
- `tools`: Optional list of Tool objects, a Toolset, or list of tool names to use for this run.
- `confirmation_strategy_context`: Optional dictionary for passing request-scoped resources
to confirmation strategies. Useful in web/server environments to provide per-request
objects (e.g., WebSocket connections, async queues, Redis pub/sub clients) that strategies
can use for non-blocking user interaction.
- `chat_message_store_kwargs`: Optional dictionary of keyword arguments to pass to the ChatMessageStore.
For example, it can include the `chat_history_id` and `last_k` parameters for retrieving chat history.
- `kwargs`: Additional data to pass to the State schema used by the Agent.
- `memory_store_kwargs`: Optional dictionary of keyword arguments to pass to the MemoryStore.
It can include:
- `user_id`: The user ID to search and add memories from.
- `run_id`: The run ID to search and add memories from.
- `agent_id`: The agent ID to search and add memories from.
- `search_criteria`: A dictionary of containing kwargs for the `search_memories` method.
This can include:
- `filters`: A dictionary of filters to search for memories.
- `query`: The query to search for memories.
Note: If you pass this, the user query passed to the agent will be
ignored for memory retrieval.
- `top_k`: The number of memories to return.
- `include_memory_metadata`: Whether to include the memory metadata in the ChatMessage.
- `kwargs`: Additional data to pass to the State schema used by the Agent.
The keys must match the schema defined in the Agent's `state_schema`.
**Raises**:
- `RuntimeError`: If the Agent component wasn't warmed up before calling `run_async()`.
- `BreakpointException`: If an agent breakpoint is triggered.
**Returns**:
A dictionary with the following keys:
- "messages": List of all messages exchanged during the agent's run.
- "last_message": The last message exchanged during the agent's run.
- Any additional keys defined in the `state_schema`.
<a id="haystack_experimental.components.agents.agent.Agent.to_dict"></a>
#### Agent.to\_dict
```python
def to_dict() -> dict[str, Any]
```
Serialize the component to a dictionary.
**Returns**:
Dictionary with serialized data
<a id="haystack_experimental.components.agents.agent.Agent.from_dict"></a>
#### Agent.from\_dict
```python
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "Agent"
```
Deserialize the agent from a dictionary.
**Arguments**:
- `data`: Dictionary to deserialize from
**Returns**:
Deserialized agent
<a id="haystack_experimental.components.agents.human_in_the_loop.breakpoint"></a>
## Module haystack\_experimental.components.agents.human\_in\_the\_loop.breakpoint
<a id="haystack_experimental.components.agents.human_in_the_loop.breakpoint.get_tool_calls_and_descriptions_from_snapshot"></a>
#### get\_tool\_calls\_and\_descriptions\_from\_snapshot
```python
def get_tool_calls_and_descriptions_from_snapshot(
agent_snapshot: AgentSnapshot,
breakpoint_tool_only: bool = True
) -> tuple[list[dict], dict[str, str]]
```
Extract tool calls and tool descriptions from an AgentSnapshot.
By default, only the tool call that caused the breakpoint is processed and its arguments are reconstructed.
This is useful for scenarios where you want to present the relevant tool call and its description
to a human for confirmation before execution.
**Arguments**:
- `agent_snapshot`: The AgentSnapshot from which to extract tool calls and descriptions.
- `breakpoint_tool_only`: If True, only the tool call that caused the breakpoint is returned. If False, all tool
calls are returned.
**Returns**:
A tuple containing a list of tool call dictionaries and a dictionary of tool descriptions
<a id="haystack_experimental.components.agents.human_in_the_loop.errors"></a>
## Module haystack\_experimental.components.agents.human\_in\_the\_loop.errors
<a id="haystack_experimental.components.agents.human_in_the_loop.errors.HITLBreakpointException"></a>
### HITLBreakpointException
Exception raised when a tool execution is paused by a ConfirmationStrategy (e.g. BreakpointConfirmationStrategy).
<a id="haystack_experimental.components.agents.human_in_the_loop.errors.HITLBreakpointException.__init__"></a>
#### HITLBreakpointException.\_\_init\_\_
```python
def __init__(message: str,
tool_name: str,
snapshot_file_path: str,
tool_call_id: str | None = None) -> None
```
Initialize the HITLBreakpointException.
**Arguments**:
- `message`: The exception message.
- `tool_name`: The name of the tool whose execution is paused.
- `snapshot_file_path`: The file path to the saved pipeline snapshot.
- `tool_call_id`: Optional unique identifier for the tool call. This can be used to track and correlate
the decision with a specific tool invocation.
<a id="haystack_experimental.components.agents.human_in_the_loop.strategies"></a>
## Module haystack\_experimental.components.agents.human\_in\_the\_loop.strategies
<a id="haystack_experimental.components.agents.human_in_the_loop.strategies.BreakpointConfirmationStrategy"></a>
### BreakpointConfirmationStrategy
Confirmation strategy that raises a tool breakpoint exception to pause execution and gather user feedback.
This strategy is designed for scenarios where immediate user interaction is not possible.
When a tool execution requires confirmation, it raises an `HITLBreakpointException`, which is caught by the Agent.
The Agent then serialize its current state, including the tool call details. This information can then be used to
notify a user to review and confirm the tool execution.
<a id="haystack_experimental.components.agents.human_in_the_loop.strategies.BreakpointConfirmationStrategy.__init__"></a>
#### BreakpointConfirmationStrategy.\_\_init\_\_
```python
def __init__(snapshot_file_path: str) -> None
```
Initialize the BreakpointConfirmationStrategy.
**Arguments**:
- `snapshot_file_path`: The path to the directory that the snapshot should be saved.
<a id="haystack_experimental.components.agents.human_in_the_loop.strategies.BreakpointConfirmationStrategy.run"></a>
#### BreakpointConfirmationStrategy.run
```python
def run(
*,
tool_name: str,
tool_description: str,
tool_params: dict[str, Any],
tool_call_id: str | None = None,
confirmation_strategy_context: dict[str, Any] | None = None
) -> ToolExecutionDecision
```
Run the breakpoint confirmation strategy for a given tool and its parameters.
**Arguments**:
- `tool_name`: The name of the tool to be executed.
- `tool_description`: The description of the tool.
- `tool_params`: The parameters to be passed to the tool.
- `tool_call_id`: Optional unique identifier for the tool call. This can be used to track and correlate the decision with a
specific tool invocation.
- `confirmation_strategy_context`: Optional dictionary for passing request-scoped resources. Not used by this strategy but included for
interface compatibility.
**Raises**:
- `HITLBreakpointException`: Always raises an `HITLBreakpointException` exception to signal that user confirmation is required.
**Returns**:
This method does not return; it always raises an exception.
<a id="haystack_experimental.components.agents.human_in_the_loop.strategies.BreakpointConfirmationStrategy.run_async"></a>
#### BreakpointConfirmationStrategy.run\_async
```python
async def run_async(
*,
tool_name: str,
tool_description: str,
tool_params: dict[str, Any],
tool_call_id: str | None = None,
confirmation_strategy_context: dict[str, Any] | None = None
) -> ToolExecutionDecision
```
Async version of run. Calls the sync run() method.
**Arguments**:
- `tool_name`: The name of the tool to be executed.
- `tool_description`: The description of the tool.
- `tool_params`: The parameters to be passed to the tool.
- `tool_call_id`: Optional unique identifier for the tool call.
- `confirmation_strategy_context`: Optional dictionary for passing request-scoped resources.
**Raises**:
- `HITLBreakpointException`: Always raises an `HITLBreakpointException` exception to signal that user confirmation is required.
**Returns**:
This method does not return; it always raises an exception.
<a id="haystack_experimental.components.agents.human_in_the_loop.strategies.BreakpointConfirmationStrategy.to_dict"></a>
#### BreakpointConfirmationStrategy.to\_dict
```python
def to_dict() -> dict[str, Any]
```
Serializes the BreakpointConfirmationStrategy to a dictionary.
<a id="haystack_experimental.components.agents.human_in_the_loop.strategies.BreakpointConfirmationStrategy.from_dict"></a>
#### BreakpointConfirmationStrategy.from\_dict
```python
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "BreakpointConfirmationStrategy"
```
Deserializes the BreakpointConfirmationStrategy from a dictionary.
**Arguments**:
- `data`: Dictionary to deserialize from.
**Returns**:
Deserialized BreakpointConfirmationStrategy.
@@ -0,0 +1,181 @@
---
title: "ChatMessage Store"
id: experimental-chatmessage-store-api
description: "Storage for the chat messages."
slug: "/experimental-chatmessage-store-api"
---
<a id="haystack_experimental.chat_message_stores.in_memory"></a>
## Module haystack\_experimental.chat\_message\_stores.in\_memory
<a id="haystack_experimental.chat_message_stores.in_memory.InMemoryChatMessageStore"></a>
### InMemoryChatMessageStore
Stores chat messages in-memory.
The `chat_history_id` parameter is used as a unique identifier for each conversation or chat session.
It acts as a namespace that isolates messages from different sessions. Each `chat_history_id` value corresponds to a
separate list of `ChatMessage` objects stored in memory.
Typical usage involves providing a unique `chat_history_id` (for example, a session ID or conversation ID)
whenever you write, read, or delete messages. This ensures that chat messages from different
conversations do not overlap.
Usage example:
```python
from haystack.dataclasses import ChatMessage
from haystack_experimental.chat_message_stores.in_memory import InMemoryChatMessageStore
message_store = InMemoryChatMessageStore()
messages = [
ChatMessage.from_assistant("Hello, how can I help you?"),
ChatMessage.from_user("Hi, I have a question about Python. What is a Protocol?"),
]
message_store.write_messages(chat_history_id="user_456_session_123", messages=messages)
retrieved_messages = message_store.retrieve_messages(chat_history_id="user_456_session_123")
print(retrieved_messages)
```
<a id="haystack_experimental.chat_message_stores.in_memory.InMemoryChatMessageStore.__init__"></a>
#### InMemoryChatMessageStore.\_\_init\_\_
```python
def __init__(skip_system_messages: bool = True,
last_k: int | None = 10) -> None
```
Create an InMemoryChatMessageStore.
**Arguments**:
- `skip_system_messages`: Whether to skip storing system messages. Defaults to True.
- `last_k`: The number of last messages to retrieve. Defaults to 10 messages if not specified.
<a id="haystack_experimental.chat_message_stores.in_memory.InMemoryChatMessageStore.to_dict"></a>
#### InMemoryChatMessageStore.to\_dict
```python
def to_dict() -> dict[str, Any]
```
Serializes the component to a dictionary.
**Returns**:
Dictionary with serialized data.
<a id="haystack_experimental.chat_message_stores.in_memory.InMemoryChatMessageStore.from_dict"></a>
#### InMemoryChatMessageStore.from\_dict
```python
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "InMemoryChatMessageStore"
```
Deserializes the component from a dictionary.
**Arguments**:
- `data`: The dictionary to deserialize from.
**Returns**:
The deserialized component.
<a id="haystack_experimental.chat_message_stores.in_memory.InMemoryChatMessageStore.count_messages"></a>
#### InMemoryChatMessageStore.count\_messages
```python
def count_messages(chat_history_id: str) -> int
```
Returns the number of chat messages stored in this store.
**Arguments**:
- `chat_history_id`: The chat history id for which to count messages.
**Returns**:
The number of messages.
<a id="haystack_experimental.chat_message_stores.in_memory.InMemoryChatMessageStore.write_messages"></a>
#### InMemoryChatMessageStore.write\_messages
```python
def write_messages(chat_history_id: str, messages: list[ChatMessage]) -> int
```
Writes chat messages to the ChatMessageStore.
**Arguments**:
- `chat_history_id`: The chat history id under which to store the messages.
- `messages`: A list of ChatMessages to write.
**Raises**:
- `ValueError`: If messages is not a list of ChatMessages.
**Returns**:
The number of messages written.
<a id="haystack_experimental.chat_message_stores.in_memory.InMemoryChatMessageStore.retrieve_messages"></a>
#### InMemoryChatMessageStore.retrieve\_messages
```python
def retrieve_messages(chat_history_id: str,
last_k: int | None = None) -> list[ChatMessage]
```
Retrieves all stored chat messages.
**Arguments**:
- `chat_history_id`: The chat history id from which to retrieve messages.
- `last_k`: The number of last messages to retrieve. If unspecified, the last_k parameter passed
to the constructor will be used.
**Raises**:
- `ValueError`: If last_k is not None and is less than 0.
**Returns**:
A list of chat messages.
<a id="haystack_experimental.chat_message_stores.in_memory.InMemoryChatMessageStore.delete_messages"></a>
#### InMemoryChatMessageStore.delete\_messages
```python
def delete_messages(chat_history_id: str) -> None
```
Deletes all stored chat messages.
**Arguments**:
- `chat_history_id`: The chat history id from which to delete messages.
<a id="haystack_experimental.chat_message_stores.in_memory.InMemoryChatMessageStore.delete_all_messages"></a>
#### InMemoryChatMessageStore.delete\_all\_messages
```python
def delete_all_messages() -> None
```
Deletes all stored chat messages from all chat history ids.
@@ -0,0 +1,152 @@
---
title: "Generators"
id: experimental-generators-api
description: "Enables text generation using LLMs."
slug: "/experimental-generators-api"
---
<a id="haystack_experimental.components.generators.chat.openai"></a>
## Module haystack\_experimental.components.generators.chat.openai
<a id="haystack_experimental.components.generators.chat.openai.OpenAIChatGenerator"></a>
### OpenAIChatGenerator
An OpenAI chat-based text generator component that supports hallucination risk scoring.
This is based on the paper
[LLMs are Bayesian, in Expectation, not in Realization](https://arxiv.org/abs/2507.11768).
## Usage Example:
```python
from haystack.dataclasses import ChatMessage
from haystack_experimental.utils.hallucination_risk_calculator.dataclasses import HallucinationScoreConfig
from haystack_experimental.components.generators.chat.openai import OpenAIChatGenerator
# Evidence-based Example
llm = OpenAIChatGenerator(model="gpt-4o")
rag_result = llm.run(
messages=[
ChatMessage.from_user(
text="Task: Answer strictly based on the evidence provided below.
"
"Question: Who won the Nobel Prize in Physics in 2019?
"
"Evidence:
"
"- Nobel Prize press release (2019): James Peebles (1/2); Michel Mayor & Didier Queloz (1/2).
"
"Constraints: If evidence is insufficient or conflicting, refuse."
)
],
hallucination_score_config=HallucinationScoreConfig(skeleton_policy="evidence_erase"),
)
print(f"Decision: {rag_result['replies'][0].meta['hallucination_decision']}")
print(f"Risk bound: {rag_result['replies'][0].meta['hallucination_risk']:.3f}")
print(f"Rationale: {rag_result['replies'][0].meta['hallucination_rationale']}")
print(f"Answer:
{rag_result['replies'][0].text}")
print("---")
```
<a id="haystack_experimental.components.generators.chat.openai.OpenAIChatGenerator.run"></a>
#### OpenAIChatGenerator.run
```python
@component.output_types(replies=list[ChatMessage])
def run(
messages: list[ChatMessage],
streaming_callback: StreamingCallbackT | None = None,
generation_kwargs: dict[str, Any] | None = None,
*,
tools: ToolsType | None = None,
tools_strict: bool | None = None,
hallucination_score_config: HallucinationScoreConfig | None = None
) -> dict[str, list[ChatMessage]]
```
Invokes chat completion based on the provided messages and generation parameters.
**Arguments**:
- `messages`: A list of ChatMessage instances representing the input messages.
- `streaming_callback`: A callback function that is called when a new token is received from the stream.
- `generation_kwargs`: Additional keyword arguments for text generation. These parameters will
override the parameters passed during component initialization.
For details on OpenAI API parameters, see [OpenAI documentation](https://platform.openai.com/docs/api-reference/chat/create).
- `tools`: A list of Tool and/or Toolset objects, or a single Toolset for which the model can prepare calls.
If set, it will override the `tools` parameter provided during initialization.
- `tools_strict`: Whether to enable strict schema adherence for tool calls. If set to `True`, the model will follow exactly
the schema provided in the `parameters` field of the tool definition, but this may increase latency.
If set, it will override the `tools_strict` parameter set during component initialization.
- `hallucination_score_config`: If provided, the generator will evaluate the hallucination risk of its responses using
the OpenAIPlanner and annotate each response with hallucination metrics.
This involves generating multiple samples and analyzing their consistency, which may increase
latency and cost. Use this option when you need to assess the reliability of the generated content
in scenarios where accuracy is critical.
For details, see the [research paper](https://arxiv.org/abs/2507.11768)
**Returns**:
A dictionary with the following key:
- `replies`: A list containing the generated responses as ChatMessage instances. If hallucination
scoring is enabled, each message will include additional metadata:
- `hallucination_decision`: "ANSWER" if the model decided to answer, "REFUSE" if it abstained.
- `hallucination_risk`: The EDFL hallucination risk bound.
- `hallucination_rationale`: The rationale behind the hallucination decision.
<a id="haystack_experimental.components.generators.chat.openai.OpenAIChatGenerator.run_async"></a>
#### OpenAIChatGenerator.run\_async
```python
@component.output_types(replies=list[ChatMessage])
async def run_async(
messages: list[ChatMessage],
streaming_callback: StreamingCallbackT | None = None,
generation_kwargs: dict[str, Any] | None = None,
*,
tools: ToolsType | None = None,
tools_strict: bool | None = None,
hallucination_score_config: HallucinationScoreConfig | None = None
) -> dict[str, list[ChatMessage]]
```
Asynchronously invokes chat completion based on the provided messages and generation parameters.
This is the asynchronous version of the `run` method. It has the same parameters and return values
but can be used with `await` in async code.
**Arguments**:
- `messages`: A list of ChatMessage instances representing the input messages.
- `streaming_callback`: A callback function that is called when a new token is received from the stream.
Must be a coroutine.
- `generation_kwargs`: Additional keyword arguments for text generation. These parameters will
override the parameters passed during component initialization.
For details on OpenAI API parameters, see [OpenAI documentation](https://platform.openai.com/docs/api-reference/chat/create).
- `tools`: A list of Tool and/or Toolset objects, or a single Toolset for which the model can prepare calls.
If set, it will override the `tools` parameter provided during initialization.
- `tools_strict`: Whether to enable strict schema adherence for tool calls. If set to `True`, the model will follow exactly
the schema provided in the `parameters` field of the tool definition, but this may increase latency.
If set, it will override the `tools_strict` parameter set during component initialization.
- `hallucination_score_config`: If provided, the generator will evaluate the hallucination risk of its responses using
the OpenAIPlanner and annotate each response with hallucination metrics.
This involves generating multiple samples and analyzing their consistency, which may increase
latency and cost. Use this option when you need to assess the reliability of the generated content
in scenarios where accuracy is critical.
For details, see the [research paper](https://arxiv.org/abs/2507.11768)
**Returns**:
A dictionary with the following key:
- `replies`: A list containing the generated responses as ChatMessage instances. If hallucination
scoring is enabled, each message will include additional metadata:
- `hallucination_decision`: "ANSWER" if the model decided to answer, "REFUSE" if it abstained.
- `hallucination_risk`: The EDFL hallucination risk bound.
- `hallucination_rationale`: The rationale behind the hallucination decision.
@@ -0,0 +1,218 @@
---
title: "Mem0 Memory Store"
id: experimental-mem0-memory-store-api
description: "Storage for the memories using Mem0 as the backend."
slug: "/experimental-mem0-memory-store-api"
---
<a id="haystack_experimental.memory_stores.mem0.memory_store"></a>
## Module haystack\_experimental.memory\_stores.mem0.memory\_store
<a id="haystack_experimental.memory_stores.mem0.memory_store.Mem0MemoryStore"></a>
### Mem0MemoryStore
A memory store implementation using Mem0 as the backend.
<a id="haystack_experimental.memory_stores.mem0.memory_store.Mem0MemoryStore.__init__"></a>
#### Mem0MemoryStore.\_\_init\_\_
```python
def __init__(*, api_key: Secret = Secret.from_env_var("MEM0_API_KEY"))
```
Initialize the Mem0 memory store.
**Arguments**:
- `api_key`: The Mem0 API key. You can also set it using `MEM0_API_KEY` environment variable.
<a id="haystack_experimental.memory_stores.mem0.memory_store.Mem0MemoryStore.to_dict"></a>
#### Mem0MemoryStore.to\_dict
```python
def to_dict() -> dict[str, Any]
```
Serialize the store configuration to a dictionary.
<a id="haystack_experimental.memory_stores.mem0.memory_store.Mem0MemoryStore.from_dict"></a>
#### Mem0MemoryStore.from\_dict
```python
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "Mem0MemoryStore"
```
Deserialize the store from a dictionary.
<a id="haystack_experimental.memory_stores.mem0.memory_store.Mem0MemoryStore.add_memories"></a>
#### Mem0MemoryStore.add\_memories
```python
def add_memories(*,
messages: list[ChatMessage],
infer: bool = True,
user_id: str | None = None,
run_id: str | None = None,
agent_id: str | None = None,
async_mode: bool = False,
**kwargs: Any) -> list[dict[str, Any]]
```
Add ChatMessage memories to Mem0.
**Arguments**:
- `messages`: List of ChatMessage objects with memory metadata
- `infer`: Whether to infer facts from the messages. If False, the whole message will
be added as a memory.
- `user_id`: The user ID to to store and retrieve memories from the memory store.
- `run_id`: The run ID to to store and retrieve memories from the memory store.
- `agent_id`: The agent ID to to store and retrieve memories from the memory store.
If you want Mem0 to store chat messages from the assistant, you need to set the agent_id.
- `async_mode`: Whether to add memories asynchronously.
If True, the method will return immediately and the memories will be added in the background.
- `kwargs`: Additional keyword arguments to pass to the Mem0 client.add method.
Note: ChatMessage.meta in the list of messages will be ignored because Mem0 doesn't allow
passing metadata for each message in the list. You can pass metadata for the whole memory
by passing the `metadata` keyword argument to the method.
**Returns**:
List of objects with the memory_id and the memory
<a id="haystack_experimental.memory_stores.mem0.memory_store.Mem0MemoryStore.search_memories"></a>
#### Mem0MemoryStore.search\_memories
```python
def search_memories(*,
query: str | None = None,
filters: dict[str, Any] | None = None,
top_k: int = 5,
user_id: str | None = None,
run_id: str | None = None,
agent_id: str | None = None,
include_memory_metadata: bool = False,
**kwargs: Any) -> list[ChatMessage]
```
Search for memories in Mem0.
If filters are not provided, at least one of user_id, run_id, or agent_id must be set.
If filters are provided, the search will be scoped to the provided filters and the other ids will be ignored.
**Arguments**:
- `query`: Text query to search for. If not provided, all memories will be returned.
- `filters`: Haystack filters to apply on search. For more details on Haystack filters, see https://docs.haystack.deepset.ai/docs/metadata-filtering
- `top_k`: Maximum number of results to return
- `user_id`: The user ID to to store and retrieve memories from the memory store.
- `run_id`: The run ID to to store and retrieve memories from the memory store.
- `agent_id`: The agent ID to to store and retrieve memories from the memory store.
If you want Mem0 to store chat messages from the assistant, you need to set the agent_id.
- `include_memory_metadata`: Whether to include the mem0 related metadata for the
retrieved memory in the ChatMessage.
If True, the metadata will include the mem0 related metadata i.e. memory_id, score, etc.
in the `mem0_memory_metadata` key.
If False, the `ChatMessage.meta` will only contain the user defined metadata.
- `kwargs`: Additional keyword arguments to pass to the Mem0 client.
If query is passed, the kwargs will be passed to the Mem0 client.search method.
If query is not passed, the kwargs will be passed to the Mem0 client.get_all method.
**Returns**:
List of ChatMessage memories matching the criteria
<a id="haystack_experimental.memory_stores.mem0.memory_store.Mem0MemoryStore.search_memories_as_single_message"></a>
#### Mem0MemoryStore.search\_memories\_as\_single\_message
```python
def search_memories_as_single_message(*,
query: str | None = None,
filters: dict[str, Any] | None = None,
top_k: int = 5,
user_id: str | None = None,
run_id: str | None = None,
agent_id: str | None = None,
**kwargs: Any) -> ChatMessage
```
Search for memories in Mem0 and return a single ChatMessage object.
If filters are not provided, at least one of user_id, run_id, or agent_id must be set.
If filters are provided, the search will be scoped to the provided filters and the other ids will be ignored.
**Arguments**:
- `query`: Text query to search for. If not provided, all memories will be returned.
- `filters`: Additional filters to apply on search. For more details on mem0 filters, see https://mem0.ai/docs/search/
- `top_k`: Maximum number of results to return
- `user_id`: The user ID to to store and retrieve memories from the memory store.
- `run_id`: The run ID to to store and retrieve memories from the memory store.
- `agent_id`: The agent ID to to store and retrieve memories from the memory store.
If you want Mem0 to store chat messages from the assistant, you need to set the agent_id.
- `kwargs`: Additional keyword arguments to pass to the Mem0 client.
If query is passed, the kwargs will be passed to the Mem0 client.search method.
If query is not passed, the kwargs will be passed to the Mem0 client.get_all method.
**Returns**:
A single ChatMessage object with the memories matching the criteria
<a id="haystack_experimental.memory_stores.mem0.memory_store.Mem0MemoryStore.delete_all_memories"></a>
#### Mem0MemoryStore.delete\_all\_memories
```python
def delete_all_memories(*,
user_id: str | None = None,
run_id: str | None = None,
agent_id: str | None = None,
**kwargs: Any) -> None
```
Delete memory records from Mem0.
At least one of user_id, run_id, or agent_id must be set.
**Arguments**:
- `user_id`: The user ID to delete memories from.
- `run_id`: The run ID to delete memories from.
- `agent_id`: The agent ID to delete memories from.
- `kwargs`: Additional keyword arguments to pass to the Mem0 client.delete_all method.
<a id="haystack_experimental.memory_stores.mem0.memory_store.Mem0MemoryStore.delete_memory"></a>
#### Mem0MemoryStore.delete\_memory
```python
def delete_memory(memory_id: str, **kwargs: Any) -> None
```
Delete memory from Mem0.
**Arguments**:
- `memory_id`: The ID of the memory to delete.
- `kwargs`: Additional keyword arguments to pass to the Mem0 client.delete method.
<a id="haystack_experimental.memory_stores.mem0.memory_store.Mem0MemoryStore.normalize_filters"></a>
#### Mem0MemoryStore.normalize\_filters
```python
@staticmethod
def normalize_filters(filters: dict[str, Any]) -> dict[str, Any]
```
Convert Haystack filters to Mem0 filters.
@@ -0,0 +1,76 @@
---
title: "Preprocessors"
id: experimental-preprocessors-api
description: "Pipelines wrapped as components."
slug: "/experimental-preprocessors-api"
---
<a id="haystack_experimental.components.preprocessors.md_header_level_inferrer"></a>
## Module haystack\_experimental.components.preprocessors.md\_header\_level\_inferrer
<a id="haystack_experimental.components.preprocessors.md_header_level_inferrer.MarkdownHeaderLevelInferrer"></a>
### MarkdownHeaderLevelInferrer
Infers and rewrites header levels in Markdown text to normalize hierarchy.
First header → Always becomes level 1 (#)
Subsequent headers → Level increases if no content between headers, stays same if content exists
Maximum level → Capped at 6 (######)
### Usage example
```python
from haystack import Document
from haystack_experimental.components.preprocessors import MarkdownHeaderLevelInferrer
# Create a document with uniform header levels
text = "## Title
## Subheader
Section
## Subheader
More Content"
doc = Document(content=text)
# Initialize the inferrer and process the document
inferrer = MarkdownHeaderLevelInferrer()
result = inferrer.run([doc])
# The headers are now normalized with proper hierarchy
print(result["documents"][0].content)
> # Title
## Subheader
Section
## Subheader
More Content
```
<a id="haystack_experimental.components.preprocessors.md_header_level_inferrer.MarkdownHeaderLevelInferrer.__init__"></a>
#### MarkdownHeaderLevelInferrer.\_\_init\_\_
```python
def __init__()
```
Initializes the MarkdownHeaderLevelInferrer.
<a id="haystack_experimental.components.preprocessors.md_header_level_inferrer.MarkdownHeaderLevelInferrer.run"></a>
#### MarkdownHeaderLevelInferrer.run
```python
@component.output_types(documents=list[Document])
def run(documents: list[Document]) -> dict
```
Infers and rewrites the header levels in the content for documents that use uniform header levels.
**Arguments**:
- `documents`: list of Document objects to process.
**Returns**:
dict: a dictionary with the key 'documents' containing the processed Document objects.
@@ -0,0 +1,124 @@
---
title: "Retrievers"
id: experimental-retrievers-api
description: "Sweep through Document Stores and return a set of candidate documents that are relevant to the query."
slug: "/experimental-retrievers-api"
---
<a id="haystack_experimental.components.retrievers.chat_message_retriever"></a>
## Module haystack\_experimental.components.retrievers.chat\_message\_retriever
<a id="haystack_experimental.components.retrievers.chat_message_retriever.ChatMessageRetriever"></a>
### ChatMessageRetriever
Retrieves chat messages from the underlying ChatMessageStore.
Usage example:
```python
from haystack.dataclasses import ChatMessage
from haystack_experimental.components.retrievers import ChatMessageRetriever
from haystack_experimental.chat_message_stores.in_memory import InMemoryChatMessageStore
messages = [
ChatMessage.from_assistant("Hello, how can I help you?"),
ChatMessage.from_user("Hi, I have a question about Python. What is a Protocol?"),
]
message_store = InMemoryChatMessageStore()
message_store.write_messages(chat_history_id="user_456_session_123", messages=messages)
retriever = ChatMessageRetriever(message_store)
result = retriever.run(chat_history_id="user_456_session_123")
print(result["messages"])
```
<a id="haystack_experimental.components.retrievers.chat_message_retriever.ChatMessageRetriever.__init__"></a>
#### ChatMessageRetriever.\_\_init\_\_
```python
def __init__(chat_message_store: ChatMessageStore, last_k: int | None = 10)
```
Create the ChatMessageRetriever component.
**Arguments**:
- `chat_message_store`: An instance of a ChatMessageStore.
- `last_k`: The number of last messages to retrieve. Defaults to 10 messages if not specified.
<a id="haystack_experimental.components.retrievers.chat_message_retriever.ChatMessageRetriever.to_dict"></a>
#### ChatMessageRetriever.to\_dict
```python
def to_dict() -> dict[str, Any]
```
Serializes the component to a dictionary.
**Returns**:
Dictionary with serialized data.
<a id="haystack_experimental.components.retrievers.chat_message_retriever.ChatMessageRetriever.from_dict"></a>
#### ChatMessageRetriever.from\_dict
```python
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "ChatMessageRetriever"
```
Deserializes the component from a dictionary.
**Arguments**:
- `data`: The dictionary to deserialize from.
**Returns**:
The deserialized component.
<a id="haystack_experimental.components.retrievers.chat_message_retriever.ChatMessageRetriever.run"></a>
#### ChatMessageRetriever.run
```python
@component.output_types(messages=list[ChatMessage])
def run(
chat_history_id: str,
*,
last_k: int | None = None,
current_messages: list[ChatMessage] | None = None
) -> dict[str, list[ChatMessage]]
```
Run the ChatMessageRetriever
**Arguments**:
- `chat_history_id`: A unique identifier for the chat session or conversation whose messages should be retrieved.
Each `chat_history_id` corresponds to a distinct chat history stored in the underlying ChatMessageStore.
For example, use a session ID or conversation ID to isolate messages from different chat sessions.
- `last_k`: The number of last messages to retrieve. This parameter takes precedence over the last_k
parameter passed to the ChatMessageRetriever constructor. If unspecified, the last_k parameter passed
to the constructor will be used.
- `current_messages`: A list of incoming chat messages to combine with the retrieved messages. System messages from this list
are prepended before the retrieved history, while all other messages (e.g., user messages) are appended
after. This is useful for including new conversational context alongside stored history so the output
can be directly used as input to a ChatGenerator or an Agent. If not provided, only the stored messages
will be returned.
**Raises**:
- `ValueError`: If last_k is not None and is less than 0.
**Returns**:
A dictionary with the following key:
- `messages` - The retrieved chat messages combined with any provided current messages.
@@ -0,0 +1,198 @@
---
title: "Summarizers"
id: experimental-summarizers-api
description: "Components that summarize texts into concise versions."
slug: "/experimental-summarizers-api"
---
<a id="haystack_experimental.components.summarizers.llm_summarizer"></a>
## Module haystack\_experimental.components.summarizers.llm\_summarizer
<a id="haystack_experimental.components.summarizers.llm_summarizer.LLMSummarizer"></a>
### LLMSummarizer
Summarizes text using a language model.
It's inspired by code from the OpenAI blog post: https://cookbook.openai.com/examples/summarizing_long_documents
Example
```python
from haystack_experimental.components.summarizers.summarizer import Summarizer
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack import Document
text = ("Machine learning is a subset of artificial intelligence that provides systems "
"the ability to automatically learn and improve from experience without being "
"explicitly programmed. The process of learning begins with observations or data. "
"Supervised learning algorithms build a mathematical model of sample data, known as "
"training data, in order to make predictions or decisions. Unsupervised learning "
"algorithms take a set of data that contains only inputs and find structure in the data. "
"Reinforcement learning is an area of machine learning where an agent learns to behave "
"in an environment by performing actions and seeing the results. Deep learning uses "
"artificial neural networks to model complex patterns in data. Neural networks consist "
"of layers of connected nodes, each performing a simple computation.")
doc = Document(content=text)
chat_generator = OpenAIChatGenerator(model="gpt-4")
summarizer = Summarizer(chat_generator=chat_generator)
summarizer.run(documents=[doc])
```
<a id="haystack_experimental.components.summarizers.llm_summarizer.LLMSummarizer.__init__"></a>
#### LLMSummarizer.\_\_init\_\_
```python
def __init__(chat_generator: ChatGenerator,
system_prompt: str
| None = "Rewrite this text in summarized form.",
summary_detail: float = 0,
minimum_chunk_size: int | None = 500,
chunk_delimiter: str = ".",
summarize_recursively: bool = False,
split_overlap: int = 0)
```
Initialize the Summarizer component.
:param chat_generator: A ChatGenerator instance to use for summarization.
:param system_prompt: The prompt to instruct the LLM to summarise text, if not given defaults to:
"Rewrite this text in summarized form."
:param summary_detail: The level of detail for the summary (0-1), defaults to 0.
This parameter controls the trade-off between conciseness and completeness by adjusting how many
chunks the text is divided into. At detail=0, the text is processed as a single chunk (or very few
chunks), producing the most concise summary. At detail=1, the text is split into the maximum number
of chunks allowed by minimum_chunk_size, enabling more granular analysis and detailed summaries.
The formula uses linear interpolation: num_chunks = 1 + detail * (max_chunks - 1), where max_chunks
is determined by dividing the document length by minimum_chunk_size.
:param minimum_chunk_size: The minimum token count per chunk, defaults to 500
:param chunk_delimiter: The character used to determine separator priority.
"." uses sentence-based splitting, "
" uses paragraph-based splitting, defaults to "."
:param summarize_recursively: Whether to use previous summaries as context, defaults to False.
:param split_overlap: Number of tokens to overlap between consecutive chunks, defaults to 0.
<a id="haystack_experimental.components.summarizers.llm_summarizer.LLMSummarizer.warm_up"></a>
#### LLMSummarizer.warm\_up
```python
def warm_up()
```
Warm up the chat generator and document splitter components.
<a id="haystack_experimental.components.summarizers.llm_summarizer.LLMSummarizer.to_dict"></a>
#### LLMSummarizer.to\_dict
```python
def to_dict() -> dict[str, Any]
```
Serializes the component to a dictionary.
**Returns**:
Dictionary with serialized data.
<a id="haystack_experimental.components.summarizers.llm_summarizer.LLMSummarizer.from_dict"></a>
#### LLMSummarizer.from\_dict
```python
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "LLMSummarizer"
```
Deserializes the component from a dictionary.
**Arguments**:
- `data`: Dictionary with serialized data.
**Returns**:
An instance of the component.
<a id="haystack_experimental.components.summarizers.llm_summarizer.LLMSummarizer.num_tokens"></a>
#### LLMSummarizer.num\_tokens
```python
def num_tokens(text: str) -> int
```
Estimates the token count for a given text.
Uses the RecursiveDocumentSplitter's tokenization logic for consistency.
**Arguments**:
- `text`: The text to tokenize
**Returns**:
The estimated token count
<a id="haystack_experimental.components.summarizers.llm_summarizer.LLMSummarizer.summarize"></a>
#### LLMSummarizer.summarize
```python
def summarize(text: str,
detail: float,
minimum_chunk_size: int,
summarize_recursively: bool = False) -> str
```
Summarizes text by splitting it into optimally-sized chunks and processing each with an LLM.
**Arguments**:
- `text`: Text to summarize
- `detail`: Detail level (0-1) where 0 is most concise and 1 is most detailed
- `minimum_chunk_size`: Minimum token count per chunk
- `summarize_recursively`: Whether to use previous summaries as context
**Raises**:
- `ValueError`: If detail is not between 0 and 1
**Returns**:
The textual content summarized by the LLM.
<a id="haystack_experimental.components.summarizers.llm_summarizer.LLMSummarizer.run"></a>
#### LLMSummarizer.run
```python
@component.output_types(summary=list[Document])
def run(*,
documents: list[Document],
detail: float | None = None,
minimum_chunk_size: int | None = None,
summarize_recursively: bool | None = None,
system_prompt: str | None = None) -> dict[str, list[Document]]
```
Run the summarizer on a list of documents.
**Arguments**:
- `documents`: List of documents to summarize
- `detail`: The level of detail for the summary (0-1), defaults to 0 overwriting the component's default.
- `minimum_chunk_size`: The minimum token count per chunk, defaults to 500 overwriting the
component's default.
- `system_prompt`: If given it will overwrite prompt given at init time or the default one.
- `summarize_recursively`: Whether to use previous summaries as context, defaults to False overwriting the
component's default.
**Raises**:
- `RuntimeError`: If the component wasn't warmed up.
@@ -0,0 +1,105 @@
---
title: "Writers"
id: experimental-writers-api
description: "Writers for Haystack."
slug: "/experimental-writers-api"
---
<a id="haystack_experimental.components.writers.chat_message_writer"></a>
## Module haystack\_experimental.components.writers.chat\_message\_writer
<a id="haystack_experimental.components.writers.chat_message_writer.ChatMessageWriter"></a>
### ChatMessageWriter
Writes chat messages to an underlying ChatMessageStore.
Usage example:
```python
from haystack.dataclasses import ChatMessage
from haystack_experimental.components.writers import ChatMessageWriter
from haystack_experimental.chat_message_stores.in_memory import InMemoryChatMessageStore
messages = [
ChatMessage.from_assistant("Hello, how can I help you?"),
ChatMessage.from_user("I have a question about Python."),
]
message_store = InMemoryChatMessageStore()
writer = ChatMessageWriter(message_store)
writer.run(chat_history_id="user_456_session_123", messages=messages)
```
<a id="haystack_experimental.components.writers.chat_message_writer.ChatMessageWriter.__init__"></a>
#### ChatMessageWriter.\_\_init\_\_
```python
def __init__(chat_message_store: ChatMessageStore) -> None
```
Create a ChatMessageWriter component.
**Arguments**:
- `chat_message_store`: The ChatMessageStore where the chat messages are to be written.
<a id="haystack_experimental.components.writers.chat_message_writer.ChatMessageWriter.to_dict"></a>
#### ChatMessageWriter.to\_dict
```python
def to_dict() -> dict[str, Any]
```
Serializes the component to a dictionary.
**Returns**:
Dictionary with serialized data.
<a id="haystack_experimental.components.writers.chat_message_writer.ChatMessageWriter.from_dict"></a>
#### ChatMessageWriter.from\_dict
```python
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "ChatMessageWriter"
```
Deserializes the component from a dictionary.
**Arguments**:
- `data`: The dictionary to deserialize from.
**Raises**:
- `DeserializationError`: If the message store is not properly specified in the serialization data or its type cannot be imported.
**Returns**:
The deserialized component.
<a id="haystack_experimental.components.writers.chat_message_writer.ChatMessageWriter.run"></a>
#### ChatMessageWriter.run
```python
@component.output_types(messages_written=int)
def run(chat_history_id: str, messages: list[ChatMessage]) -> dict[str, int]
```
Run the ChatMessageWriter on the given input data.
**Arguments**:
- `chat_history_id`: A unique identifier for the chat session or conversation whose messages should be retrieved.
Each `chat_history_id` corresponds to a distinct chat history stored in the underlying ChatMessageStore.
For example, use a session ID or conversation ID to isolate messages from different chat sessions.
- `messages`: A list of chat messages to write to the store.
**Returns**:
- `messages_written`: Number of messages written to the ChatMessageStore.