Skip to content

[Bug]: ReactAgent workflow crashes with context in formatter #19156

Open
@Florian-BACHO

Description

@Florian-BACHO

Bug Description

Hi,

I noticed that the llama_index.core.agent.workflow.ReActAgent workflow crashes when setting a ReActChatFormatter formatter with a context.

Using ReActChatFormatter.from_defaults() works fine but using ReActChatFormatter.from_defaults(context="This is some context") produces a KeyError.

Version

0.12.43

Steps to Reproduce

import asyncio

from llama_index.core.agent import ReActChatFormatter
from llama_index.core.agent.workflow import ReActAgent
from llama_index.core.base.llms.types import ChatMessage
from llama_index.llms.openai import OpenAI


async def main() -> None:
    llm = OpenAI(model="gpt-4o")

    formatter = ReActChatFormatter.from_defaults(context="This is some context")

    agent = ReActAgent(
        tools=[], llm=llm, formatter=formatter
    )

    usr_msg = ChatMessage(role="user", content="hello")

    await agent.run(user_msg=usr_msg)


if __name__ == "__main__":
    asyncio.run(main())

Relevant Logs/Tracbacks

Traceback (most recent call last):
  File "/home/root/test/.venv/lib/python3.12/site-packages/workflows/context/context.py", line 628, in _step_worker
    new_ev = await instrumented_step(**kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/root/test/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 365, in async_wrapper
    result = await func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/root/test/.venv/lib/python3.12/site-packages/llama_index/core/agent/workflow/base_agent.py", line 346, in run_agent_step
    agent_output = await self.take_step(
                   ^^^^^^^^^^^^^^^^^^^^^
  File "/home/root/test/.venv/lib/python3.12/site-packages/llama_index/core/agent/workflow/react_agent.py", line 86, in take_step
    input_chat = react_chat_formatter.format(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/root/test/.venv/lib/python3.12/site-packages/llama_index/core/agent/react/formatter.py", line 81, in format
    fmt_sys_header = self.system_header.format(**format_args)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'context'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/root/.local/share/uv/python/cpython-3.12.10-linux-x86_64-gnu/lib/python3.12/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/home/root/test/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 290, in handle_future_result
    raise exception
  File "/home/root/test/.venv/lib/python3.12/site-packages/workflows/workflow.py", line 416, in _run_workflow
    raise exception_raised
  File "/home/root/test/.venv/lib/python3.12/site-packages/workflows/context/context.py", line 637, in _step_worker
    raise WorkflowRuntimeError(
workflows.errors.WorkflowRuntimeError: Error in step 'run_agent_step': 'context'
Traceback (most recent call last):
  File "/home/root/test/.venv/lib/python3.12/site-packages/workflows/context/context.py", line 628, in _step_worker
    new_ev = await instrumented_step(**kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/root/test/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 365, in async_wrapper
    result = await func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/root/test/.venv/lib/python3.12/site-packages/llama_index/core/agent/workflow/base_agent.py", line 346, in run_agent_step
    agent_output = await self.take_step(
                   ^^^^^^^^^^^^^^^^^^^^^
  File "/home/root/test/.venv/lib/python3.12/site-packages/llama_index/core/agent/workflow/react_agent.py", line 86, in take_step
    input_chat = react_chat_formatter.format(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/root/test/.venv/lib/python3.12/site-packages/llama_index/core/agent/react/formatter.py", line 81, in format
    fmt_sys_header = self.system_header.format(**format_args)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'context'

Metadata

Metadata

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