Skip to content

Commit 5add74e

Browse files
docs: sync Haystack API reference on Docusaurus (#12201)
Co-authored-by: anakin87 <44616784+anakin87@users.noreply.github.com>
1 parent e3aa021 commit 5add74e

1 file changed

Lines changed: 184 additions & 0 deletions

File tree

docs-website/reference/haystack-api/tools_api.md

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,190 @@ slug: "/tools-api"
66
---
77

88

9+
## agent_tool
10+
11+
### agent_result_to_string
12+
13+
```python
14+
agent_result_to_string(result: dict[str, Any]) -> str
15+
```
16+
17+
Default `outputs_to_string` handler
18+
19+
### AgentTool
20+
21+
Bases: <code>ComponentTool</code>
22+
23+
A Tool that wraps a Haystack Agent, allowing it to be used as a tool by another Agent.
24+
25+
AgentTool is a building block for multi-agent systems: an Agent specialized in one task becomes a tool that
26+
other Agents can delegate to. The calling Agent only sees the final reply, so all the steps the wrapped Agent
27+
takes stay out of its context. Sensible defaults make this work out of the box: the task is delegated as a
28+
single user message and comes back as text.
29+
30+
To use AgentTool, you first need a Haystack Agent. Below is an example of creating an AgentTool from an Agent
31+
that searches the web with a SerperDevWebSearch component from the `serperdev-haystack` integration package
32+
(`pip install serperdev-haystack`).
33+
34+
## Usage Example:
35+
36+
<!-- test-ignore -->
37+
38+
```python
39+
from haystack.components.agents import Agent
40+
from haystack.components.generators.chat import OpenAIResponsesChatGenerator
41+
from haystack.dataclasses import ChatMessage
42+
from haystack.tools import AgentTool, ComponentTool
43+
from haystack_integrations.components.websearch.serperdev import SerperDevWebSearch
44+
45+
researcher = Agent(
46+
chat_generator=OpenAIResponsesChatGenerator(model="gpt-5.4-mini"),
47+
system_prompt="You are a research specialist. Investigate the task and report your findings.",
48+
tools=[
49+
ComponentTool(
50+
component=SerperDevWebSearch(
51+
top_k=3,
52+
),
53+
name="web_search",
54+
description="Search the web for current information on any topic",
55+
),
56+
],
57+
)
58+
59+
research = AgentTool(
60+
agent=researcher,
61+
name="research",
62+
description="Research a question on the web and report the findings",
63+
)
64+
65+
coordinator = Agent(
66+
chat_generator=OpenAIResponsesChatGenerator(model="gpt-5.4"),
67+
tools=[research],
68+
system_prompt="You coordinate specialists. Delegate research questions, then answer the user.",
69+
)
70+
71+
result = coordinator.run([ChatMessage.from_user("What are the latest developments in the Haystack framework?")])
72+
print(result["last_message"].text)
73+
```
74+
75+
#### __init__
76+
77+
```python
78+
__init__(
79+
agent: Agent,
80+
*,
81+
name: str,
82+
description: str,
83+
parameters: dict[str, Any] | None = None,
84+
outputs_to_string: dict[str, str | Callable[[Any], str]] | None = None,
85+
inputs_from_state: dict[str, str] | None = None,
86+
outputs_to_state: dict[str, dict[str, str | Callable]] | None = None
87+
) -> None
88+
```
89+
90+
Create a Tool instance from a Haystack Agent.
91+
92+
**Parameters:**
93+
94+
- **agent** (<code>Agent</code>) – The Haystack Agent to wrap as a tool.
95+
- **name** (<code>str</code>) – Name of the tool.
96+
- **description** (<code>str</code>) – Description of the tool. It should tell the calling LLM what the Agent is specialized in
97+
and when to delegate to it.
98+
- **parameters** (<code>dict\[str, Any\] | None</code>) – A JSON schema defining the parameters expected by the Tool.
99+
Will fall back to a schema with the task to delegate as a single user message, plus one string parameter
100+
for every other mandatory input of the Agent, if not provided.
101+
- **outputs_to_string** (<code>dict\[str, str | Callable\\[[Any\], str\]\] | None</code>) – Optional dictionary defining how tool outputs should be converted into string(s) or results.
102+
If not provided, the tool result is the text of the Agent's final reply, or the serialized message if
103+
the reply has no text. A warning is appended if the Agent stopped because it reached `max_agent_steps`.
104+
105+
`outputs_to_string` supports two formats:
106+
107+
1. Single output format - use "source", "handler", and/or "raw_result" at the root level:
108+
109+
```python
110+
{
111+
"source": "last_message", "handler": format_reply, "raw_result": False
112+
}
113+
```
114+
115+
- `source`: If provided, only the specified output key is sent to the handler.
116+
- `handler`: A function that takes the tool output (or the extracted source value) and returns the
117+
final result.
118+
- `raw_result`: If `True`, the result is returned raw without string conversion, but applying the
119+
`handler` if provided. This is intended for tools that return images. In this mode, the `handler`
120+
is required, since the Agent returns a dictionary, and it must return a list of
121+
`TextContent`/`ImageContent` objects to ensure compatibility with Chat Generators.
122+
123+
1. Multiple output format - map keys to individual configurations:
124+
125+
```python
126+
{
127+
"reply": {"source": "last_message", "handler": format_reply},
128+
"steps": {"source": "step_count", "handler": str}
129+
}
130+
```
131+
132+
Each key maps to a dictionary that can contain "source" and/or "handler".
133+
Note that `raw_result` is not supported in the multiple output format.
134+
135+
- **inputs_from_state** (<code>dict\[str, str\] | None</code>) – Optional dictionary mapping the calling Agent's state keys to Agent input names.
136+
Example: `{"subject": "topic"}` maps state's "subject" to the Agent's "topic" input.
137+
Inputs mapped this way are not added to the generated `parameters` schema, since the calling Agent
138+
provides them.
139+
- **outputs_to_state** (<code>dict\[str, dict\[str, str | Callable\]\] | None</code>) – Optional dictionary defining how tool outputs map to keys within state as well as optional handlers.
140+
The keys must be declared in the `state_schema` of the calling Agent.
141+
Handlers merge the tool output into the state and are called as `handler(current_value, tool_output)`.
142+
If the source is provided only the specified output key is sent to the handler.
143+
Example:
144+
145+
```python
146+
{
147+
"notes": {"source": "last_message", "handler": custom_handler}
148+
}
149+
```
150+
151+
If the source is omitted the whole tool result is sent to the handler.
152+
Example:
153+
154+
```python
155+
{
156+
"notes": {"handler": custom_handler}
157+
}
158+
```
159+
160+
**Raises:**
161+
162+
- <code>TypeError</code> – If the object passed is not a Haystack Agent instance.
163+
- <code>ValueError</code> – If `parameters` is provided but does not cover all the mandatory inputs of the Agent.
164+
165+
#### to_dict
166+
167+
```python
168+
to_dict() -> dict[str, Any]
169+
```
170+
171+
Serializes the AgentTool to a dictionary.
172+
173+
**Returns:**
174+
175+
- <code>dict\[str, Any\]</code> – The serialized dictionary representation of AgentTool.
176+
177+
#### from_dict
178+
179+
```python
180+
from_dict(data: dict[str, Any]) -> AgentTool
181+
```
182+
183+
Deserializes the AgentTool from a dictionary.
184+
185+
**Parameters:**
186+
187+
- **data** (<code>dict\[str, Any\]</code>) – The dictionary representation of AgentTool.
188+
189+
**Returns:**
190+
191+
- <code>AgentTool</code> – The deserialized AgentTool instance.
192+
9193
## component_tool
10194

11195
### ComponentTool

0 commit comments

Comments
 (0)