Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion libs/giskard-agents/src/giskard/agents/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ async def execute(

async def _run_tools(self, chat: Chat[Any]) -> AsyncGenerator[ToolMessage, None]:
if (
not chat.last
not chat.messages
or not isinstance(chat.last, AssistantMessage)
or not chat.last.tool_calls
):
Expand Down
26 changes: 26 additions & 0 deletions libs/giskard-agents/tests/test_workflow_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,29 @@ async def test_run_returns_successful_chat():

assert not chat.failed
assert chat.last.content == "World!"


async def test_run_with_no_messages_does_not_raise_indexerror():
"""A ChatWorkflow with no messages added must not crash with IndexError.

``_run_tools``'s guard checks ``not chat.last`` before checking whether
there is anything to process, but ``Chat.last`` indexes ``messages[-1]``,
which raises ``IndexError`` on an empty list instead of behaving falsy.
"""
gen = MagicMock(spec=BaseGenerator)
gen.complete = AsyncMock(
return_value=CompletionResponse(
choices=[
Choice(
message=AssistantMessage(content="Hello!"),
finish_reason="stop",
index=0,
)
]
)
)

chat = await ChatWorkflow(generator=gen).run()

assert not chat.failed
assert chat.last.content == "Hello!"
Loading