Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return when flow changes during pre_actions #111

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

balalofernandez
Copy link

This PR suggests a change in pre-action allowing to call flow_manager.set_node() inside pre-action and perform an early return on the other first set_node call.

Consider this scenario where a pre-action changes the flow to a different node:

async def check_and_redirect_user(action: dict, flow_manager: FlowManager) -> None:
    """Pre-action that checks user state and redirects to a different node if needed."""
    user_state = action.get("user_state", {})
    
    # If the user needs to be redirected to a different flow node
    if user_state.get("needs_onboarding"):
        logger.info("User needs onboarding, redirecting to onboarding node")
        # This call changes the current_node, but the original set_node would continue
        await flow_manager.set_node("onboarding", onboarding_node_config)

Then in a node configuration:

def welcome_node() -> NodeConfig:
    """Welcome node that may redirect to onboarding."""
    return {
        "task_messages": [
            {
                "role": "user",
                "content": "Welcome to our service!"
            }
        ],
        "functions": [...],
        "pre_actions": [
            {
                "type": "check_user_state",
                "handler": check_and_redirect_user,
                "user_state": {"needs_onboarding": True}
            }
        ]
    }

# Usage in main code
await flow_manager.set_node("welcome", welcome_node())

With the fix, when check_and_redirect_user calls set_node("onboarding", ...), the original set_node("welcome", ...) call detects the node change and returns early, preventing it from overwriting the context with the welcome node's configuration.
This ensures that when a pre-action explicitly changes nodes, we respect that decision and don't continue with the original node setup.

I particularly use it to run deterministic functions before, but sometimes I don't want to keep running the original set_node.

Copy link

vercel bot commented Mar 17, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
pipecat-flows ✅ Ready (Inspect) Visit Preview Mar 18, 2025 2:26pm

@balalofernandez balalofernandez marked this pull request as draft March 18, 2025 11:48
@balalofernandez balalofernandez marked this pull request as ready for review March 18, 2025 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant