How to clear context? #5

Closed
opened 2026-02-16 02:16:03 -05:00 by yindo · 2 comments
Owner

Originally created by @albertauyeung on GitHub (Jul 21, 2025).

In https://github.com/run-llama/workflows-py/blob/702c0ba2ec4c8a0bf41edce2127c386ab7456ed4/src/workflows/context/context.py#L552 it is mentioned that

DEPRECATED: Use `await ctx.store.set(StateCLS())` instead.
This method is deprecated and will be removed in a future version.

Anyone know how do we use the new statement instead of calling ctx.clear()?

What should we put in the place of StateCLS()? An example would be appreciated. Thank you!

Originally created by @albertauyeung on GitHub (Jul 21, 2025). In https://github.com/run-llama/workflows-py/blob/702c0ba2ec4c8a0bf41edce2127c386ab7456ed4/src/workflows/context/context.py#L552 it is mentioned that ``` DEPRECATED: Use `await ctx.store.set(StateCLS())` instead. This method is deprecated and will be removed in a future version. ``` Anyone know how do we use the new statement instead of calling `ctx.clear()`? What should we put in the place of `StateCLS()`? An example would be appreciated. Thank you!
yindo closed this issue 2026-02-16 02:16:03 -05:00
Author
Owner

@logan-markewich commented on GitHub (Jul 21, 2025):

@albertauyeung you have two options

  1. Using a typed state
class MyState(BaseModel):
  val: int = Field(default=0)

class MyWorkflow(Workflow):
  @step
  async def my_step(self, ctx: Context[MyState], ev: StartEvent) -> StopEvent:
    return StopEvent()

w = MyWorkflow()
ctx = Context(w)

# Reset the state
w.store.set_state(MyState())
  1. Using the default state
from workflows.context.state_store import DictState

class MyWorkflow(Workflow):
  @step
  async def my_step(self, ctx: Context, ev: StartEvent) -> StopEvent:
    return StopEvent()

w = MyWorkflow()
ctx = Context(w)

# Reset the state
w.store.set_state(DictState())

I'm realizing now that the deprecation message is slightly out of date, I'll fix that. Should be .set_state()

@logan-markewich commented on GitHub (Jul 21, 2025): @albertauyeung you have two options 1. Using a typed state ```python class MyState(BaseModel): val: int = Field(default=0) class MyWorkflow(Workflow): @step async def my_step(self, ctx: Context[MyState], ev: StartEvent) -> StopEvent: return StopEvent() w = MyWorkflow() ctx = Context(w) # Reset the state w.store.set_state(MyState()) ``` 2. Using the default state ```python from workflows.context.state_store import DictState class MyWorkflow(Workflow): @step async def my_step(self, ctx: Context, ev: StartEvent) -> StopEvent: return StopEvent() w = MyWorkflow() ctx = Context(w) # Reset the state w.store.set_state(DictState()) ``` I'm realizing now that the deprecation message is slightly out of date, I'll fix that. Should be `.set_state()`
Author
Owner

@albertauyeung commented on GitHub (Jul 23, 2025):

Thanks for the reply! Would be great if you can add these to the docstring.

@albertauyeung commented on GitHub (Jul 23, 2025): Thanks for the reply! Would be great if you can add these to the docstring.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: run-llama/workflows-py#5