Find answers from the community

Updated 5 months ago

Are workflows meant to be created on each request or reused across requests

At a glance
The community member has a question about implementing workflows in the context of a web server. They are unsure whether workflows should be created on each request or if a single instance of a workflow should be used and called on each request. Another community member responds that it is up to the user, and provides some example code demonstrating how to persist the workflow context between runs, which allows reusing a single workflow instance. The original community member indicates that this clarifies the issue.
Hello, just started trying to implement workflows and have a quick question.

In the context of a web server, are workflows meant to be created on each request or are we supposed to create one instance of a workflow and call workflow.run(…) on each request?
L
g
2 comments
Up to you!

By default, workflows are stateless, unless you decide to persist the context between runs

Plain Text
from llama_index.core.workflow.context_serializers import JsonSerializer, JsonPicklerSerializer
from llama_index.core.workflow import Context

# stateless
result = await workflow.run()

# saving the state
handler = workflow.run()
result = await handler

ctx_data = handler.ctx.to_dict(serializer=JsonPicklerSerializer())

# reloading the state
ctx = Context.from_dict(workflow, ctx_data, serializer=JsonPicklerSerializer())
result = await workflow.run(ctx=ctx)
Great thanks, Logan. That clarifies it
Add a reply
Sign up and join the conversation on Discord