Skip to content

[Bug]: AgentWorkflow doesn't preserve system prompt when passing chat_history to run #17721

@tcdent

Description

@tcdent

Bug Description

Passing chat_history to AgentWorkflow.run does not work as expected.

Version

0.12.15

Steps to Reproduce

import asyncio
from llama_index.core.llms import ChatMessage
from llama_index.core.agent.workflow import (
    FunctionAgent, 
    AgentWorkflow, 
    AgentOutput,  
)
from llama_index.llms.openai import OpenAI

def format_history(history: list[ChatMessage]) -> str:
    return "\n\n".join([f"{msg.role}: {msg.content}" for msg in history])

def print_agent_messages(event):
    if isinstance(event, AgentOutput) and event.response.content:
        print(event.current_agent_name)
        print(event.response.content)

history: list[ChatMessage] = [
    ChatMessage(role="user", content="Tell me your name."),
]
agent = FunctionAgent(
    name="agent", 
    description="Test agent", 
    system_prompt="Your name is Steve. You are a helpful assistant.", 
    llm=OpenAI(model='gpt-4o'), 
    tools=[], 
)
workflow = AgentWorkflow(
    agents=[agent, ], 
)

# when passing chat_history the workflow still looks at the user_msg
# and results in an error
try:
    handler = workflow.run(
        chat_history=history, 
    )
    async for event in handler.stream_events():
        print_agent_messages(event)
except Exception as e:
    print(e)

# the error can be bypassed by passing an empty string to user_msg, but
# the agent does not have awareness of it's system prompt
handler = workflow.run(
    user_msg="", 
    chat_history=history, 
)
async for event in handler.stream_events():
    print_agent_messages(event)

# if we pass the chat history to user_msg, the agent has awareness of it's
# system prompt and the workflow runs correctly
handler = workflow.run(
    user_msg=format_history(history), 
)
async for event in handler.stream_events():
    print_agent_messages(event)

Relevant Logs/Tracbacks

Traceback (most recent call last):
  File "/opt/homebrew/Cellar/python@3.12/3.12.7_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/Users/tcdent/Work/llamaindex/.venv/lib/python3.12/site-packages/llama_index/core/instrumentation/dispatcher.py", line 285, in handle_future_result
    raise exception
  File "/Users/tcdent/Work/llamaindex/.venv/lib/python3.12/site-packages/llama_index/core/workflow/workflow.py", line 445, in _run_workflow
    raise exception_raised
  File "/Users/tcdent/Work/llamaindex/.venv/lib/python3.12/site-packages/llama_index/core/workflow/workflow.py", line 254, in _task
    raise WorkflowRuntimeError(
llama_index.core.workflow.errors.WorkflowRuntimeError: Error in step 'init_run': 'NoneType' object has no attribute 'content'
agent
Hello! How can I assist you today?
agent
My name is Steve. How can I assist you today?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriageIssue needs to be triaged/prioritized

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions