llamaIndex - 💡(How to fix) Fix [Bug]: AgentWorkflow leaks `initial_state` mutations across runs [1 pull requests]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Error Message

Relevant Logs/Tracebacks

Fix Action

Fixed

Code Example

### Relevant Logs/Tracebacks
RAW_BUFFERClick to expand / collapse

Bug Description

When the same AgentWorkflow instance is reused for multiple run() calls, mutations made to workflow state during one run are visible in later runs. This appears to violate the documented behavior that workflows are stateless between runs unless a Context is explicitly reused.

The multi-agent path stores self.initial_state in the context by reference, so in-place state mutations mutate the workflow’s own initial_state. The single-agent path uses a shallow copy, which avoids top-level leakage but still leaks nested mutable values such as lists and dicts.

Version

0.14.19

Steps to Reproduce

import asyncio
from llama_index.core.agent.workflow import AgentWorkflow
from llama_index.core.workflow import Context
from llama_index.core.llms.mock import MockFunctionCallingLLM


async def bump_counter(ctx: Context) -> str:
      async with ctx.store.edit_state() as state:
          state["state"]["counter"] += 1
      return "bumped"


async def main() -> None:
      workflow = AgentWorkflow.from_tools_or_functions(
          [bump_counter],
          llm=MockFunctionCallingLLM(),
          system_prompt="Call bump_counter exactly once, then reply 'done'.",
          initial_state={"counter": 0},
      )
      await workflow.run(user_msg="bump it")
      handler = workflow.run(user_msg="bump it")
      await handler
      state = await handler.ctx.store.get("state")
      print(state)

asyncio.run(main())

Relevant Logs/Tracebacks

Expected output : {'counter': 1} 
Actual output : {'counter': 2}

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING