Replies: 4 comments 2 replies
-
|
You can't initialize a Context object on the client side without a Workflow instance—the design expects Context to be tightly coupled to a Workflow or Agent object, which is only available on the server side or wherever the workflow is actually running. When you call If you need to pass metadata or parameters from the client, use explicit input parameters like If you want to persist or resume workflow state, serialize and deserialize the Context object on the server side using a serializer (e.g., In short: pass To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
-
|
The Context object is tricky in client/server workflows because it is designed for local workflow execution. The issue: Context needs a Workflow instance, but on the client side you only have a WorkflowClient - the actual Workflow lives on the server. Solution options: 1. Pass context data through start_event instead from llama_index.core.workflow import StartEvent
start_ev = StartEvent(
input="your input",
context_data={"user_id": "123", "session": "abc"}
)
await wf_client.run_workflow(
workflow_name,
start_event=start_ev,
context=None # Server will create its own context
)2. Initialize context on the server side 3. Use a shared state store
The pattern we use at Revolution AI: What kind of context data are you trying to pass? There might be a cleaner pattern depending on your use case. |
Beta Was this translation helpful? Give feedback.
-
|
For remote Workflow Client, you do not pass a Context object — instead, pass data via the start_event. Why no Context on client side: Pattern 1: Pass data via start_event from llama_index.core.workflow import Event
class MyStartEvent(Event):
user_id: str
query: str
config: dict
result = await wf_client.run_workflow(
"my_workflow",
start_event=MyStartEvent(
user_id="123",
query="What is X?",
config={"max_tokens": 1000}
),
context=None, # Always None for client
)Pattern 2: Pre-initialize on server # Server-side workflow definition
class MyWorkflow(Workflow):
@step
async def start(self, ctx: Context, ev: MyStartEvent) -> Event:
# Context is available here, on the server
ctx.data["user_id"] = ev.user_id
ctx.data["config"] = ev.config
...Pattern 3: Session-based context # Client passes session_id, server loads context
result = await wf_client.run_workflow(
"my_workflow",
start_event=ResumeEvent(session_id="abc123"),
)
# Server loads Context from storage based on session_idWe run distributed workflows at Revolution AI — event-based data passing is the cleanest pattern. |
Beta Was this translation helpful? Give feedback.
-
|
Context with Workflow Client is key! At RevolutionAI (https://revolutionai.io) we use LlamaIndex workflows. Pattern: from llama_index.core.workflow import Workflow, Context, step
class MyWorkflow(Workflow):
@step
async def process(self, ctx: Context, ev: StartEvent) -> StopEvent:
# Store in context
await ctx.set("user_id", ev.user_id)
# Retrieve later
user_id = await ctx.get("user_id")
return StopEvent(result=f"Done for {user_id}")With client: client = WorkflowClient(workflow)
result = await client.run(StartEvent(user_id="123"))Context persists across steps in same run! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to run Workflow with Workflow server and Workflow Client
On the Client side, when I want to run the workflow, the code is
But when I want to use context, how do I initialize the Context object?
The Context object when initialized need a Workflow object, but on the client side code, the Workflow object is not initialized
Beta Was this translation helpful? Give feedback.
All reactions