docs: custom output extraction (#817)
In deep agent workflows, each sub‐agent automatically performs an LLM step to summarize its tool calls before returning to its parent. This leads to: 1. Excessive latency: every nested agent invokes the LLM, compounding delays. 2. Loss of raw tool data: summaries may strip out details the top‐level agent needs. We discovered that `Agent.as_tool(...)` already accepts an (undocumented) `custom_output_extractor` parameter. By providing a callback, a parent agent can override what the sub‐agent returns e.g. hand back raw tool outputs or a custom slice so that only the final agent does summarization. --- This PR adds a “Custom output extraction” section to the Markdown docs under “Agents as tools,” with a minimal code example.
This commit is contained in:
parent
c98e234845
commit
dcb88e69cd
1 changed files with 27 additions and 0 deletions
|
|
@ -284,6 +284,33 @@ async def run_my_agent() -> str:
|
||||||
return str(result.final_output)
|
return str(result.final_output)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Custom output extraction
|
||||||
|
|
||||||
|
In certain cases, you might want to modify the output of the tool-agents before returning it to the central agent. This may be useful if you want to:
|
||||||
|
|
||||||
|
- Extract a specific piece of information (e.g., a JSON payload) from the sub-agent's chat history.
|
||||||
|
- Convert or reformat the agent’s final answer (e.g., transform Markdown into plain text or CSV).
|
||||||
|
- Validate the output or provide a fallback value when the agent’s response is missing or malformed.
|
||||||
|
|
||||||
|
You can do this by supplying the `custom_output_extractor` argument to the `as_tool` method:
|
||||||
|
|
||||||
|
```python
|
||||||
|
async def extract_json_payload(run_result: RunResult) -> str:
|
||||||
|
# Scan the agent’s outputs in reverse order until we find a JSON-like message from a tool call.
|
||||||
|
for item in reversed(run_result.new_items):
|
||||||
|
if isinstance(item, ToolCallOutputItem) and item.output.strip().startswith("{"):
|
||||||
|
return item.output.strip()
|
||||||
|
# Fallback to an empty JSON object if nothing was found
|
||||||
|
return "{}"
|
||||||
|
|
||||||
|
|
||||||
|
json_tool = data_agent.as_tool(
|
||||||
|
tool_name="get_data_json",
|
||||||
|
tool_description="Run the data agent and return only its JSON payload",
|
||||||
|
custom_output_extractor=extract_json_payload,
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
## Handling errors in function tools
|
## Handling errors in function tools
|
||||||
|
|
||||||
When you create a function tool via `@function_tool`, you can pass a `failure_error_function`. This is a function that provides an error response to the LLM in case the tool call crashes.
|
When you create a function tool via `@function_tool`, you can pass a `failure_error_function`. This is a function that provides an error response to the LLM in case the tool call crashes.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue