Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions haystack/components/agents/state/state_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,23 @@ def _is_list_type(type_hint: Any) -> bool:
return type_hint == list or (hasattr(type_hint, "__origin__") and get_origin(type_hint) == list)


def merge_lists(current: Union[list[T], T, None], new: Union[list[T], T]) -> list[T]:
def merge_lists(current: Union[list[T], T, None], new: Union[list[T], T, None]) -> list[T]:
"""
Merges two values into a single list.

If either `current` or `new` is not already a list, it is converted into one.
The function ensures that both inputs are treated as lists and concatenates them.

If `current` is None, it is treated as an empty list.
If `new` is None, the current list is returned unchanged.

:param current: The existing value(s), either a single item or a list.
:param new: The new value(s) to merge, either a single item or a list.
:param new: The new value(s) to merge, either a single item, a list, or None.
:return: A list containing elements from both `current` and `new`.
"""
current_list = [] if current is None else current if isinstance(current, list) else [current]
if new is None:
return current_list
new_list = new if isinstance(new, list) else [new]
return current_list + new_list

Expand Down
4 changes: 4 additions & 0 deletions haystack/components/tools/tool_invoker.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,10 @@ def _merge_tool_outputs(tool: Tool, result: Any, state: State) -> None:
source_key = config.get("source", None)
output_value = result.get(source_key) if source_key else result

# Skip state update when the tool didn't produce this output key
if output_value is None:
continue

# Merge other outputs into the state
state.set(state_key, output_value, handler_override=config.get("handler"))

Expand Down