Skip to content

Workflow Strict validation not working #126

@logan-markewich

Description

@logan-markewich

Was trying to run an example from the docs

import { createWorkflow, workflowEvent } from "@llamaindex/workflow-core";
import { withValidation } from "@llamaindex/workflow-core/middleware/validation";

// Define events with explicit types
const inputEvent = workflowEvent<string>();
const validateEvent = workflowEvent<string>();
const processEvent = workflowEvent<string>();
const resultEvent = workflowEvent<string>();
const errorEvent = workflowEvent<Error>({
  debugLabel: "errorEvent", // Add debug labels for better error messages
});

// Define the allowed event flow paths
const workflow = withValidation(createWorkflow(), [
  [[inputEvent], [validateEvent, errorEvent]], // inputEvent can lead to validateEvent or errorEvent
  [[validateEvent], [processEvent, errorEvent]], // validateEvent can lead to processEvent or errorEvent
  [[processEvent], [resultEvent, errorEvent]], // processEvent can lead to resultEvent or errorEvent
  [[errorEvent], [resultEvent]], // errorEvent can lead to resultEvent
]);

// Now use strictHandle to get compile-time validation
workflow.strictHandle([inputEvent], (sendEvent, event) => {
    if (!event.data || event.data.trim().length === 0) {
        throw new Error("Empty input");
    }
    // This is allowed by our validation rules
    console.log("Sending validate event from input event");
    sendEvent(validateEvent.with(event.data.trim()));

    // This would cause a compile-time error:
    console.log("Sending result event from input event");
    sendEvent(resultEvent.with("Result")); // ❌ Not allowed by validation rules
});

// The rest of the workflow with strict validation
workflow.strictHandle([validateEvent], (sendEvent, event) => {
  console.log("Sending process event from validate event");
  // Validation logic here
  sendEvent(processEvent.with(event.data));
});

workflow.strictHandle([processEvent], (sendEvent, event) => {
  console.log("Sending result event from process event");
  // Processing logic here
  sendEvent(resultEvent.with(`Processed: ${event.data}`));
});

workflow.strictHandle([errorEvent], (sendEvent, event) => {
  console.log("Sending result event from error event");
  // Error handling logic here
  sendEvent(resultEvent.with(`Error handled: ${event.data.message}`));
});

const main = async () => {
  const { sendEvent } = workflow.createContext();

  // Cause a validation error
  sendEvent(inputEvent.with("test1"));
}

void main().catch(console.error);

I added extra logs to see what was happening, here's the output

Sending validate event from input event
Invalid input detected [test1]
Sending process event from validate event
Invalid input detected [test1]
Sending result event from process event
Invalid input detected [Processed: test1]
Sending result event from input event
Invalid input detected [Result]

It seems like all handlers are being restricted by the strict handler, like none of the steps are really running properly?

Also, shouldn't this raise an error instead of printing logs or nah?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions