v2.9.0
⭐️ Highlights
Tool Calling Support
We are introducing the Tool, a simple and unified abstraction for representing tools in Haystack, and the ToolInvoker, which executes tool calls prepared by LLMs. These features make it easy to integrate tool calling into your Haystack pipelines, enabling seamless interaction with tools when used with components like OpenAIChatGenerator and HuggingFaceAPIChatGenerator. Here's how you can use them:
def dummy_weather_function(city: str):
return f"The weather in {city} is 20 degrees."
tool = Tool(
name="weather_tool",
description="A tool to get the weather",
function=dummy_weather_function,
parameters={
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
}
)
pipeline = Pipeline()
pipeline.add_component("llm", OpenAIChatGenerator(model="gpt-4o-mini", tools=[tool]))
pipeline.add_component("tool_invoker", ToolInvoker(tools=[tool]))
pipeline.connect("llm.replies", "tool_invoker.messages")
message = ChatMessage.from_user("How is the weather in Berlin today?")
result = pipeline.run({"llm": {"messages": [message]}})Use Components as Tools
As an abstraction of Tool, ComponentTool allows LLMs to interact directly with components like web search, document processing, or custom user components. It simplifies schema generation and type conversion, making it easy to expose complex component functionality to LLMs.
# Create a tool from the component
tool = ComponentTool(
component=SerperDevWebSearch(api_key=Secret.from_env_var("SERPERDEV_API_KEY"), top_k=3),
name="web_search", # Optional: defaults to "serper_dev_web_search"
description="Search the web for current information on any topic" # Optional: defaults to component docstring
)New Splitting Method: RecursiveDocumentSplitter
RecursiveDocumentSplitter introduces a smarter way to split text. It uses a set of separators to divide text recursively, starting with the first separator. If chunks are still larger than the specified size, the splitter moves to the next separator in the list. This approach ensures efficient and granular text splitting for improved processing.
from haystack.components.preprocessors import RecursiveDocumentSplitter
splitter = RecursiveDocumentSplitter(split_length=260, split_overlap=0, separators=["\n\n", "\n", ".", " "])
doc_chunks = splitter.run([Document(content="...")])⚠️ Refactored ChatMessage dataclass
ChatMessage dataclass has been refactored to improve flexibility and compatibility. As part of this update, the content attribute has been removed and replaced with a new text property for accessing the ChatMessage's textual value. This change ensures future-proofing and better support for features like tool calls and their results. For details on the new API and migration steps, see the ChatMessage documentation. If you have any questions about this refactoring, feel free to let us know in this Github discussion.
⬆️ Upgrade Notes
- The refactoring of the
ChatMessagedata class includes some breaking changes involvingChatMessagecreation and accessing attributes. If you have aPipelinecontaining aChatPromptBuilder, serialized withhaystack-ai =< 2.9.0, deserialization may break. For detailed information about the changes and how to migrate, see the ChatMessage documentation. - Removed the deprecated
converterinit argument fromPyPDFToDocument. Use other init arguments instead, or create a custom component. - The
SentenceWindowRetrieveroutput keycontext_documentsnow outputs aList[Document]containing the retrieved documents and the context windows ordered bysplit_idx_start. - Update default value of
store_full_pathtoFalsein converters
🚀 New Features
-
Introduced the
ComponentTool, a new tool that wraps Haystack components, allowing them to be utilized as tools for LLMs (various ChatGenerators). ThisComponentToolsupports automatic tool schema generation, input type conversion, and offers support for components with run methods that have input types:- Basic types (str, int, float, bool, dict)
- Dataclasses (both simple and nested structures)
- Lists of basic types (e.g.,
List[str]) - Lists of dataclasses (e.g.,
List[Document]) - Parameters with mixed types (e.g.,
List[Document], str etc.)
Example usage:
from haystack import component, Pipeline from haystack.tools import ComponentTool from haystack.components.websearch import SerperDevWebSearch from haystack.utils import Secret from haystack.components.tools.tool_invoker import ToolInvoker from haystack.components.generators.chat import OpenAIChatGenerator from haystack.dataclasses import ChatMessage # Create a SerperDev search component search = SerperDevWebSearch(api_key=Secret.from_env_var("SERPERDEV_API_KEY"), top_k=3) # Create a tool from the component tool = ComponentTool( component=search, name="web_search", # Optional: defaults to "serper_dev_web_search" description="Search the web for current information on any topic" # Optional: defaults to component docstring ) # Create pipeline with OpenAIChatGenerator and ToolInvoker pipeline = Pipeline() pipeline.add_component("llm", OpenAIChatGenerator(model="gpt-4o-mini", tools=[tool])) pipeline.add_component("tool_invoker", ToolInvoker(tools=[tool])) # Connect components pipeline.connect("llm.replies", "tool_invoker.messages") message = ChatMessage.from_user("Use the web search tool to find information about Nikola Tesla") # Run pipeline result = pipeline.run({"llm": {"messages": [message]}}) print(result)
-
Add
XLSXToDocumentconverter that loads an Excel file using Pandas + openpyxl and by default converts each sheet into a separateDocumentin CSV format. -
Added a new
store_full_pathparameter to the__init__methods ofPyPDFToDocumentandAzureOCRDocumentConverter. The default value isTrue, which stores the full file path in the metadata of the output documents. When set toFalse, only the file name is stored. -
Add a new experimental component
ToolInvoker. This component invokes tools based on tool calls prepared by Language Models and returns the results as a list ofChatMessageobjects with tool role. -
Adding a
RecursiveSplitter, which uses a set of separators to split text recursively. It attempts to divide the text using the first separator, and if the resulting chunks are still larger than the specified size, it moves to the next separator in the list. -
Added a
create_tool_from_functionfunction to create aTooinstance from a function, with automatic generation of name, description and parameters. Added atooldecorator to achieve the same result. -
Add support for Tools in the Hugging Face API Chat Generator.
-
Changed the
ChatMessagedataclass to support different types of content, including tool calls, and tool call results. -
Add support for Tools in the OpenAI Chat Generator.
-
Added a new
Tooldataclass to represent a tool for which Language Models can prepare calls. -
Added the component
StringJoinerto join strings from different components to a list of strings.
⚡️ Enhancement Notes
-
Added
default_headersparameter toAzureOpenAIDocumentEmbedderandAzureOpenAITextEmbedder. -
Add
tokenargument toNamedEntityExtractorto allow usage of private Hugging Face models. -
Add the
from_openai_dict_formatclass method to theChatMessageclass. It allows you to create aChatMessagefrom a dictionary in the format that OpenAI's Chat API expects. -
Add a testing job to check that all packages can be imported successfully. This should help detect several issues, such as forgetting to use a forward reference for a type hint coming from a lazy import.
-
DocumentJoinermethods_concatenate()and_distribution_based_rank_fusion()were converted to static methods. -
Improve serialization and deserialization of callables. We now allow serialization of class methods and static methods and explicitly prohibit serialization of instance methods, lambdas, and nested functions.
-
Added new initialization parameters to the
PyPDFToDocumentcomponent to customize the text extraction process from PDF files. -
Reorganized the document store test suite to isolate
dataframefilter tests. This change prepares for potential future deprecation of the Document class'sdataframefield. -
Move
Toolto a new dedicatedtoolspackage. RefactorToolserialization and deserialization to make it more flexible and include type information. -
The
NLTKDocumentSplitterwas merged into theDocumentSplitterwhich now provides the same functionality as theNLTKDocumentSplitter. Thesplit_by="sentence"now uses a custom sentence boundary detection based on thenltklibrary. The previoussentencebehaviour can still be achieved bysplit_by="period". -
Improved deserialization of callables by using
importlibinstead ofsys.modules. This change allows importing local functions and classes that are not insys.moduleswhen deserializing callable. -
Change
OpenAIDocumentEmbedderto keep running if a batch fails embedding. Now OpenAI returns an error we log that error and keep processing following batches.
⚠️ Deprecation Notes
-
The
NLTKDocumentSplitterwill be deprecated and will be removed in the next release. TheDocumentSplitterwill instead support the functionality of theNLTKDocumentSplitter. -
The function role and
ChatMessage.from_functionclass method have been deprecated and will be removed in Haystack 2.10.0.ChatMessage.from_functionalso attempts to produce a valid tool message. For more information, see the documentation: https://docs.haystack.deepset.ai/docs/chatmessage -
The
SentenceWindowRetrieveroutput ofcontext_documentschanged. Instead of aList[List[Document], the output is aList[Document], where the documents are ordered bysplit_idx_startvalue.
🐛 Bug Fixes
-
Add missing stream mime type assignment to the
LinkContentFetcherfor the single url scenario. -
Previously, the pipelines that use
FileTypeRoutercould fail if they received a single URL as an input. -
OpenAIChatGenerator no longer passes tools to the OpenAI client if none are provided. Previously, a null value was passed. This change improves compatibility with OpenAI-compatible APIs that do not support tools.
-
ByteStream now truncates the data to 100 bytes in the string representation to avoid excessive log output.
-
Make the HuggingFaceLocalChatGenerator compatible with the new ChatMessage format, by converting the messages to the format expected by HuggingFace.
-
Serialize the
chat_templateparameter. -
Moved the NLTK download of
DocumentSplitterandNLTKDocumentSplittertowarm_up(). This prevents calling to an external API during instantiation. If aDocumentSplitterorNLTKDocumentSplitteris used for sentence splitting outside of a pipeline,warm_up()now needs to be called before running the component. -
PDFMinerToDocumentnow creates documents withidbased on converted text and metadata. Before,PDFMinerToDocumentdid not consider the document's meta field when generating the document'sid. -
Pin OpenAI client to >=1.56.1 to avoid issues related to changes in the httpx library.
-
PyPDFToDocumentnow creates documents with id based on converted text and metadata. Before it didn't take the meta data into account. -
Fixes issues with deserialization of components in multi-threaded environments.