Skip to content
Open
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
5 changes: 4 additions & 1 deletion src/agents/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,10 @@ def _convert_tool_output(cls, output: Any) -> str | ResponseFunctionCallOutputIt
maybe_converted_output_list = [
cls._maybe_get_output_as_structured_function_output(item) for item in output
]
if all(maybe_converted_output_list):
# An empty list/tuple has no structured items; ``all([])`` is ``True``,
# so guard against it to avoid emitting an empty structured-output list
# (which would drop the tool result) and stringify instead.
if maybe_converted_output_list and all(maybe_converted_output_list):
return [
cls._convert_single_tool_output_pydantic_model(item)
for item in maybe_converted_output_list
Expand Down
23 changes: 23 additions & 0 deletions tests/test_tool_output_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,26 @@ def test_tool_call_output_item_mixed_list_partial_invalid_not_converted() -> Non
# All-or-nothing: if any item is invalid, convert entire list to string
assert isinstance(payload["output"], str)
assert payload["output"] == "[{'type': 'text', 'text': 'hello'}, {'msg': 'foobar'}]"


def test_tool_call_output_item_empty_list_not_converted() -> None:
"""An empty list has no structured items, so it should stringify rather than
produce an empty structured-output list (which would drop the tool result)."""
call = _make_tool_call()
payload = ItemHelpers.tool_call_output_item(call, [])

assert payload["type"] == "function_call_output"
assert payload["call_id"] == call.call_id
assert isinstance(payload["output"], str)
assert payload["output"] == "[]"


def test_tool_call_output_item_empty_tuple_not_converted() -> None:
"""An empty tuple should stringify, mirroring the empty-list behavior."""
call = _make_tool_call()
payload = ItemHelpers.tool_call_output_item(call, ())

assert payload["type"] == "function_call_output"
assert payload["call_id"] == call.call_id
assert isinstance(payload["output"], str)
assert payload["output"] == "()"
Loading